@@ -39,6 +39,12 @@ function SearchBarContent() {
3939 const uuid = ( typeof crypto !== 'undefined' && crypto . randomUUID ) ? crypto . randomUUID ( ) : Math . random ( ) . toString ( 36 ) . slice ( 2 ) + Date . now ( ) . toString ( 36 ) ;
4040 const value = JSON . stringify ( { id : uuid , month : monthKey } ) ;
4141 window . localStorage . setItem ( key , value ) ;
42+ try {
43+ // Also set a same‑origin cookie so serverless proxy can read it
44+ const ttlDays = 45 ; // longer than a month for safety; we rotate monthly client‑side
45+ const maxAge = ttlDays * 24 * 60 * 60 ;
46+ document . cookie = `msUserId=${ uuid } ; Max-Age=${ maxAge } ; Path=/; SameSite=Lax` + ( window . location . protocol === 'https:' ? '; Secure' : '' ) ;
47+ } catch { }
4248 return uuid ;
4349 } catch ( _ ) {
4450 return null ;
@@ -158,9 +164,12 @@ function SearchBarContent() {
158164 import ( 'meilisearch-docsearch' ) ,
159165 import ( 'meilisearch-docsearch/css' )
160166 ] ) . then ( ( [ { docsearch } ] ) => {
167+ const meiliHost = siteConfig . customFields . meilisearch . host ;
168+ // Use proxy on non-localhost to avoid CORS limits and inject server-side header
169+ const useProxy = typeof window !== 'undefined' && window . location && window . location . hostname !== 'localhost' ;
161170 const baseOptions = {
162171 container : searchButtonRef . current ,
163- host : siteConfig . customFields . meilisearch . host ,
172+ host : useProxy ? ` ${ window . location . origin } /api` : meiliHost ,
164173 apiKey : siteConfig . customFields . meilisearch . apiKey ,
165174 indexUid : siteConfig . customFields . meilisearch . indexUid ,
166175
@@ -256,6 +265,8 @@ function SearchBarContent() {
256265
257266 const search = docsearch ( baseOptions ) ;
258267
268+ // If using proxy, docsearch will call `${origin}/api/indexes/<uid>/search`
269+
259270 searchInstanceRef . current = search ;
260271 setIsLoaded ( true ) ;
261272
0 commit comments