vercel / next.js

The React Framework
https://nextjs.org
MIT License
127.47k stars 27.04k forks source link

Unable to do server-side `react-dom` rendering inside Next API route #64052

Closed EvHaus closed 8 months ago

EvHaus commented 8 months ago

Link to the code that reproduces this issue

https://stackblitz.com/edit/stackblitz-starters-zt49kv

To Reproduce

I'm using react-pdf to generate a PDF file on the server (in a Next.js API route). My PDF has some charts, for which I use react-pdf-charts. That library uses renderToStaticMarkup from react-dom/server to generate some server-side DOM that it parses and injects into the PDF. This all works great in a regular Node.js or Bun app, but when running inside a Next.js API route, it fails with:

⨯ node_modules/recharts/es6/component/TooltipBoundingBox.js (12:106) @ _inherits
 ⨯ TypeError: Super expression must either be null or a function
    at _inherits (webpack-internal:///(rsc)/./node_modules/recharts/es6/component/TooltipBoundingBox.js:19:113)
    at eval (webpack-internal:///(rsc)/./node_modules/recharts/es6/component/TooltipBoundingBox.js:28:3)
    at eval (webpack-internal:///(rsc)/./node_modules/recharts/es6/component/TooltipBoundingBox.js:159:2)
    at (rsc)/./node_modules/recharts/es6/component/TooltipBoundingBox.js (file:///home/projects/stackblitz-starters-zt49kv/.next/server/vendor-chunks/recharts.js:260:1)
    at __webpack_require__ (file:///home/projects/stackblitz-starters-zt49kv/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/recharts/es6/component/Tooltip.js:8:77)
    at (rsc)/./node_modules/recharts/es6/component/Tooltip.js (file:///home/projects/stackblitz-starters-zt49kv/.next/server/vendor-chunks/recharts.js:250:1)
    at __webpack_require__ (file:///home/projects/stackblitz-starters-zt49kv/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/recharts/es6/chart/generateCategoricalChart.js:25:77)
    at (rsc)/./node_modules/recharts/es6/chart/generateCategoricalChart.js (file:///home/projects/stackblitz-starters-zt49kv/.next/server/vendor-chunks/recharts.js:160:1)
    at __webpack_require__ (file:///home/projects/stackblitz-starters-zt49kv/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/recharts/es6/chart/BarChart.js:5:83)
    at (rsc)/./node_modules/recharts/es6/chart/BarChart.js (file:///home/projects/stackblitz-starters-zt49kv/.next/server/vendor-chunks/recharts.js:150:1)
    at __webpack_require__ (file:///home/projects/stackblitz-starters-zt49kv/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./app/api/build-pdf/MyDocument.tsx:10:128)
    at (rsc)/./app/api/build-pdf/MyDocument.tsx (file:///home/projects/stackblitz-starters-zt49kv/.next/server/app/api/build-pdf/route.js:342:1)
    at __webpack_require__ (file:///home/projects/stackblitz-starters-zt49kv/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./app/api/build-pdf/route.tsx:9:69)
    at (rsc)/./app/api/build-pdf/route.tsx (file:///home/projects/stackblitz-starters-zt49kv/.next/server/app/api/build-pdf/route.js:352:1)
    at __webpack_require__ (file:///home/projects/stackblitz-starters-zt49kv/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fbuild-pdf%2Froute&page=%2Fapi%2Fbuild-pdf%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fbuild-pdf%2Froute.tsx&appDir=%2Fhome%2Fprojects%2Fstackblitz-starters-zt49kv%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2Fhome%2Fprojects%2Fstackblitz-starters-zt49kv&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!:17:127)
    at (rsc)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fbuild-pdf%2Froute&page=%2Fapi%2Fbuild-pdf%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fbuild-pdf%2Froute.tsx&appDir=%2Fhome%2Fprojects%2Fstackblitz-starters-zt49kv%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2Fhome%2Fprojects%2Fstackblitz-starters-zt49kv&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D! (file:///home/projects/stackblitz-starters-zt49kv/.next/server/app/api/build-pdf/route.js:332:1)
    at __webpack_require__ (file:///home/projects/stackblitz-starters-zt49kv/.next/server/webpack-runtime.js:33:43)
    at __webpack_exec__ (file:///home/projects/stackblitz-starters-zt49kv/.next/server/app/api/build-pdf/route.js:362:39)
    at eval (file:///home/projects/stackblitz-starters-zt49kv/.next/server/app/api/build-pdf/route.js:363:1141)
    at __webpack_require__.X (file:///home/projects/stackblitz-starters-zt49kv/.next/server/webpack-runtime.js:163:21)
    at eval (file:///home/projects/stackblitz-starters-zt49kv/.next/server/app/api/build-pdf/route.js:363:47)
    at Object.eval (file:///home/projects/stackblitz-starters-zt49kv/.next/server/app/api/build-pdf/route.js:366:3)
    at Object.function (https://stackblitzstarterszt49kv-if1z.w-credentialless-staticblitz.com/blitz.7dbe22a9.js:352:194333)
    at Module._compile (https://stackblitzstarterszt49kv-if1z.w-credentialless-staticblitz.com/blitz.7dbe22a9.js:54:14871)
    at Module._extensions..js (https://stackblitzstarterszt49kv-if1z.w-credentialless-staticblitz.com/blitz.7dbe22a9.js:54:15550)
    at Module.load (https://stackblitzstarterszt49kv-if1z.w-credentialless-staticblitz.com/blitz.7dbe22a9.js:54:13457)
    at Module._load (https://stackblitzstarterszt49kv-if1z.w-credentialless-staticblitz.com/blitz.7dbe22a9.js:54:10535)
    at Module.require (https://stackblitzstarterszt49kv-if1z.w-credentialless-staticblitz.com/blitz.7dbe22a9.js:54:13775)
    at mod.require (file:///home/projects/stackblitz-starters-zt49kv/node_modules/next/dist/server/require-hook.js:65:28)
    at i (https://stackblitzstarterszt49kv-if1z.w-credentialless-staticblitz.com/blitz.7dbe22a9.js:98:2198)
    at _0x13b05e (https://stackblitzstarterszt49kv-if1z.w-credentialless-staticblitz.com/blitz.7dbe22a9.js:352:193518)
    at requirePage (file:///home/projects/stackblitz-starters-zt49kv/node_modules/next/dist/server/require.js:109:84)
    at eval (file:///home/projects/stackblitz-starters-zt49kv/node_modules/next/dist/server/load-components.js:98:84)
    at async loadComponentsImpl (file:///home/projects/stackblitz-starters-zt49kv/node_modules/next/dist/server/load-components.js:98:26)
    at async DevServer.findPageComponentsImpl (file:///home/projects/stackblitz-starters-zt49kv/node_modules/next/dist/server/next-server.js:664:36)
    at async DevServer.findPageComponents (file:///home/projects/stackblitz-starters-zt49kv/node_modules/next/dist/server/dev/next-dev-server.js:565:20)
    at async DevServer.renderPageComponent (file:///home/projects/stackblitz-starters-zt49kv/node_modules/next/dist/server/base-server.js:1832:24)
    at async DevServer.renderToResponseImpl (file:///home/projects/stackblitz-starters-zt49kv/node_modules/next/dist/server/base-server.js:1882:32)
    at async DevServer.pipeImpl (file:///home/projects/stackblitz-starters-zt49kv/node_modules/next/dist/server/base-server.js:895:25)
    at async NextNodeServer.handleCatchallRenderRequest (file:///home/projects/stackblitz-starters-zt49kv/node_modules/next/dist/server/next-server.js:269:17)
    at async DevServer.handleRequestImpl (file:///home/projects/stackblitz-starters-zt49kv/node_modules/next/dist/server/base-server.js:791:17)
    at async eval (file:///home/projects/stackblitz-starters-zt49kv/node_modules/next/dist/server/dev/next-dev-server.js:331:20)
    at async Span.traceAsyncFn (file:///home/projects/stackblitz-starters-zt49kv/node_modules/next/dist/trace/trace.js:151:20)
    at async DevServer.handleRequest (file:///home/projects/stackblitz-starters-zt49kv/node_modules/next/dist/server/dev/next-dev-server.js:328:24) {
  page: '/api/build-pdf'

I'm the author of react-pdf-charts and I can't find any way to get this to work in Next.js. It only seems to be a problem inside Next.js. I'm guessing something to do with RSC thinking that this renderToStaticMarkup call is trying to render on the client... even though it's actually on the server.

Note, I'm using:

const nextConfig = {
  experimental: {
    serverComponentsExternalPackages: ['@react-pdf/renderer'],
  },
};

As without it, the app crashes instead with this error:

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

Any recommendations on how this can be addressed would be really helpful! Thank you.

Current vs. Expected behavior

Next.js app crashes with the stacktrace above. The expectation is that it doesn't crash and continues to generate the PDF file in my API route.

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: Ubuntu 20.04.0 LTS Wed Apr 03 2024 23:06:32 GMT-0700 (Pacific Daylight Time)
Binaries:
  Node: 18.18.0
  npm: 10.2.3
  Yarn: 1.22.19
  pnpm: 8.15.3
Relevant Packages:
  next: 14.1.4
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

App Router

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

No response

github-actions[bot] commented 8 months ago

We could not detect a valid reproduction link. Make sure to follow the bug report template carefully.

Why was this issue closed?

To be able to investigate, we need access to a reproduction to identify what triggered the issue. We need a link to a public GitHub repository (template for App Router, template for Pages Router), but you can also use these templates: CodeSandbox: App Router or CodeSandbox: Pages Router.

The bug template that you filled out has a section called "Link to the code that reproduces this issue", which is where you should provide the link to the reproduction.

What should I do?

Depending on the reason the issue was closed, you can do the following:

In general, assume that we should not go through a lengthy onboarding process at your company code only to be able to verify an issue.

My repository is private and cannot make it public

In most cases, a private repo will not be a sufficient minimal reproduction, as this codebase might contain a lot of unrelated parts that would make our investigation take longer. Please do not make it public. Instead, create a new repository using the templates above, adding the relevant code to reproduce the issue. Common things to look out for:

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps by opening a new issue.

I think my reproduction is good enough, why aren't you looking into it quickly?

We look into every Next.js issue and constantly monitor open issues for new comments.

However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources