Revalidation is necessary for JAMStack Projects. It tells the server when the content is updated in the CMS. So users will see the latest content without you re-deploying the website each time.
This is a general instruction on integrating revalidation with Sanity CMS and Next.js. Make sure you make the necessary changes according to the features and frameworks.
Sanity CMS provides GROQ-powered Webhook feature which will be used to trigger our revalidation in our Next.js Project.
https://YOUR_SITE_URL.com/api/revalidate
You can also run npx sanity hook create
in your project terminal to create a new webhook in sanity.
Now, copy the secret you created earlier and add it to your `.env.local` file as well as vercel or production env using the name: SANITY_REVALIDATE_SECRET
To add the secret to vercel using the terminal, use the following command.
npx vercel env add SANITY_REVALIDATE_SECRET
To change which pages to update, open /api/revalidate.ts file and change the logic on what files to update. By default, it looks for the current /post/[slug]
path and home page and updates it when a change happens.
You can add or update the pages and the logic as needed for your project.
Once everything looks good, you can re-deploy the project to activate the Revalidation API using the Groq powered Webhook.
Use the following command for vercel deploy from terminal
npx vercel --prod