next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Fallback rewrites do not trigger after calling `notFound` function in dynamic route

Open jplwood opened this issue 4 months ago • 0 comments

Link to the code that reproduces this issue

https://github.com/jplwood/nextjs-rewrite-notfound-repro

To Reproduce

  1. start the application with npm run dev

Demonstrate the fallback rewrite:

  1. go to home route https://localhost:3000/home 👉 page loads ✅
  2. go to random route under home https://localhost:3000/home/rewriteworks 👉 rewrite to google is triggered ✅

Demonstrate fallback not working

  1. go to dynamic route https://localhost:3000/exists 👉 page loads ✅
  2. go to dynamic route again to trigger not found https://localhost:3000/rewritesad 👉 page rewrite is not triggered, 404 page is shown ❌

Current vs. Expected behavior

Per rewrite docs:

  1. fallback rewrites are checked/applied, these are applied before rendering the 404 page and after dynamic routes/all static assets have been checked. source

To me this means, if a route is not found, if there is a fallback rewrite, that should always apply, no matter how that not found is initiated. i.e.,

  • Implicit 404 ✅ - As shown in the first example, if the next router resolves a 404, the rewrite applies.
  • explicit 404 ❌ - In the second example, if we're in a dynamic route, but, per whatever lookup logic we do (in my case, api call to a headless CMS), if the dynamic path isn't resolvable and you call notFound(), this bypasses the fallback rewrite and goes straight to the 404 page.

Similarly, this seems like a topic to potentially mention in the Dynamic Routes doc page?—how to handle dynamic routes that are not found (i.e., notFound(), rewrites, redirects, etc...)

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 22.3.0: Mon Jan 30 20:39:46 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T6020
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 20.10.0
  npm: 10.3.0
  Yarn: 1.22.22
  pnpm: 9.12.1
Relevant Packages:
  next: 15.0.2-canary.6 // Latest available version is detected (15.0.2-canary.6).
  eslint-config-next: N/A
  react: 19.0.0-rc-1631855f-20241023
  react-dom: 19.0.0-rc-1631855f-20241023
  typescript: 5.3.3
Next.js Config:
  output: N/A

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

Runtime

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

next dev (local), next build (local), next start (local)

Additional context

I tested my reproduction in Next 15.x and 13.x w/ App Router, both have the issue. I suspect this is just an app router issue.

jplwood avatar Oct 24 '24 22:10 jplwood