0.2.0 • Published 3 years ago
instantsearch-router-next-experimental v0.2.0
instantsearch-router-next-experimental
This package is an experimental router for React InstantSearch Hooks that is compatible with Next.js routing.
Warning As the name suggests, this package is experimental and its name and API will change in the future.
Installation
yarn add instantsearch-router-next-experimental
# or with npm
npm install instantsearch-router-next-experimentalUsage
If you are doing SSR with the getServerState and InstantSearchSSRProvider from react-instantsearch-hooks-server, you need to pass the url prop to createInstantSearchNextRouter's serverUrl option :
import { createInstantSearchNextRouter } from 'instantsearch-router-next-experimental';
export default function Page({ serverState, url }) {
return (
<InstantSearchSSRProvider {...serverState}>
<InstantSearch
searchClient={searchClient}
indexName="instant_search"
routing={{ router: createInstantSearchNextRouter({ serverUrl: url }) }}
>
{/* ... */}
</InstantSearch>
</InstantSearchSSRProvider>
);
}If you are not doing SSR but only CSR, you can omit the serverUrl option:
import { createInstantSearchNextRouter } from 'instantsearch-router-next-experimental';
export default function Page() {
return (
<InstantSearch
searchClient={searchClient}
indexName="instant_search"
routing={{ router: createInstantSearchNextRouter() }}
>
{/* ... */}
</InstantSearch>
);
}Lastly, if you had custom routing logic in your app, you can pass it to the createInstantSearchNextRouter's routerOptions option:
import { createInstantSearchNextRouter } from 'instantsearch-router-next-experimental';
export default function Page({ serverState, url }) {
return (
{/* ... */}
<InstantSearch
searchClient={searchClient}
indexName="instant_search"
routing={{
router: createInstantSearchNextRouter({
serverUrl: url,
routerOptions: {
createURL: /* ... */,
parseURL: /* ... */,
},
}),
// if you are using a custom `stateMapping` you can still pass it :
stateMapping: /* ... */,
}}
>
{/* ... */}
</InstantSearch>
{/* ... */}
);
}API
The options are :
serverUrl: the URL of the page on the server. It's used to compute the initialuiState.routerOptions: the options passed to thehistoryrouter. See the documentation for more details. If you need to overridegetLocationmake sure to handle server-side rendering by checking ifwindowis defined or not.
For troubleshooting purposes, some other options are available :
doNotOverrideBeforePopState: if you provided a custombeforePopStateto the next router, you can pass this option tocreateInstantSearchNextRouterto prevent it from overriding it. You will however need to handle thebeforePopStatelogic yourself.- in
routerOptions:beforeStart: a function called when the router starts. It receives a callback which should normally besetUiStateas well as therouterinstance.beforeDispose: a function called when the router is disposed. It's used to detach events which were attached inbeforeStart/onUpdate.