Skip to content
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

Statically Generated Sites & webhook-based on-demand page revalidation #105

Merged
merged 21 commits into from Mar 9, 2022

Conversation

j0nas
Copy link
Contributor

@j0nas j0nas commented Mar 8, 2022

Statically Generated Sites & webhook-based on-demand page revalidation

Intent

Description

Instead of constructing Pages on-the-fly for every user, we fetch all Routes and their associated pages, and generate them at build-time.

I've set up two webhooks in the Sanity project's settings. The enabled webhook pings the /api/revalidate endpoint of this branch's latest Preview Deploy whenever anything in Studio is changed. When the endpoint is called, we have to find the Routes associated with the updated content and get the slug.current of these Routes. Then the magic happens; we call unstable_revalidate on each slug, which rebuilds the relevant Routes.

This setup seems perfect for Sanity. The addition of unstable_revalidate in Next.js v12.1 could not have been better timed. I'm quite happy with how smoothly this setup seems to work, the performance benefits are massive when compared to every user having to wait for every query on every page load and navigation.

(The disabled webhook is to be enabled when this PR is merged.)

Article pages will be handled in a separate follow-up PR once the approach taking in this PR is approved.

Testing this PR

When testing this PR, make sure that you're on the Preview Deploy that's got the branch name, and not the latest shorthash; these are not the same, and I wasted some time being puzzled at "suddenly-broken webhooks" before I realized this. 😅

(/sections-testing and /shared-sections are useful Routes for testing)

  1. Open the Realtime Logs for the latest Preview Deploy for this PR, and select "api/revalidate" in the Function dropdown
  2. Open Studio and change the following elements:
    • A sharedSection referenced by a Page
    • The contents of a Page
    • The slug and metadata of a Route
  3. For each of the changes above, verify that:
    • a [POST] /api/revalidate request is logged in the Realtime Logs
    • that you get output indicating success is logged the Realtime Logs, e.g. [revalidate] Updated routes: shared-sections
    • That the Route referencing the updated element renders the updated content on refresh (this is where using the correct Preview Deploy URL is important!)

@vercel
Copy link

vercel bot commented Mar 8, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployments, click below or on the icon next to each commit.

structured-content-2022-web – ./web

🔍 Inspect: https://vercel.com/sanity-io/structured-content-2022-web/HFwRMZuMefzjiSGRngNoFa7Xmxfq
✅ Preview: https://structured-content-2022-web-git-next-ssg.sanity.build

structured-content-2022-studio – ./studio

🔍 Inspect: https://vercel.com/sanity-io/structured-content-2022-studio/46Phw4sampMxQ8sSEZLfzoxQXQmP
✅ Preview: https://structured-content-2022-studio-git-next-ssg.sanity.build

Copy link
Collaborator

@oyvind-stenhaug oyvind-stenhaug left a comment

Choose a reason for hiding this comment

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

Nice to see the return of static generation. It took a while before I realized that this was no longer used once [[...slug]].tsx replaced the individual pages (about.tsx etc), but at some point it dawned on me that the Lighthouse scores were quite a bit lower than before.

(Also, a nice side-effect of getting an upgraded Next.js is that they fixed their <meta charset> placement. Previously there were all sorts of other tags before it, but to be valid HTML it's really supposed to be located within the first 1024 bytes of the document.)

Mainly I'm just wondering if articles have been forgotten here or if it's outside of scope/there will be a separate PR for it. But I also added some other minor comments.

How will this work with other PR branches in the future if we need to test with some updated content? Is a manual redeploy in Vercel the way to go about it then? Could it be worth putting the answer in the web README?

web/pages/[[...slug]].tsx Show resolved Hide resolved
web/pages/[[...slug]].tsx Outdated Show resolved Hide resolved
web/pages/[[...slug]].tsx Outdated Show resolved Hide resolved
web/pages/[[...slug]].tsx Outdated Show resolved Hide resolved
web/pages/api/revalidate.ts Outdated Show resolved Hide resolved
web/pages/api/revalidate.ts Outdated Show resolved Hide resolved
@j0nas j0nas merged commit 3b1d979 into main Mar 9, 2022
@j0nas j0nas mentioned this pull request Mar 9, 2022
kodiakhq bot pushed a commit to vercel/next.js that referenced this pull request Mar 10, 2022
Documents how to setup GROQ webhooks that revalidates ISR routes on-demand.
It's trivial to do this for posts as you only need to lookup the `slug` based on the `id` you receive in the webhook payload.
When editing authors the GROQ query demonstrates how you can do a reverse lookup (find all pages that reference the edited author) then invalidate their routes.

This work is based on sanity-io/structured-content-2022#105, thanks @j0nas! 🎉 

[Test **Deploy with Vercel**](https://vercel.com/new/git/external?repository-url=https://github.com/stipsan/next.js/tree/add-on-demand-ISR-to-cms-sanity/examples/cms-sanity&project-name=cms-sanity&repository-name=cms-sanity&env=NEXT_PUBLIC_SANITY_PROJECT_ID,SANITY_API_TOKEN,SANITY_PREVIEW_SECRET,SANITY_STUDIO_REVALIDATE_SECRET&envDescription=Required%20to%20connect%20the%20app%20with%20Sanity&envLink=https://vercel.link/cms-sanity-env)

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`



Co-authored-by: Lee Robinson <9113740+leerob@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants