next.js: Unable to import react-dom/server in a server component
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 22.1.0: Sun Oct 9 20:14:30 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8103 Binaries: Node: 16.18.0 npm: 8.19.2 Yarn: 1.22.19 pnpm: 7.13.6 Relevant packages: next: 13.0.7-canary.1 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true)
Link to the code that reproduces this issue
https://stackblitz.com/edit/vercel-next-js-arwqsz?file=app%2Fpage.js
To Reproduce
app/page.js:
import ReactDOMServer from "react-dom/server";
const staticMarkup = ReactDOMServer.renderToStaticMarkup(<p>TEST</p>);
export default function TestPage() {
return <p>123</p>;
}
Run next dev and try opening the index page.
Describe the Bug
I’m receiving an error message:
Failed to compile. ./app/page.js
You’re importing a component that imports react-dom/server. To fix it, render or return the content directly as a Server Component instead for perf and security.
Maybe one of these should be marked as a client entry “use client”: app/page.js
Expected Behavior
Be able to use react-dom/server on the server.
My usecase it to take some React tree, render it so markup, run it through another postprocessing step which is not aware of anything React specific, and then, return an iframe containing that html.
Contrary to what the error message says, I think there’s nothing to fix about importing react-dom/server, and neither should any of this require using the “use client” directive
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 60
- Comments: 23 (2 by maintainers)
I found this while trying to render a react component svg in a
route.tsxhandler:Importing ReactDOMServer dynamically does fix this. So thank you – just wanted to share my usecase 😃
It’s similar to what https://github.com/FormidableLabs/react-ssr-prepass and others like https://github.com/kmoskwiak/useSSE rely on (e.g. a real or simulated pre-render pass to hoist certain work and then actually send it already digested into SSR as initial props)
In general, this kind of pattern should become even more of an edge-case, as with RSC you could fetch/do async stuff anywhere and Next.js would attempt to deduplicate the requests.
However, if you do need to render something static (as your example shows, but not pre-rendering your own app itself), you might be able to work around it by dynamically importing
ReactDOMServer(see forked stackblitz).But trying to render your actual tree of components will likely fail as they’ll be client or server components and both cases will error out (not quite sure how Next.js internally is handling this to be able to render these new trees, if there’s any webpack work involved and why it doesn’t just work on its own)
Same problem here. My use case is rendering a page with various components into a static RSS feed using
ReactDOMServer.renderToStaticMarkup.I was able to import
and use it on the server
Here’s my solution to render jsx to string on both client and server on NextJS > 13 without having server component issue in import
// render-client.js
// render-server.ts
// index.tsx
I ran into this as well with a similar use case (I want to render React components as static HTML and do post-processing on that HTML, such as removing or replacing HTML tags).
It should be noted that
react-dom/serveris an official, documented React API. I don’t think Next.js should be prohibiting its usage.It works indeed, but crashes with
If using
"use client"in the tsx file.That might feel obvious, but my need was to generate React code from an API endpoint which would return HTML, and have some React “client” run on the frontend, but I couldn’t achieve that.
I would greatly appreciate it if someone could help on this issue.
//client component with some browser context
while converting the client component to HTML-string getting below error (renderToString(<ClientComponent content={content} />)) or renderToStaticMarkup
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
So, could some of you please suggest how to convert a client in a server page to pass that updated HTML string to render dynamically in another component using dangerouslySetInnerHTML?
thank you.
when I try to render a dynamic components with props it gives me this Error:
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.My code is like below and it runs when we call an api (so in the server side):
does anyone has a solution?
Thanks, dynamic import worked for me! My use case is to convert mdx to text to prepare my content for meilisearch, I think that use case is legitimate. Had to do something like this:
this worked for me:
This Solution by @hdodov worked, But I am facing another issue now with next.js to ‘use client’, as most of my app components use styled-components so I have to write ‘use client’, which will not allow than using that inside server api, Looks like next.js is making things hard now, Even ServerStyleSheet will not work on server side, Am i missing anything
import { ServerStyleSheet } from 'styled-components';My requirement is to generate html templates from existing react components and send html for pdf file generation.
I don’t think
react-dom/server.browseris a good idea, since that’s a different bundle, made specifically for the browser, and not Node.I hacked my way around this by creating my own wrapper around
react-dom/server.utils/react-dom.tspage.tsxThis even preserves the types and I can use
renderToStaticMarkupas usual, except I have to load it from@/utils/react-domand notreact-dom/server.The only downside I see is that there is a fraction of a second where
renderToStaticMarkupwould beundefinedwhen the server starts, but I think this wouldn’t realistically be a problem.Same Problem here. Has anyone found a solution to this issue?