next.js: [NEXT-734] Revalidated page with notFound true does not return 404 Page

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

npx --no-install next info

    Operating System:
      Platform: linux
      Arch: x64
      Version: #32-Ubuntu SMP PREEMPT_DYNAMIC Fri Jan 20 15:20:08 UTC 2023
    Binaries:
      Node: 16.19.0
      npm: 8.19.3
      Yarn: 1.22.19
      pnpm: N/A
    Relevant packages:
      next: 13.2.2-canary.3
      eslint-config-next: 13.2.1
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue

https://github.com/floreq/200-to-404

To Reproduce

  1. Install project:
npm install
  1. Build and run production application:
npm run build && npm run start
  1. Go to http://localhost:3000/blog/1 page.
  2. Make some page refreshes.
  3. Spot that the page never become 404 Page when minutes are even.
    • In this example, when minutes are even is set notFound: true for Post Page.

Describe the Bug

When a page was correctly generated. Than when getStaticProps returns notFound: true for that page. The page is not removed from .next/server/pages. The page is still visible.

In the code to reproduce issue, when the page is revalidated with notFound: true on even minutes. x-nextjs-cache is set to STALE and stays that way. Even if refreshing the page.

  • x-nextjs-cache changes only to HIT when the page is revalidated without notFound: true.

In next.config.js is set isrMemoryCacheSize: 0 to highlight this problem.

//next.config.js
module.exports = {
  [...]
  experimental: {
    isrMemoryCacheSize: 0,
  },
};

Image 1

  • correctly loaded page, x-nextjs-cache: HIT, odd minutes

image

Image 2

  • incorrectly loaded page, notFound: true, x-nextjs-cache: STALE, even minutes image

Expected Behavior

When a page is successfully revalidated with notFound: true returned from getStaticProps:

  1. the page should display 404 Page, not itself,
  2. and the page should not be in .next/server/pages.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

next start

NEXT-734

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 4
  • Comments: 16 (2 by maintainers)

Most upvoted comments

Yes, it’s the same issue but not related to using middleware.

  1. Page created at build using getStaticPaths / getStaticProps (/products/x)
  2. Slug is changed in the database (x -> y)
  3. New slug is re-validated, creating the new page path successfully (/products/y)
  4. Old slug is re-validated, returning notFound: true (/products/x)
  5. New slug is served with the correct content at /products/y
  6. Old slug is served with the old version of the page at /products/x instead of a 404 not found

Seems like the file is not removed from the file cache and so is still served despite being an invalid path now.

Only affected when using ISR Memory Cache = 0, for self-hosted solutions.

@samcx

Also be aware that while setting ISR cache to 0 is a fast track to triggering this issue, the issue becomes more devious when you realize that the default memory of 50MB can be exhausted by simply having a large site.

This happened to us. 2000 content pages in our CMS and suddenly deleted pages were popping back up and was extremely confusing and disruptive.

@ci-scott Thank you for sharing! Is it possible to create a public, minimal reproduction repository that showcases this issue so we can take a closer look?

@samcx issue with was described by @ci-scott matches description from this thread. I think Link to the code that reproduces this issue will do.

Provided example:

  • doesn’t use middilware,
  • ISR Memory Cache is set to 0,
  • you can see old version of the page when returned notFound: true.

Is this the same as #37945? Sure seems to be the exact same.

Yup, looks the same to me too.

I upgraded to v13.2.4, and still experiencing this issue @timneutkens . I don’t have a middleware in my project, setup is ± same as the one from OP.

duplicate or related to https://github.com/vercel/next.js/issues/38239?

I have a similar issue with notFound and getting {} as result of that page.

Might as well past part of my comment here:

Repo: https://github.com/joggienl/nextjs-middleware-and-redirects Running “production” demo: https://nextjs-middleware-and-redirects.vercel.app/

Direct link to the page with getStaticProps and a notFound set for the specific id: https://nextjs-middleware-and-redirects.vercel.app/post/2