@@ -60,9 +60,6 @@ function SearchBarContent() {
6060 }
6161
6262 useEffect ( ( ) => {
63- if ( ! searchButtonRef . current ) {
64- return ;
65- }
6663
6764 const handleKeyDown = ( e ) => {
6865 const kapaContainer = document . getElementById ( 'kapa-widget-container' ) ;
@@ -150,18 +147,22 @@ function SearchBarContent() {
150147 searchInstanceRef . current . destroy ?. ( ) ;
151148 searchInstanceRef . current = null ;
152149 }
153-
154- searchButtonRef . current . innerHTML = '' ;
155150
156- Promise . all ( [
157- import ( 'meilisearch-docsearch' ) ,
158- import ( 'meilisearch-docsearch/css' )
159- ] ) . then ( ( [ { docsearch } ] ) => {
160- const baseOptions = {
161- container : searchButtonRef . current ,
162- host : siteConfig . customFields . meilisearch . host ,
163- apiKey : siteConfig . customFields . meilisearch . apiKey ,
164- indexUid : siteConfig . customFields . meilisearch . indexUid ,
151+ if ( searchButtonRef . current ) {
152+ searchButtonRef . current . innerHTML = '' ;
153+ }
154+
155+ // Initialize docsearch only when container is ready
156+ if ( searchButtonRef . current ) {
157+ Promise . all ( [
158+ import ( 'meilisearch-docsearch' ) ,
159+ import ( 'meilisearch-docsearch/css' )
160+ ] ) . then ( ( [ { docsearch } ] ) => {
161+ const baseOptions = {
162+ container : searchButtonRef . current ,
163+ host : siteConfig . customFields . meilisearch . host ,
164+ apiKey : siteConfig . customFields . meilisearch . apiKey ,
165+ indexUid : siteConfig . customFields . meilisearch . indexUid ,
165166
166167 transformItems : ( items ) => {
167168 return items . map ( ( item ) => {
@@ -241,31 +242,32 @@ function SearchBarContent() {
241242 getMissingResultsUrl : ( { query } ) => {
242243 return `https://github.com/strapi/documentation/issues/new?title=Missing+search+results+for+${ query } ` ;
243244 } ,
244- } ;
245-
246- // Prefer official header wiring if library supports it
247- if ( userId ) {
248- // Some versions accept requestConfig.headers, others accept headers; set both safely
249- baseOptions . requestConfig = {
250- ...( baseOptions . requestConfig || { } ) ,
251- headers : { 'X-MS-USER-ID' : userId }
252245 } ;
253- baseOptions . headers = { ...( baseOptions . headers || { } ) , 'X-MS-USER-ID' : userId } ;
254- }
255246
256- const search = docsearch ( baseOptions ) ;
247+ // Prefer official header wiring if library supports it
248+ if ( userId ) {
249+ // Some versions accept requestConfig.headers, others accept headers; set both safely
250+ baseOptions . requestConfig = {
251+ ...( baseOptions . requestConfig || { } ) ,
252+ headers : { 'X-MS-USER-ID' : userId }
253+ } ;
254+ baseOptions . headers = { ...( baseOptions . headers || { } ) , 'X-MS-USER-ID' : userId } ;
255+ }
257256
258- searchInstanceRef . current = search ;
259- setIsLoaded ( true ) ;
257+ const search = docsearch ( baseOptions ) ;
260258
261- if ( colorMode === 'dark' ) {
262- dropdownRef . current ?. classList . add ( 'dark' ) ;
263- } else {
264- dropdownRef . current ?. classList . remove ( 'dark' ) ;
265- }
266- } ) . catch ( ( error ) => {
267- console . error ( 'Failed to load MeiliSearch:' , error ) ;
268- } ) ;
259+ searchInstanceRef . current = search ;
260+ setIsLoaded ( true ) ;
261+
262+ if ( colorMode === 'dark' ) {
263+ dropdownRef . current ?. classList . add ( 'dark' ) ;
264+ } else {
265+ dropdownRef . current ?. classList . remove ( 'dark' ) ;
266+ }
267+ } ) . catch ( ( error ) => {
268+ console . error ( 'Failed to load MeiliSearch:' , error ) ;
269+ } ) ;
270+ }
269271
270272 return ( ) => {
271273 document . removeEventListener ( 'keydown' , handleKeyDown , true ) ;
0 commit comments