Skip to content

Commit

Permalink
fix(next-app-router): prevent client-side search when rerendering
Browse files Browse the repository at this point in the history
  • Loading branch information
dhayab committed Nov 28, 2024
1 parent d68f02c commit 53a56c1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/react-instantsearch-nextjs/src/InstantSearchNext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export function InstantSearchNext<
...instantSearchProps
}: InstantSearchNextProps<TUiState, TRouteState>) {
const isMounting = useRef(true);
const isServer = typeof window === 'undefined';

useEffect(() => {
isMounting.current = false;
return () => {
Expand Down Expand Up @@ -77,9 +79,9 @@ This message will only be displayed in development mode.`
<InstantSearchRSCContext.Provider value={promiseRef}>
<InstantSearchSSRProvider initialResults={initialResults}>
<InstantSearch {...instantSearchProps} routing={routing}>
{!initialResults && <InitializePromise nonce={nonce} />}
{isServer && <InitializePromise nonce={nonce} />}
{children}
{!initialResults && <TriggerSearch />}
{isServer && <TriggerSearch />}
</InstantSearch>
</InstantSearchSSRProvider>
</InstantSearchRSCContext.Provider>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @jest-environment jsdom
*/

import { createSearchClient } from '@instantsearch/mocks';
import { wait } from '@instantsearch/testutils';
import { act, render } from '@testing-library/react';
import React from 'react';
import { SearchBox } from 'react-instantsearch';

import { InstantSearchNext } from '../InstantSearchNext';

test('it rerenders without triggering a client-side search', async () => {
const client = createSearchClient();

function Component() {
return (
<InstantSearchNext searchClient={client} indexName="indexName">
<SearchBox />
</InstantSearchNext>
);
}

const { rerender } = render(<Component />);

await act(async () => {
await wait(0);
});

rerender(<Component />);

await act(async () => {
await wait(0);
});

expect(client.search).toHaveBeenCalledTimes(0);
});

0 comments on commit 53a56c1

Please sign in to comment.