vercel / next.js

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

Next.js throws `RangeError: Maximum call stack size exceeded` when running `yarn build` #58494

Open danieltroger opened 10 months ago

danieltroger commented 10 months ago

Link to the code that reproduces this issue

https://github.com/danieltroger/react-server-components-stuff/tree/dt/next_infinite_loop

To Reproduce

  1. git clone git@github.com:danieltroger/react-server-components-stuff.git
  2. cd react-server-components-stuff
  3. git checkout dt/next_infinite_loop
  4. yarn build

Current vs. Expected behavior

Current behavior:

RangeError: Maximum call stack size exceeded
    at nO (/private/tmp/nextjs/nextjs-app-router-examples/.yarn/__virtual__/next-virtual-d2b131c8dd/5/Users/daniel/.yarn/berry/cache/next-npm-14.0.3-canary.9-630a1159b2-10c0.zip/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:60707)
    at nR (/private/tmp/nextjs/nextjs-app-router-examples/.yarn/__virtual__/next-virtual-d2b131c8dd/5/Users/daniel/.yarn/berry/cache/next-npm-14.0.3-canary.9-630a1159b2-10c0.zip/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:45694)
    at nT (/private/tmp/nextjs/nextjs-app-router-examples/.yarn/__virtual__/next-virtual-d2b131c8dd/5/Users/daniel/.yarn/berry/cache/next-npm-14.0.3-canary.9-630a1159b2-10c0.zip/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:46289)
    at nO (/private/tmp/nextjs/nextjs-app-router-examples/.yarn/__virtual__/next-virtual-d2b131c8dd/5/Users/daniel/.yarn/berry/cache/next-npm-14.0.3-canary.9-630a1159b2-10c0.zip/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:62795)
    at nR (/private/tmp/nextjs/nextjs-app-router-examples/.yarn/__virtual__/next-virtual-d2b131c8dd/5/Users/daniel/.yarn/berry/cache/next-npm-14.0.3-canary.9-630a1159b2-10c0.zip/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:45694)
    at nT (/private/tmp/nextjs/nextjs-app-router-examples/.yarn/__virtual__/next-virtual-d2b131c8dd/5/Users/daniel/.yarn/berry/cache/next-npm-14.0.3-canary.9-630a1159b2-10c0.zip/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:46289)
    at nO (/private/tmp/nextjs/nextjs-app-router-examples/.yarn/__virtual__/next-virtual-d2b131c8dd/5/Users/daniel/.yarn/berry/cache/next-npm-14.0.3-canary.9-630a1159b2-10c0.zip/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:62795)
    at nR (/private/tmp/nextjs/nextjs-app-router-examples/.yarn/__virtual__/next-virtual-d2b131c8dd/5/Users/daniel/.yarn/berry/cache/next-npm-14.0.3-canary.9-630a1159b2-10c0.zip/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:45694)
    at nT (/private/tmp/nextjs/nextjs-app-router-examples/.yarn/__virtual__/next-virtual-d2b131c8dd/5/Users/daniel/.yarn/berry/cache/next-npm-14.0.3-canary.9-630a1159b2-10c0.zip/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:46289)
    at nO (/private/tmp/nextjs/nextjs-app-router-examples/.yarn/__virtual__/next-virtual-d2b131c8dd/5/Users/daniel/.yarn/berry/cache/next-npm-14.0.3-canary.9-630a1159b2-10c0.zip/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:62795)

Expected behavior:

Error: Component nesting too deep, might exceed maximum call stack size soon. This usually happens if you have a component that recurses infinitely. Top of component stack:
SearchPage: app/components/SearchPage.tsx:4
SearchPage: app/components/SearchPage.tsx:4
SearchPage: app/components/SearchPage.tsx:4
SearchPage: app/components/SearchPage.tsx:4
SearchPage: app/components/SearchPage.tsx:4
SearchPage: app/components/SearchPage.tsx:4
SearchPage: app/components/SearchPage.tsx:4
SearchPage: app/components/SearchPage.tsx:4

Verify canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:43 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6000
Binaries:
  Node: 20.5.0
  npm: 9.8.0
  Yarn: 4.0.1
  pnpm: 8.7.5
Relevant Packages:
  next: 14.0.3-canary.9
  eslint-config-next: 14.0.2
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.2.2
Next.js Config:
  output: N/A

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

Not sure

Additional context

I wasted an embarrassingly long time on stepping through react internals for this (the function in the stack trace of the error that infinite loops' source code is here https://github.com/facebook/react/blob/593ecee66a609d4a4c2b36b39b1e5e23b2456dd1/packages/react-server/src/ReactFizzServer.js#L2168) when it's actually my fault. The reason this happened is because I forgot an import here: https://github.com/danieltroger/react-server-components-stuff/blob/dt/next_infinite_loop/app/components/SearchPage.tsx (it should be import { SearchPage } from "@depict-ai/react-ui")

BUT the reason I'm reporting this as a bug is because my feeling is that next/react has the philosophy of displaying user friendly error messages when doing something wrong (I partly blame that I introduced this on running into 5:16 Error: Component definition is missing display name react/display-name so I called the function something without thinking about it). And I think this error message is the absolute opposite of user friendly. It leaves someone encountering this two options: debugging react, which leads to nothing because the variables in scope where it infinite loops aren't helpful or just changing random things in ones app until one discovers the infinite loop.

Both are not optimal. I'd love to see an improved error message, as detailed in the Expected behavior section

danieltroger commented 7 months ago

@Kayeddy not sure how your comment relates to this issue? There's no mongodb or JSON.stringify here

shawnesquivel commented 7 months ago

Was running into the same issues:

Now successfully working with:

Doesn't work if you don't have sharp installed for some reason, even though I found conflicting information from several Github issues.

Kayeddy commented 7 months ago

@Kayeddy not sure how your comment relates to this issue? There's no mongodb or JSON.stringify here

My apologies for the oversight, I kind of confused the context of the issue with another issue I saw on Stack Overflow.

Kind regards.

SujithPriyamRajan commented 2 months ago

I'm getting a similar kind of error. Don't know what's the issue. Screenshot 2024-07-29 151848

danieltroger commented 2 months ago

@SujithPriyamRajan you're probably calling a component recursively, maybe the error message I detailed under "Expected behavior" helps explaining it?

SujithPriyamRajan commented 2 months ago

@SujithPriyamRajan you're probably calling a component recursively, maybe the error message I detailed under "Expected behavior" helps explaining it?

I’m curious about this issue. Everything was working fine when using the client-side API, but since I moved the API to the server side, I’ve been facing problems with downloading blobs and creating window.URL.createObjectURL(blob)

"use client"

 const response = await api.viewDownload.download(path, FileTypes.CSV, details?.applicationName);
                const blobType = Object.getOwnPropertyDescriptor(response.headers, 'content-type')?.value;
                const blob = new Blob([response.data], {
                    type: blobType
                });

                const url = window.URL.createObjectURL(blob);

"use server"

 const response = await axios({
            url: fetchUrl.startsWith(config.REACT_APP_API_ENDPOINT) ? fetchUrl : '',
            method: options?.method,
            headers: options?.method?.toLowerCase() === 'get' ? { Accept: 'application/json' } : headers,
            data: options?.body
        });

        return options?.responseType === ResponseTypes.BLOB ? response : response.data.detail;

I have tried with https://github.com/vercel/next.js/issues/58494#issuecomment-1942427098

My current version is "next": "14.2.3"