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

Could not import ReactDOMServer #3633

Closed
slackerzz opened this issue Sep 23, 2022 · 7 comments · Fixed by #3658
Closed

Could not import ReactDOMServer #3633

slackerzz opened this issue Sep 23, 2022 · 7 comments · Fixed by #3658

Comments

@slackerzz
Copy link

🐛 Bug description

ERROR	Error: Could not import ReactDOMServer.
    at file:///var/task/node_modules/.pnpm/react-instantsearch-hooks-server@6.33.0_fzyfqr3mixyxftdbf7mmfo2jb4/node_modules/react-instantsearch-hooks-server/dist/es/getServerState.js:166:13
    at async getServerSideProps (/var/task/site/.next/server/pages/algolia.js:98:25)
    at async Object.renderToHTML (/var/task/node_modules/.pnpm/next@12.3.0_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/render.js:512:20)
    at async doRender (/var/task/node_modules/.pnpm/next@12.3.0_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/base-server.js:642:38)
    at async cacheEntry.responseCache.get.isManualRevalidate.isManualRevalidate (/var/task/node_modules/.pnpm/next@12.3.0_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/base-server.js:747:28)
    at async /var/task/node_modules/.pnpm/next@12.3.0_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/response-cache/index.js:80:36 {  page: '/algolia'}
RequestId: 3de89872-78cc-4717-adcf-7b57a96a1de9 Error: Runtime exited with error: exit status 1
Runtime.ExitError

I'm using react-instantsearch-hooks-server with Next.js (vercel/commerce) and I have this error only in production (Vercel), in local dev environment it works as expected.
The error started to appear with React 18, with 17 it worked.

🔍 Bug reproduction

I tried to reproduce the bug in a codesandbox but it doesn't happen.
I've setup a repo with an example at https://github.com/slackerzz/vercel-commerce
It is a clean vercel/commerce with a route /algolia where i use the ssr provider.
The code is availabel at https://github.com/slackerzz/vercel-commerce/blob/main/site/pages/algolia.tsx
The index used is from https://codesandbox.io/s/github/algolia/react-instantsearch/tree/master/examples/hooks-next

In a local environment running pnpm dev the /algolia route works as expected:

Schermata 2022-09-23 alle 14 40 16

After deploying the app to Vercel the same route returns a 500 error

Environment

  • React InstantSearch Hooks version: 6.33
  • React version: 18.2.0
  • Next.js version: 12.3.1

Additional context

@Haroenv
Copy link
Contributor

Haroenv commented Sep 23, 2022

I feel like this is a new bug that was already attempted to be fixed in #3618, does that PR work for you? the situation is complicated as Webpack will try to import both the react 17 and react 18 import if it's a literal import, while pnpm removes the package if not in a literal import

@slackerzz
Copy link
Author

Thank you @Haroenv , adding a pnpm patch with the changes from #3618 fixes the problem

@Haroenv
Copy link
Contributor

Haroenv commented Sep 23, 2022

Thanks for confirming, we'll look more into it

@fireseasonnow
Copy link

Thank you @Haroenv , adding a pnpm patch with the changes from #3618 fixes the problem

Sadly, it doesn't work for me. Although I don't get Error: Could not import ReactDOMServer. anymore, page seems to load endlessly and throws 504 time-out. Locally no issues.

My environment:

  • React InstantSearch Hooks version: 6.36
  • React version: 18.2.0
  • Next.js version: 12.3.1

Any upcoming fixes @Haroenv?

@Haroenv
Copy link
Contributor

Haroenv commented Oct 12, 2022

Are you also using pnpm @whitebackdoor ? if so you can use the patch

@fireseasonnow
Copy link

I'm using pnpm but patch doesn't fix the issue for me.

@Haroenv
Copy link
Contributor

Haroenv commented Oct 12, 2022

Please open a new issue with reproduction in that case, I guess it might not be the same issue @whitebackdoor

Haroenv added a commit that referenced this issue Oct 19, 2022
There are some cases where the combination of trying to make sure renderToString doesn't end up in a browser bundle, being runnable on esm + cjs, react 17 and 18, .js extension etc. blows up. One of those is pnpm caching removing "unused" packages.

This PR introduces a new argument `renderToString` to `getServerState` so you can inject the dependency yourself, meaning the import is within your own code and won't be purged.

```js
import { renderToString } from 'react-dom/server';
await getServerState(<App/>, renderToString);
await getServerState(<App/>, import('react-dom/server').then(mod => mod.renderToString));
```

fixes #3633
closes #3618
see vercel/next.js#40067
Haroenv added a commit that referenced this issue Oct 20, 2022
**Summary**

There are some cases where the combination of trying to make sure
renderToString doesn't end up in a browser bundle, being runnable on esm
+ cjs, react 17 and 18, .js extension etc. blows up.

One of those is pnpm/vercel removing "unused" packages.




<!--
  Thanks for submitting a pull request!
Please provide enough information so that others can review your pull
request.
-->


<!--
  Explain the **motivation** for making this change.
  What existing problem does the pull request solve?
  Are there any linked issues?
-->

**Result**


This PR introduces a new argument `renderToString` to `getServerState`
so you can inject the dependency yourself, meaning the import is within
your own code and won't be purged.

```js
import { renderToString } from 'react-dom/server';
await getServerState(<App/>, renderToString);
await getServerState(<App/>, import('react-dom/server').then(mod => mod.renderToString));
```

<!--
  Demonstrate the code is solid.
  Example: The exact commands you ran and their output,
  screenshots / videos if the pull request changes UI.
-->

fixes #3633
closes #3618
see vercel/next.js#40067
FX-1869

Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com>
Haroenv added a commit to algolia/instantsearch that referenced this issue Jan 4, 2023
…eact-instantsearch#3658)

**Summary**

There are some cases where the combination of trying to make sure
renderToString doesn't end up in a browser bundle, being runnable on esm
+ cjs, react 17 and 18, .js extension etc. blows up.

One of those is pnpm/vercel removing "unused" packages.




<!--
  Thanks for submitting a pull request!
Please provide enough information so that others can review your pull
request.
-->


<!--
  Explain the **motivation** for making this change.
  What existing problem does the pull request solve?
  Are there any linked issues?
-->

**Result**


This PR introduces a new argument `renderToString` to `getServerState`
so you can inject the dependency yourself, meaning the import is within
your own code and won't be purged.

```js
import { renderToString } from 'react-dom/server';
await getServerState(<App/>, renderToString);
await getServerState(<App/>, import('react-dom/server').then(mod => mod.renderToString));
```

<!--
  Demonstrate the code is solid.
  Example: The exact commands you ran and their output,
  screenshots / videos if the pull request changes UI.
-->

fixes algolia/react-instantsearch#3633
closes algolia/react-instantsearch#3618
see vercel/next.jsalgolia/react-instantsearch#40067
FX-1869

Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants