-
Notifications
You must be signed in to change notification settings - Fork 95
Fix search bar on Docosaurus 3.9 #961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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>
|
There was a problem hiding this comment.
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.
|
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:
|
|
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
left a comment
There was a problem hiding this comment.
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?
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
yes it generates style.css, but I removed it and imported it from source as we don't change it: wanted to keep only the relevant parts |
Yu-Jack
left a comment
There was a problem hiding this comment.
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.
| items; | ||
|
|
||
| // Add null guards for all items | ||
| return processedItems.map((item) => { |
There was a problem hiding this comment.
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: facebook/docusaurus#8461 (comment)
There was a problem hiding this comment.
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!
|
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 |
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:

Also can be tested in the Deploy Preview below
Additional documentation or context
Following what we did with DocVersionBanner, same was done with SearchBar:
Most likely culprit is here:
algolia/docsearch#2780 (comment)