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

Commit

Permalink
fix(hooks-server): import react server via an expression (#3515)
Browse files Browse the repository at this point in the history
* fix(hooks-server): import react server via an expression

This is still WIP, but can be tried out via codesandbox now.

fixes #3512

* clarified comment
  • Loading branch information
Haroenv committed Jun 13, 2022
1 parent fc94d80 commit 91b96f7
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions packages/react-instantsearch-hooks-server/src/getServerState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,23 @@ function getInitialResults(rootIndex: IndexWidget): InitialResults {
}

function importRenderToString() {
return Promise.all([
// React pre-18 doesn't use `exports` in package.json, requiring a fully resolved path
// Thus, only one of these imports is correct
// eslint-disable-next-line import/extensions
import('react-dom/server.js').catch(() => {}),
import('react-dom/server').catch(() => {}),
]).then((imports) => {
const ReactDOMServer = imports.find(
(mod): mod is { renderToString: typeof RenderToString } =>
mod !== undefined
);
// 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'];

// 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 RenderToString } =>
mod !== undefined
);

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

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

return ReactDOMServer.renderToString;
});
);
}

0 comments on commit 91b96f7

Please sign in to comment.