@@ -49,38 +49,35 @@ export const addPrefixToDocs = <T extends SerializedOramaDb>(
4949 } ;
5050} ;
5151
52+ const loadOrama = async ( ) => {
53+ const db = create ( {
54+ schema : {
55+ title : 'string' ,
56+ description : 'string' ,
57+ href : 'string' ,
58+ siteSection : 'string' ,
59+ } ,
60+ } ) ;
61+
62+ const indexes = await Promise . all (
63+ Object . entries ( ORAMA_DB_URLS ) . map ( async ( [ key , url ] ) => {
64+ const response = await fetch ( url ) ;
65+ const fetchedDb = ( await response . json ( ) ) as SerializedOramaDb ;
66+ return addPrefixToDocs ( fetchedDb , `/${ key } ` ) ;
67+ } )
68+ ) ;
69+
70+ for ( const index of indexes ) {
71+ await insertMultiple ( db , Object . values ( index . docs . docs ) as Array < never > ) ;
72+ }
73+
74+ return save ( db ) ;
75+ } ;
76+
5277const WithSearch : FC = ( ) => {
5378 const t = useTranslations ( ) ;
5479
55- // `useOrama` expects an initializer that returns serialized RawData.
56- // We build a temporary DB, populate it from every configured index, then
57- // serialize it so the hook can `load()` it into its own client instance.
58- const client = useOrama ( async ( ) => {
59- const db = create ( {
60- schema : {
61- title : 'string' ,
62- description : 'string' ,
63- href : 'string' ,
64- siteSection : 'string' ,
65- } ,
66- } ) ;
67-
68- // Kick off every fetch concurrently — network latency, not CPU, is the
69- // bottleneck here, so serializing these would waste ~N× round-trip time.
70- const indexes = await Promise . all (
71- Object . entries ( ORAMA_DB_URLS ) . map ( async ( [ key , url ] ) => {
72- const response = await fetch ( url ) ;
73- const fetchedDb = ( await response . json ( ) ) as SerializedOramaDb ;
74- return addPrefixToDocs ( fetchedDb , `/${ key } ` ) ;
75- } )
76- ) ;
77-
78- for ( const index of indexes ) {
79- await insertMultiple ( db , Object . values ( index . docs . docs ) as Array < never > ) ;
80- }
81-
82- return save ( db ) ;
83- } ) ;
80+ const client = useOrama ( loadOrama ) ;
8481
8582 return (
8683 < SearchBox
0 commit comments