vercel / next.js

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

Dynamic routes cache 404 pages with `dynamic = 'force-static'` and calling `notfound()` #63483

Open Javad9s opened 7 months ago

Javad9s commented 7 months ago

Link to the code that reproduces this issue

https://github.com/Javad9s/nextjs-dynamic-params-cached-notfound/

To Reproduce

  1. Build and start the application (npm run build , npm start)
  2. Open http://localhost:3000/params/aa => calls notfound(), also caches the result
  3. Refresh the page => returns the same result
  4. Open http://localhost:3000/params/bb => calls notfound(), caches a new page
  5. Open http://localhost:3000/params/cc => calls notfound(), caches a new page
  6. Open ...

This way there is no control over memory and storage usage. Can cause problems in large scale applications.

Current vs. Expected behavior

Current : caches every notfound() call Expected : not cache any notfound() call

Provide environment information

Binaries:
  Node: 20.9.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.2.0-canary.31 // Latest available version is detected (14.2.0-canary.31).
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.2
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 start (local)

Additional context

Using generateStaticParams() also opts the page into static generation and is equivalent to setting dynamic = 'force-static' in the context of this issue

coffeecupjapan commented 7 months ago

@Javad9s

I don't know what you mean for

cause problems in large scale applications.

but does it mean that files cache (or redis cache) under .next/server/app/params becomes too big or something else ?

If you mean for file cache (or redis cache) becomes too big , it seems like adding code below

if (isNotFoundError(response.err?)) {
  metadata.isNotFound = "NEXT_NOT_FOUND"
}

under

https://github.com/vercel/next.js/blob/33f8428f7066bf8b2ec61f025427ceb2a54c4bdf/packages/next/src/server/app-render/app-render.tsx#L1342

fix the problem (.rsc .html .meta format files are not added under .next/server/app/params) , but original code here

https://github.com/vercel/next.js/blob/33f8428f7066bf8b2ec61f025427ceb2a54c4bdf/packages/next/src/server/base-server.ts#L2409-L2412

is intended to be used under environment of pages-router, not app-router, so I don't have confidence.

You can ask any Next.js core team member about this issue I guess.