Share driver between multiple instances of SearchProvider
See original GitHub issueThe children of SearchProvider
will only render when the driver has been initialized, which on SSR, only happens client-side:
We use SearchBox
on every page and on the search results page we of course also use SearchResults
(and friends). On the search page we wrap pretty much everything in SearchProvider
, but on the other pages we can’t because that would hurt our SEO (the server-side rendered page would be close to empty).
So what we did is wrap just SearchBox
, but as this lives under a component also used on the search page you end up with two nested SearchProvider
and we noticed they both make the same requests when on the search page.
To work around this for now, we’re using this instead of the SearchProvider
directly:
const SearchProviderOnce = (props: Props) => {
const { children } = props;
return (
<SearchContext.Consumer>
{value => value ? children : (
<SearchProvider config={config}>{children}</SearchProvider>
)}
</SearchContext.Consumer>
);
};
This ensures that we only wrap once.
A more clean solution would be great, e.g. by sharing all SearchProvider
instances (at least those with the same config
property) or their drivers.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:8 (7 by maintainers)
Hey @FokkeZB, I believe I may have resolved a bit unintentionally, and it never occurred to me to follow up here.
https://github.com/elastic/search-ui/commit/2bf0a330ebbc755ac16d5a9643a4d88d3d38265d
I made it so that a
SearchDriver
can be instantiated independently of aSearchProvider
, and then passed in as a prop toSearchProvider
.This essentially means that you can have multiple
SearchProvider
s on a page that all work as a single unit.Here is a working example of this in action: https://codesandbox.io/s/search-ui-national-parks-example-forked-4e5ti?file=/src/App.js
I believe we fixed this issue here. I’m going to close this issue.