Skip to content

feat: refresh hooks api implementation #1950

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 48 commits into from
Mar 22, 2023
Merged

feat: refresh hooks api implementation #1950

merged 48 commits into from
Mar 22, 2023

Conversation

orinokai
Copy link
Contributor

@orinokai orinokai commented Mar 1, 2023

Summary

This PR incorporates the work of the refresh hooks PoC #1937 and integrates it with the new refresh hooks API endpoint to implement on-demand revalidation with the Next Runtime.

The approach is to subclass the Next.js Server with an implementation that overrides getRequestHandler() so that we can provide our own handler that checks for the existence of an x-prerender-revalidate header, which gets sent during a call to res.revalidate().

Once we detect the header we can handle the request by, first, allowing the original handler to complete (to use Next itself as the source of truth for error handling) and then calling our own netlifyRevalidate() function, which parses the prerender-manifest.json to retrieve all paths associated with a route (eg. the data route or RSC route), normalizes them (e.g. index routes and locales), computes any dynamic params, and passes them as the body of an API call to the refresh_on_demand_builders endpoint.

Errors specific to the Netlify revalidation are logged to the serverless function logs and then thrown in the same way as the Next.js handler does.

Test plan

  1. Open the deploy preview https://deploy-preview-1950--netlify-plugin-nextjs-demo.netlify.app/
  2. Visit the following ISR page (300s TTL) and observe the timestamp https://deploy-preview-1950--netlify-plugin-nextjs-demo.netlify.app/getStaticProps/with-revalidate/
  3. Hit the revalidate API route https://deploy-preview-1950--netlify-plugin-nextjs-demo.netlify.app/api/revalidate/
  4. Observe the timestamp has changed on the following page

UPDATE: Add a ?select={number} query to the api/revalidate route to test revalidation of other route types. Details here: https://github.com/netlify/next-runtime/pull/1950/files#diff-77d2214b5448bd28b99f8d9394345fc60030c657bd155778f461295932d54098

Unit and e2e tests are included.

Closes netlify/pod-ecosystem-frameworks#391

@netlify
Copy link

netlify bot commented Mar 1, 2023

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

Name Link
🔨 Latest commit fd32f38
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/641b135f1022e000081d2177
😎 Deploy Preview https://deploy-preview-1950--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 Mar 1, 2023

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

Name Link
🔨 Latest commit fd32f38
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/641b1360ef36160008f77889
😎 Deploy Preview https://deploy-preview-1950--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 Mar 1, 2023

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

Name Link
🔨 Latest commit fd32f38
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/641b136031c63500088ecdd3
😎 Deploy Preview https://deploy-preview-1950--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: feature code contributing to the implementation of a feature and/or user facing functionality label Mar 1, 2023
@netlify
Copy link

netlify bot commented Mar 1, 2023

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

Name Link
🔨 Latest commit fd32f38
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/641b136053df930008ea13ea
😎 Deploy Preview https://deploy-preview-1950--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 Mar 1, 2023

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit fd32f38
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/641b13609fedfa0008317693
😎 Deploy Preview https://deploy-preview-1950--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 Mar 1, 2023

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit fd32f38
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/641b13607c32dd00086c7188
😎 Deploy Preview https://deploy-preview-1950--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.

@netlify
Copy link

netlify bot commented Mar 1, 2023

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

Name Link
🔨 Latest commit fd32f38
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/641b13600fb9900008318582
😎 Deploy Preview https://deploy-preview-1950--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 Mar 1, 2023

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

Name Link
🔨 Latest commit fd32f38
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/641b13600f82df0008ede1f3
😎 Deploy Preview https://deploy-preview-1950--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 Mar 1, 2023

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

Name Link
🔨 Latest commit fd32f38
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/641b13603af66c0008f96b00
😎 Deploy Preview https://deploy-preview-1950--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.

@orinokai orinokai marked this pull request as ready for review March 21, 2023 15:10
@orinokai orinokai requested a review from a team March 21, 2023 15:10
@@ -47,6 +47,10 @@ export const generateFunctions = async (
await ensureDir(join(functionsDir, functionName))
await writeFile(join(functionsDir, functionName, `${functionName}.js`), apiHandlerSource)
await copyFile(bridgeFile, join(functionsDir, functionName, 'bridge.js'))
await copyFile(
join(__dirname, '..', '..', 'lib', 'templates', 'server.js'),
join(functionsDir, functionName, 'server.js'),

Choose a reason for hiding this comment

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

Can we get some comments on why we're copying this file? (This whole functions.ts file could use those tbh hah, but I won't put that on you)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, i totally agree! i don't know enough about some of the functions in there to comment it all, but i've added comments to generateFunctions() in fd32f38


// Normalize a data route to include the locale prefix and remove the index suffix
export const localizeDataRoute = (dataRoute: string, localizedRoute: string): string => {
if (dataRoute.endsWith('.rsc')) return dataRoute

Choose a reason for hiding this comment

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

.rsc should probably be a constant at this point

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i'm not certain about this - there's a bunch of strings in that function that we could make into constants - but they are unlikely to change and i think it helps readability to know what they are in that context, especially since specifics like whether they have a . or a / are important with that string wrangling

Choose a reason for hiding this comment

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

Fair!

Copy link

@sarahetter sarahetter left a comment

Choose a reason for hiding this comment

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

🎉 Yayy Rob!

@kodiakhq kodiakhq bot merged commit fb93b54 into main Mar 22, 2023
@kodiakhq kodiakhq bot deleted the rs/refresh-hooks-api branch March 22, 2023 15:35
@charkour
Copy link

This is so exciting! Thank you Netlify team!

@charkour
Copy link

I'm wondering how to update the Netlify Docs for Next.js On-Demand ISR. Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants