Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
instantsearch-hooks-server: use literal imports
Browse files Browse the repository at this point in the history
Next.js build process (using pnpm) ends up not seeing these import
statements cause the import statements didn't have string literals.

The result was that it didn't put these dependencies in the nested
`node_modules` directory and thus this whole package breaks. Notably
this only fails in the production build and not the dev build.

This change is equivalent, we're just reformatting the code so the
literal import statements can be found by whatever tool Next.js is
using.
  • Loading branch information
danielbeardsley committed Sep 9, 2022
1 parent 93ee9d0 commit b181c45
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions packages/react-instantsearch-hooks-server/src/getServerState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,23 @@ function getInitialResults(rootIndex: IndexWidget): InitialResults {
function importRenderToString() {
// React pre-18 doesn't use `exports` in package.json, requiring a fully resolved path
// Thus, only one of these imports is correct
const modules = ['react-dom/server.js', 'react-dom/server'];
const modules = [
// eslint-disable-next-line import/extensions
import('react-dom/server.js').catch(() => {}),
import('react-dom/server').catch(() => {}),
];

// import is an expression to make sure https://github.com/webpack/webpack/issues/13865 does not kick in
return Promise.all(modules.map((mod) => import(mod).catch(() => {}))).then(
(imports: unknown[]) => {
const ReactDOMServer = imports.find(
(mod): mod is { renderToString: typeof reactRenderToString } =>
mod !== undefined
);

if (!ReactDOMServer) {
throw new Error('Could not import ReactDOMServer.');
}
return Promise.all(modules).then((imports: unknown[]) => {
const ReactDOMServer = imports.find(
(mod): mod is { renderToString: typeof reactRenderToString } =>
mod !== undefined
);

return ReactDOMServer.renderToString;
if (!ReactDOMServer) {
throw new Error('Could not import ReactDOMServer.');
}
);

return ReactDOMServer.renderToString;
});
}

0 comments on commit b181c45

Please sign in to comment.