next.js: Using `notFound` in suspended interleaved server component breaks rendering if not-found page (or layout) renders a `next/link` - production-only - Minified React error #310

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/elastic-sound-fpmpdt

To Reproduce

  1. Build and run the project with pnpm build && pnpm start if it’s not already running.
  2. Open https://fpmpdt-3000.csb.app/en
  3. If no client-side exception shows up, hard-refresh until it does.
  4. Observe the Minified React error #310 (Rendered more hooks than during the previous render.) being logged in the browser’s console.

Note: The project has to be built. In dev-mode, no error occurs. It’s also not always the case.

Current vs. Expected behavior

I expect the not-found page inside /[locale] to be rendered without issue. However, the page rendering results in a React error.

Provide environment information

Original

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
  Available memory (MB): 4102
  Available CPU cores: 2
Binaries:
  Node: 20.9.0
  npm: 9.8.1
  Yarn: 1.22.19
  pnpm: 8.10.2
Relevant Packages:
  next: 14.2.0-canary.26 // Latest available version is detected (14.2.0-canary.26).
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

Also tested with

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Thu Jan 11 04:09:03 UTC 2024
  Available memory (MB): 15853
  Available CPU cores: 16
Binaries:
  Node: 18.20.1
  npm: 10.5.0
  Yarn: 1.22.22
  pnpm: 8.15.5
Relevant Packages:
  next: 14.3.0-canary.73 // Latest available version is detected (14.3.0-canary.73).
  eslint-config-next: N/A
  react: 19.0.0-beta-04b058868c-20240508
  react-dom: 19.0.0-beta-04b058868c-20240508
  typescript: 5.1.3
Next.js Config:
  output: N/A

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

App Router, Routing (next/router, next/navigation, next/link)

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

next start (local), Vercel (Deployed)

Additional context

First of all, the crash won’t happen 100% of the time, and only in production mode. This took a while to debug 😅

The stars need to align The crash stops happening, or happens less often, if one of the following is the case:

  • The [locale]/not-found.tsx page does not include a next/link <Link>.
  • The <Suspense> is removed.
  • The useRouter and its useEffect is commented out.
  • The data fetching in the server component is taking longer. E.g. 500ms instead of the 50ms configured. This points to a race condition.

Race condition Because of the race-conditiony nature of the bug, it could not be reproducible on some machines.

next/link links in general? It seems that any next/link link that will be rendered together with the not found page (e.g. those in the layout) triggers the bug. In a bigger project (where the issue first appeared), turning all the <Link> tags into <a> tags got rid of the issue.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 24
  • Comments: 25 (2 by maintainers)

Commits related to this issue

Most upvoted comments

It’s not a workaround. You won’t get similar behaviour at all.

Not using a feature doesn’t work around a bug of said feature. Imagine you open an issue because SCSS doesn’t work with a framework. “Just don’t use SCSS.” is not a workaround. Maybe “compile SCSS in a build-step instead of on-demand” could be.

Anyway, I’d like to keep on-topic and find a way to fix the issue - with something different from “just don’t use the feature”.

This is still an issue in Next 15.1.6. Using loading.tsx and redirect in page.tsx

Same issue for me, when I’m removing the loading.tsx, it works well. On my page, I have a redirect to a page that is using a loading.tsx

Does not work for nextjs 14.1.4 and 14.2.4

Same for me, was very surprised that such basic functionality cannot work together. And very upset when I found out about it in production, because this problem is not reproduced on the dev build. I’d appreciate at least some clue in the documentation that this all together won’t work. Since according to the documentation these are (next/link, notFound() and loading.tsx) all recommended things that you wouldn’t expect such problems with them.

As for me, it reproducing more consistently when hovering over links while the page is loading.

Just to chime in, also am seeing this in "next": "14.2.25".

Such an odd one. Will have to remove the loading.tsx files for now, until a better solution appears.

I’m having the same issue using next@14.1.4, but we are not using a not-found page or next/link on the layout. We have a next/link on the loading.tsx page, and then we are using a redirect, NOT a notFound and getting the same issue

Like @andremendonca, I use page streaming and I get this random error when I call notFound()

Disabling streaming (deleting loading.tsx file) fixes the issue

Same issue here, removing the loading.tsx file seems to fix the issue. This is a UX issue for us- will this be addressed in an upcoming release?

Like @andremendonca, I use page streaming and I get this random error when I call notFound()

Disabling streaming (deleting loading.tsx file) fixes the issue

Exactly what I had to do to “fix” the issue.