Skip to content

fix: ISR 404 pages should be cached ephemerally #2233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 27, 2023
Merged

Conversation

orinokai
Copy link
Contributor

@orinokai orinokai commented Jul 26, 2023

Description

In Next.js it's possible to make an ISR 404 page that revalidates after a period of time.

A 404 page will be served in a number of scenarios, including (but not limited to):

  • a non-existing route, e.g. /blahblah
  • an SSR route that returns notFound: true
  • a non-prerendered path on a dynamic route with fallback: false
    • e.g. /posts/3 on the dynamic route /posts/[id] with id 1 and 2 prerendered and fallback: false
  • a path on a dynamic route that returns notFound: true
    • e.g. /posts/4 on the dynamic route /posts/[id] where id 4 matches a condition that returns notFound: true

Currently, on Netlify, we have inconsistent behavior in these different scenarios. An ISR 404 page will behave like an SSR page in the first 2 scenarios, a static page in the 3rd scenario and an ISR page without a TTL in the last scenario (which is particularly problematic because the notFound logic might change due to some external state changing (e.g. CMS content), but the 404 will remain indefinitely and the route will never be revalidated).

These behaviors occur for the following reasons:

  1. currently we can only revalidate a route that is detected as ISR at build time, therefore a non-existing route cannot be routed to the ODB handler
  2. dynamic routes with fallback: false are only intended to serve prerendered paths on the route, therefore we currently redirect the entire route to the statically rendered 404 page on the CDN (relying on the CDN to serve the prerendered HTML pages instead of following the redirect)
  3. dynamic routes that return notFound: true will serve an ISR 404 page, but Next.js does not use stale-while-revalidate headers in this case, unlike other ISR content, and this prevents us from identifying the response as ISR and extracting the TTL

This PR adds a special case to the TTL logic and assumes that if the route is an ODB route and the status code is 404, then we should only cache the response ephemerally for 60s. It is a stopgap solution designed to limit the impact of point 3 above. With the PR, an ISR 404 will no longer be cached indefinitely, however it will still not respect that actual TTL specified in the revalidate value. This PR does not address points 1 and 2 above, which will be tackled as part of forthcoming changes to the caching implementation.

Tests

Tests added

@netlify
Copy link

netlify bot commented Jul 26, 2023

Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!

Name Link
🔨 Latest commit c57e831
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/64c2627adf9ebc00083c4e97
😎 Deploy Preview https://deploy-preview-2233--netlify-plugin-nextjs-nx-monorepo-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Jul 26, 2023

Deploy Preview for netlify-plugin-nextjs-export-demo ready!

Name Link
🔨 Latest commit c57e831
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/64c2627a983ab200080c604e
😎 Deploy Preview https://deploy-preview-2233--netlify-plugin-nextjs-export-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions github-actions bot added the type: bug code to address defects in shipped code label Jul 26, 2023
@netlify
Copy link

netlify bot commented Jul 26, 2023

Deploy Preview for netlify-plugin-nextjs-next-auth-demo ready!

Name Link
🔨 Latest commit c57e831
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/64c2627abaef2300087b3c79
😎 Deploy Preview https://deploy-preview-2233--netlify-plugin-nextjs-next-auth-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Jul 26, 2023

Deploy Preview for netlify-plugin-nextjs-demo ready!

Name Link
🔨 Latest commit c57e831
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/64c2627af310c60008726381
😎 Deploy Preview https://deploy-preview-2233--netlify-plugin-nextjs-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Jul 26, 2023

Deploy Preview for nextjs-plugin-custom-routes-demo ready!

Name Link
🔨 Latest commit c57e831
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/64c2627afb79a30008423cd4
😎 Deploy Preview https://deploy-preview-2233--nextjs-plugin-custom-routes-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Jul 26, 2023

Deploy Preview for next-plugin-edge-middleware ready!

Name Link
🔨 Latest commit c57e831
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/64c2627a5f2a9900093ebbc8
😎 Deploy Preview https://deploy-preview-2233--next-plugin-edge-middleware.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Jul 26, 2023

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit c57e831
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/64c2627a23b06e0008cb6da1
😎 Deploy Preview https://deploy-preview-2233--next-plugin-canary.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Jul 26, 2023

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit c57e831
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/64c2627af3e75b00071c612a
😎 Deploy Preview https://deploy-preview-2233--next-i18next-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Jul 26, 2023

Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!

Name Link
🔨 Latest commit c57e831
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/64c2627ae2bd9f00080af355
😎 Deploy Preview https://deploy-preview-2233--netlify-plugin-nextjs-static-root-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

MarcL
MarcL previously approved these changes Jul 27, 2023
Copy link
Contributor

@MarcL MarcL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a solid quick fix until we have proper cache headers. Thanks!

@orinokai orinokai marked this pull request as ready for review July 27, 2023 12:01
@orinokai orinokai requested a review from a team as a code owner July 27, 2023 12:01
Copy link
Contributor

@MarcL MarcL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit: Looks good to me. Thanks!

@kodiakhq kodiakhq bot merged commit d7aad8a into main Jul 27, 2023
@kodiakhq kodiakhq bot deleted the rs/isr-404-fix branch July 27, 2023 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants