Skip to content

Fix search bar on Docosaurus 3.9 - #961

Merged
martindekov merged 1 commit into
harvester:mainfrom
martindekov:swizzlesearch9791
Jan 22, 2026
Merged

martindekov merged 1 commit into
harvester:mainfrom
martindekov:swizzlesearch9791

Conversation

@martindekov

@martindekov martindekov commented Jan 21, 2026 •

Copy link
Copy Markdown
Member

Fixing search bar on Docosaurus by swizzling the
problematic package SearchBar of:

docusaurus/theme-search-algolia

And fixing the places where null is returned. Running the site locally with those guards prevented nulls.

Problem:

Search bar does not work properly on newer version and complains it cannot call replace method on null, due to skipped null guards.

Solution:

Extract search bar from the dependency using swizzle and add the guards

Related Issue(s):

harvester/harvester#9791

Test plan:

Ran site and tried to search something and it did not throw exception:
image

Also can be tested in the Deploy Preview below

Additional documentation or context

Following what we did with DocVersionBanner, same was done with SearchBar:

  1. Found the component which throw the exception
  2. Ran swizzle to extract it and it's basically javascript version of this code which is typescript: https://github.com/facebook/docusaurus/blob/v3.9.2/packages/docusaurus-theme-search-algolia/src/theme/SearchBar/index.tsx
  3. Guard nulls
  4. Leave the rest as is

Most likely culprit is here:
algolia/docsearch#2780 (comment)

Fixing search bar on Docosaurus by swizzling the
problematic package SearchBar of:

docusaurus/theme-search-algolia

And fixing the places where null is returned. Running
the site locally with those guards prevented nulls.

Signed-off-by: Martin Dekov <martin.dekov@suse.com>
@github-actions

Copy link
Copy Markdown
Name Link
🔨 Latest commit 0777708
😎 Deploy Preview https://6970ef680cf5a11324fd04a3--harvester-preview.netlify.app

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This WIP pull request addresses search bar issues in Docusaurus 3.9 by swizzling the @docusaurus/theme-search-algolia SearchBar component and adding null guards to prevent runtime errors from null values.

Changes:

  • Added a custom SearchBar component with null guards for navigation URLs
  • Implemented fallback values for search result items and hierarchy fields
  • Added null safety checks for Ask AI feature parameters

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/theme/SearchBar/index.js Resolved
Comment thread src/theme/SearchBar/index.js Resolved
Comment thread src/theme/SearchBar/index.js Resolved
Comment thread src/theme/SearchBar/index.js Resolved
Comment thread src/theme/SearchBar/index.js Resolved
@martindekov martindekov changed the title [WIP] Fix search bar on Docosaurus 3.9 Fix search bar on Docosaurus 3.9 Jan 21, 2026
@martindekov

Copy link
Copy Markdown
Member Author

Resolved the comments of copilot without addressing, I would not like to stray away much from the original implementation, simply attempted to add basic guards against potential null places. Might not be the prettiest javascript code though but process was:

  1. yarn docusaurus swizzle @docusaurus/theme-search-algolia SearchBar --wrap --danger
  2. add guards only when necessary
  3. leave the rest as is

@jillian-maroket
jillian-maroket requested review from Yu-Jack and removed request for akashraj4261 and dariavladykina January 22, 2026 04:06
@Yu-Jack

Yu-Jack commented Jan 22, 2026 •

Copy link
Copy Markdown
Collaborator

I also saw you opened a revert PR #960, but it seems like you have fixed it in this PR. So, merging this PR can fix the search issue, right?

@Yu-Jack Yu-Jack left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one question, when I run the yarn swizzle @docusaurus/theme-search-algolia SearchBar and select JS, Eject and YES: I know what I am doing!. It generated a style.css, do we need that?

@martindekov

Copy link
Copy Markdown
Member Author

I also saw you opened a revert PR #960, but it seems like you have fixed it in this PR. So, merging this PR can fix the search issue, right?

This fixes the issue, yes, the revert was in case I couldn't fix it by EOD, kept them bot open in case you decide you don't like this approach of swizzling things

Just one question, when I run the yarn swizzle @docusaurus/theme-search-algolia SearchBar and select JS, Eject and YES: I know what I am doing!. It generated a style.css, do we need that?

yes it generates style.css, but I removed it and imported it from source as we don't change it:

  return Promise.all([
    import('@docsearch/react/modal'),
    import('@docsearch/react/style'),
    #########################<------------------------------------------------------ the import was here local 
  ]).then(([{DocSearchModal: Modal}]) => {
    DocSearchModal = Modal;
  });

wanted to keep only the relevant parts

@Yu-Jack Yu-Jack left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fix.

@martindekov
martindekov merged commit 0faacb0 into harvester:main Jan 22, 2026
11 checks passed
items;

// Add null guards for all items
return processedItems.map((item) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of ejecting the component, you can probably wrap it and pass an extra transformItems prop to the wrapper to apply your custom logic: less code to maintain.

This trick was first suggested here: meta/docusaurus#8461 (comment)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did try to do that and code with wrapper was a lot less, but for some reason the problem persisted and I just decided to take the whole swizzle and fix in place. We needed this fix fast so that's why I went with this and decided to stop debugging the wrapper approach. Will keep this suggestion open for further reference.

Also very grateful for all your help, truly did not expect once again to go as far as reviewing specific changes on project which adopts the library!

@slorber

slorber commented Jan 22, 2026

Copy link
Copy Markdown

I can't tell for sure, but maybe the problem is that your custom crawler config is different from the recommended setup?

https://github.com/harvester/docs/blob/main/docsearch.json
https://docsearch.algolia.com/docs/templates/#docusaurus-v2--v3-template

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants