Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next.js App Router react-dom server error #109

Open
3 tasks done
rodrgds opened this issue Oct 11, 2023 · 8 comments
Open
3 tasks done

Next.js App Router react-dom server error #109

rodrgds opened this issue Oct 11, 2023 · 8 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@rodrgds
Copy link

rodrgds commented Oct 11, 2023

Describe the bug

I'm getting the following error when I try to render a chart using ReactPDFChart in Next.js:

ReactServerComponentsError:

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.
Learn more: https://nextjs.org/docs/getting-started/react-essentials

The error was caused by importing 'react-pdf-charts/dist/index.js' in './pdfs/report.tsx'.

Versions:

"next": "13.4.8",
"@joshuajaco/react-pdf-renderer-bundled": "^3.1.12",
"recharts": "^2.8.0",
"react-pdf-charts": "^0.2.1",

I am also using @joshuajaco/react-pdf-renderer-bundled instead of regular react-pdf because of another bug with Next.js. I'm unsure of whether that has any implications on this particular case.

Reproduction

Using some chart, for example:

pdfs/report.tsx

...
  <ReactPDFChart>
    <PieChart width={500} height={300}>
      <Pie
        data={data}
        dataKey="value"
        nameKey="name"
        cx="50%"
        cy="50%"
        outerRadius={80}
        fill="#8884d8"
        label
      >
        {data.map((entry, index) => (
          <Cell
            key={`cell-${index}`}
            fill={COLORS[index % COLORS.length]}
          />
        ))}
      </Pie>
      <Legend />
    </PieChart>
  </ReactPDFChart>
...

This is how I render it and return it to the client, on a route.jsx API "page" (which worked without the charts):

some-api/route.jsx

const pdfBuffer = await renderToBuffer(
  <ReportPDF
    assessmentData={assessmentDataJson}
    companyData={companyDataJson}
    userStats={newUserStats}
    stats={stats}
  />
);

return new Response(pdfBuffer, {
  headers: {
    "Content-Type": "application/pdf",
    "Content-Disposition": `attachment; filename="invoice.pdf"`,
  },
});

Validations

@rodrgds rodrgds added the bug Something isn't working label Oct 11, 2023
@EvHaus
Copy link
Owner

EvHaus commented Oct 12, 2023

Does it work if you add use client on the file which imports react-pdf-charts?

@rodrgds
Copy link
Author

rodrgds commented Oct 15, 2023

It doesn't. It now displays a different error:

Error: Attempted to call the default export of /home/kraktoos/dev/abc/pdfs/report.tsx from the server but it's on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.

@EvHaus EvHaus added the help wanted Extra attention is needed label Oct 18, 2023
@EvHaus
Copy link
Owner

EvHaus commented Feb 1, 2024

I was able to get around the ReactServerComponentsError by using experimental.serverComponentsExternalPackages, but recharts is now failing to render on the server because it's using some client code that Next 14 can't run in a route handler. 😠

Here's where I got to: https://stackblitz.com/edit/stackblitz-starters-zt49kv?file=package.json

It currently fails with:

TypeError: Super expression must either be null or a function
node_modules/react-smooth/es6/Animate.js (19:112)
node_modules/react-smooth/es6/Animate.js (34:2)
node_modules/react-smooth/es6/Animate.js (314:2)

UPDATE: Unfortunately Next 14.2 still has the same issue. Here's a repo to reproduce the issue: https://github.com/EvHaus/react-pdf-charts-next-14

@dominik-juriga
Copy link

I was able to get around the ReactServerComponentsError by using experimental.serverComponentsExternalPackages, but recharts is now failing to render on the server because it's using some client code that Next 14 can't run in a route handler. 😠

Here's where I got to: https://stackblitz.com/edit/stackblitz-starters-zt49kv?file=package.json

It currently fails with:

TypeError: Super expression must either be null or a function
node_modules/react-smooth/es6/Animate.js (19:112)
node_modules/react-smooth/es6/Animate.js (34:2)
node_modules/react-smooth/es6/Animate.js (314:2)

Hi,

Have you ever found the solution to this problem? I have a couple charts that I need to render into a PDF in a route handler. This seems like the most elegant option, but I am stuck on the Animate.js issue.

@poojas10
Copy link

poojas10 commented Apr 3, 2024

I am getting this issue "Module not found: Error: Can't resolve 'react/jsx-runtime" and "Module not found: Error: Can't resolve 'react-dom/server" in react app where my react and react-dom both are pointing to version 17. How can I resolve it

@EvHaus
Copy link
Owner

EvHaus commented Apr 4, 2024

I don't know how to resolve it. It seems to be a bug (or limitation of) Next.js. I've opened an issue with them here. Feel free to upvote it to raise its priority.

@poojas10
Copy link

poojas10 commented Apr 4, 2024

I don't know how to resolve it. It seems to be a bug (or limitation of) Next.js. I've opened an issue with them here. Feel free to upvote it to raise its priority.

I am not using nextjs I have a react app with version 17 but I am getting the same error

@EvHaus
Copy link
Owner

EvHaus commented Apr 4, 2024

Can you please create a repo with your setup and share it with me? I can take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants