Skip to content

fix: correctly handle ISR for appDir pages #1855

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 35 commits into from
Jan 13, 2023
Merged

fix: correctly handle ISR for appDir pages #1855

merged 35 commits into from
Jan 13, 2023

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Dec 22, 2022

Summary

Data routes are handled differently for appDir pages. Instead of a JSON file in /_next/data, the request is made to the same path, passing the RSC header. This returns the RSC data to stream the update. The fact this varies on a header means we can't use simple ODB handling for these pages, because it will lead to errors like #1844, where the RSC data is returned when HTML is requested, or vice versa.

This PR fixes this by adding an edge function which routes appDir requests that include the rsc header to the appropriate .rsc path instead. The original request path is put into a header, which then replaces the .rsc path in the handler. These are real files but not normally requested directly. The reason for doing this is so that these requests have unique URLs, meaning they're cached separately as ODBs.

There's a final extra part that is needed, which is to handle trailing slashes. If we normalise all of the requests to the matching .rsc file, then there will be no difference to requests with and without the trailing slash. This is a problem, because depending on whether the version with or without the slash is requested first, we may cache the redirect instead of the actual data (or vice versa). To handle this, we do a bit of trickery. If the request has a trailing slash, we include a trailing slash on the rewritten .rscpath. For example, while an rsc request to /blog will be rewritten to /blog.rsc, a request for /blog/ (with trailing slash) will be rewritten to /blog.rsc/. This means that it will be treated as a separate ODB path, and will be cached differently.

Basically, all of this is to implement Vary in ODBs. 🤷🏻

Test plan

  1. Visit an appDir page on the default demo site. Navigate around, checking in the network panel that SPA data requests are correctly returning application/octet-stream RSC data.
  2. Load any of the /blog pages directly and ensure that HTML is returned
  3. Try loading them with and without the trailing slash and see if they redirect correctly.

Relevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal

Fixes #1844

capy wants an apple

Standard checks:

  • Check the Deploy Preview's Demo site for your PR's functionality
  • Add docs when necessary

🧪 Once merged, make sure to update the version if needed and that it was published correctly.

@netlify
Copy link

netlify bot commented Dec 22, 2022

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

Name Link
🔨 Latest commit 82ff851
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/63c166fd35f53a00098293bf
😎 Deploy Preview https://deploy-preview-1855--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 settings.

@netlify
Copy link

netlify bot commented Dec 22, 2022

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

Name Link
🔨 Latest commit 82ff851
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/63c166fd4cc02f0008239b53
😎 Deploy Preview https://deploy-preview-1855--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 settings.

@netlify
Copy link

netlify bot commented Dec 22, 2022

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

Name Link
🔨 Latest commit 82ff851
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/63c166fde85b3f0008b7fe94
😎 Deploy Preview https://deploy-preview-1855--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 settings.

@github-actions github-actions bot added the type: bug code to address defects in shipped code label Dec 22, 2022
@netlify
Copy link

netlify bot commented Dec 22, 2022

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

Name Link
🔨 Latest commit 82ff851
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/63c166fdd960ee000923786f
😎 Deploy Preview https://deploy-preview-1855--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 settings.

@netlify
Copy link

netlify bot commented Dec 22, 2022

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

Name Link
🔨 Latest commit 82ff851
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/63c166fd07160d00090518a2
😎 Deploy Preview https://deploy-preview-1855--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 settings.

@netlify
Copy link

netlify bot commented Dec 22, 2022

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

Name Link
🔨 Latest commit 82ff851
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/63c166fdd1936900098af31b
😎 Deploy Preview https://deploy-preview-1855--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 settings.

@netlify
Copy link

netlify bot commented Dec 22, 2022

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit 82ff851
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/63c166fda17cc10009a7c179
😎 Deploy Preview https://deploy-preview-1855--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 settings.

@netlify
Copy link

netlify bot commented Dec 22, 2022

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

Name Link
🔨 Latest commit 82ff851
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/63c166fd35f53a00098293c4
😎 Deploy Preview https://deploy-preview-1855--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 settings.

@netlify
Copy link

netlify bot commented Dec 22, 2022

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit 82ff851
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/63c166fdcd632800086bf6fc
😎 Deploy Preview https://deploy-preview-1855--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 settings.

@nadecancode
Copy link

When is this getting merged?

@ascorbic ascorbic force-pushed the mk/rsc-no-isr branch 2 times, most recently from f5d1014 to 884d3be Compare January 3, 2023 11:26
@ascorbic ascorbic marked this pull request as ready for review January 3, 2023 13:32
@ascorbic ascorbic requested a review from a team January 3, 2023 13:32
@@ -15,32 +15,19 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
node-version: [14, '*']
Copy link
Contributor Author

Choose a reason for hiding this comment

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

AppDir requires 16+

})

// This needs trailing slash handling to be fixed
it.skip('correctly redirects HTML requests for static pages', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prerendered pages do the normal Netlify trailing slash handling. This shoudl work once we have the trialing slash edge funciton

to: ODB_FUNCTION_PATH,
force: true,
buildId,
dataRoute: isAppDir ? dataRoute : null,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

appDir routes provide their own data route value

@ascorbic ascorbic changed the title fix: don't use ISR for appDir pages fix: correctly handle ISR for appDir pages Jan 13, 2023
@orinokai
Copy link
Contributor

@ascorbic is there a reason we can't just not normalise the request? so a request to /blog will be rewritten to /blog.rsc and a request to /blog/ will be rewritten to /blog.rsc/

it would make it easier to reason about when looking at logs, etc. but let me know if i've misunderstood something

@ascorbic
Copy link
Contributor Author

I tried that at first, and it didn't work. I can't for the life of me rememebr why! Let me see if I can reproduce it. You're right it would be simpler. I'm now wondering if other changes I made sinc ethen will make it work.

@ascorbic
Copy link
Contributor Author

@orinokai OK, so it didn't work right away, but with a few changes it does! Good suggestion. I've updated the description too.

Copy link
Contributor

@orinokai orinokai left a comment

Choose a reason for hiding this comment

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

Amazing! Nice one for sticking with it. 🚢

route: string
dataRoute: string
withData: boolean
}) => [...(withData ? toNetlifyRoute(dataRoute) : []), ...toNetlifyRoute(route)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Will be so good to replace all these helper functions with edge router!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

💯 The number of times I've thought that while working on this!

@gpoole
Copy link

gpoole commented Dec 5, 2023

@ascorbic the issue described in #1844 and fixed by this PR unfortunately seems to have surfaced again as #2089. It's happening with auto-detected version of the runtime with Next 13.5.6 (latest released v13 as of now). Could it be the same underlying problem and, if so, potentially a regression?

@ascorbic
Copy link
Contributor Author

ascorbic commented Dec 5, 2023

@gpoole yes, this is related to internal changes in Next.js. We're working on some major changes that will fix this and many more issues. Beta will be available soon.

@gpoole
Copy link

gpoole commented Dec 6, 2023 via email

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.

[Bug]: appDir pages not varying cache on RSC header
4 participants