Skip to content

Commit

Permalink
feat(getServerState): require renderToString (#5786)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `getServerState` now requires `renderToString` to be passed as an option.
  • Loading branch information
aymeric-giraudet authored and Haroenv committed Aug 7, 2023
1 parent 4db165e commit f9762ce
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 510 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getServerState } from 'react-instantsearch-hooks-server';

export async function getServerSideProps() {
const serverState = await getServerState(<HomePage />);

return {
props: {
serverState,
},
};
}

export async function getServerSideProps2() {
const serverState = await getServerState(<HomePage />, {});

return {
props: {
serverState,
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { getServerState } from 'react-instantsearch';
import { renderToString } from 'react-dom/server';

export async function getServerSideProps() {
const serverState = await getServerState(<HomePage />, {
renderToString
});

return {
props: {
serverState,
},
};
}

export async function getServerSideProps2() {
const serverState = await getServerState(<HomePage />, {
renderToString
});

return {
props: {
serverState,
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ const define = require('jscodeshift/dist/testUtils').defineTest;
define(__dirname, 'src/rish-to-ris', null, 'rish-to-ris/import');
define(__dirname, 'src/rish-to-ris', null, 'rish-to-ris/path');
define(__dirname, 'src/rish-to-ris', null, 'rish-to-ris/use');
define(__dirname, 'src/rish-to-ris', null, 'rish-to-ris/renderToString');
56 changes: 56 additions & 0 deletions packages/instantsearch-codemods/src/rish-to-ris.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,62 @@ export default function transformer(
});
};

const addRenderToStringIfMissing = () => {
const importDeclaration = source
.find(j.ImportDeclaration)
.filter(
(path) => path.node.source.value === 'react-instantsearch-hooks-server'
);

const importSpecifier = importDeclaration
.find(j.ImportSpecifier)
.filter((path) => path.node.imported.name === 'getServerState');

const callExpression = source.find(j.CallExpression, {
callee: { name: 'getServerState' },
});

if (importSpecifier.size() === 0 || callExpression.length === 0) {
return;
}

const hasRenderToString =
source
.find(j.ImportDeclaration)
.filter((path) => path.node.source.value === 'react-dom/server')
.find(j.ImportSpecifier)
.filter((path) => path.node.imported.name === 'renderToString')
.size() > 0;

if (!hasRenderToString) {
importDeclaration.insertAfter(
j.importDeclaration(
[j.importSpecifier(j.identifier('renderToString'))],
j.literal('react-dom/server')
)
);
}

callExpression.forEach((path) => {
if (
path.value.arguments.length === 2 &&
j(path)
.find(j.Property, { key: { name: 'renderToString' } })
.size()
) {
return;
}

const expression = j.template.expression`{ renderToString }`;

path.value.arguments.length === 2
? (path.value.arguments[1] = expression)
: path.value.arguments.push(expression);
});
};

addRenderToStringIfMissing();

replaceImports(
j,
source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('getServerState', () => {
}

await expect(
getServerState(<App />)
getServerState(<App />, { renderToString })
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unable to retrieve InstantSearch's server state in \`getServerState()\`. Did you mount the <InstantSearch> component?"`
);
Expand All @@ -149,7 +149,7 @@ describe('getServerState', () => {
}

await expect(
getServerState(<App />)
getServerState(<App />, { renderToString })
).rejects.toThrowErrorMatchingInlineSnapshot(
`"getServerState should be called with a single InstantSearchSSRProvider and a single InstantSearch component."`
);
Expand All @@ -168,7 +168,7 @@ describe('getServerState', () => {
searchClient,
});

await expect(getServerState(<App />)).rejects.toThrow(
await expect(getServerState(<App />, { renderToString })).rejects.toThrow(
/Search client error/
);
});
Expand All @@ -183,7 +183,7 @@ describe('getServerState', () => {
searchClient,
});

await expect(getServerState(<App />)).rejects.toThrow(
await expect(getServerState(<App />, { renderToString })).rejects.toThrow(
/Search client error/
);
});
Expand All @@ -195,7 +195,7 @@ describe('getServerState', () => {
const searchClient = createAlgoliaSearchClient({});
const { App } = createTestEnvironment({ searchClient });

await getServerState(<App />);
await getServerState(<App />, { renderToString });

expect(searchClient.addAlgoliaAgent).toHaveBeenCalledWith(
`react (${ReactVersion})`
Expand All @@ -218,7 +218,7 @@ describe('getServerState', () => {
const searchClient = createSearchClient({});
const { App } = createTestEnvironment({ searchClient });

await getServerState(<App />);
await getServerState(<App />, { renderToString });

expect(searchClient.search).toHaveBeenCalledTimes(1);
expect(searchClient.search).toHaveBeenCalledWith([
Expand Down Expand Up @@ -333,7 +333,7 @@ describe('getServerState', () => {
const searchClient = createAlgoliaSearchClient({});
const { App } = createTestEnvironment({ searchClient });

const serverState = await getServerState(<App />);
const serverState = await getServerState(<App />, { renderToString });

expect(Object.keys(serverState.initialResults)).toHaveLength(4);
expect(serverState.initialResults).toMatchSnapshot();
Expand All @@ -346,7 +346,8 @@ describe('getServerState', () => {
await getServerState(
<App>
<DynamicWidgets fallbackComponent={RefinementList} />
</App>
</App>,
{ renderToString }
);

expect(searchClient.search).toHaveBeenCalledTimes(2);
Expand All @@ -365,7 +366,8 @@ describe('getServerState', () => {
<Index indexName="something">
<DynamicWidgets fallbackComponent={RefinementList} />
</Index>
</App>
</App>,
{ renderToString }
);

expect(searchClient.search).toHaveBeenCalledTimes(2);
Expand Down Expand Up @@ -403,7 +405,8 @@ describe('getServerState', () => {
await getServerState(
<App>
<DynamicWidgets fallbackComponent={RefinementList} />
</App>
</App>,
{ renderToString }
);

expect(searchClient.search).toHaveBeenCalledTimes(2);
Expand Down Expand Up @@ -452,7 +455,7 @@ describe('getServerState', () => {
});
const { App } = createTestEnvironment({ searchClient });

const serverState = await getServerState(<App />);
const serverState = await getServerState(<App />, { renderToString });
const html = renderToString(<App serverState={serverState} />);

expect(html).toMatchInlineSnapshot(`
Expand Down

This file was deleted.

Loading

0 comments on commit f9762ce

Please sign in to comment.