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
Describe the bug
I'm getting the following error when I try to render a chart using ReactPDFChart in Next.js:
Versions:
I am also using
@joshuajaco/react-pdf-renderer-bundledinstead of regularreact-pdfbecause 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.tsxThis is how I render it and return it to the client, on a
route.jsxAPI "page" (which worked without the charts):some-api/route.jsxValidations