Skip to content

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

Description

@rodrgds

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions