Revalidation of cache is an important aspect of server-side rendering and is a key part of the Next.js caching strategy.
We are utilizing DatoCMS Cache Tags to revalidate GQL queries on demand, when relevant records are updated in the CMS.
When an update is made, a webhook is triggered and sends a request to the website’s API to revalidate the cache. The API routes are defined in each project, under /api/revalidate (see Forge for example).
Each new project needs to configured with the correct endpoints after the fork of Forge has been done.
Updating the webhook
- First make sure the project
package.jsonnamefield has been updated to the new project name. - Go to the project’s DatoCMS dashboard.
- Go to the project’s settings.
- Go to the webhooks section.
- Open or create the “Invalidate cache tags” webhook.
- Set the URL to the new project’s API route (
example.com/api/revalidate/cache-tags, probably avercel.appurl before prod deployment) - Add a new “Custom header”
- Set key to
Webhook-Tokenand set the value to the same value as theWEBHOOK_TOKENin the.env.localfile and Vercel environment. ℹ️ TheWEBHOOK_TOKENshould be generated via 1password if the project does not yet have one. - Save the changes.
🔂 Repeat this procedure for any staging environments.
Circumventing Vercel deployment protection
Most projects will have deployment protection enabled, usually in the form of a password. This can be circumvented via a custom header:
- Go to Vercel Project Settings -> Deployment Protection
- Under “Protection Bypass for Automation” add a secret key, generate one via 1password and make sure it is shared.
- Go back to Dato and add the
x-vercel-protection-bypassheader in the webhook request with the generated secret key. - Save the changes.