Hi guys,
I am trying to implement on-demand incremental static regeneration into my site.
I have followed the article here.
I created a PRISMIC_WEBHOOK_SECRET in my env.local as well as on my Netlify account.
I made a revalidate.js in my api folder with the code below:
// pages/api/revalidate.js
import * as prismic from '@prismicio/client';
import * as prismicH from '@prismicio/helpers';
// Import your app's Link Resolver (if your app uses one)
import { linkResolver, repositoryName } from '../../prismicio';
/**
* This API endpoint will be called by a Prismic webhook. The webhook
* will send an object containing a list of added, updated, or deleted
* documents. Pages for those documents will be rebuilt.
*
* The Prismic webhook must send the correct secret.
*/
export default async function handler(req, res) {
if (req.body.type === 'api-update' && req.body.documents.length > 0) {
// Check for secret to confirm this is a valid request
if (req.body.secret !== process.env.PRISMIC_WEBHOOK_SECRET) {
return res.status(401).json({ message: 'Invalid token' });
}
// If you have a `createClient()` function defined elsewhere in
// your app, use that instead
const client = prismic.createClient(repositoryName);
// Get a list of URLs for any new, updated, or deleted documents
const documents = await client.getAllByIDs(req.body.documents);
const urls = documents.map(doc => prismicH.asLink(doc, linkResolver));
try {
// Revalidate the URLs for those documents
await Promise.all(
urls.map(async url => await res.unstable_revalidate(url))
);
return res.json({ revalidated: true });
} catch (err) {
// If there was an error, Next.js will continue to show
// the last successfully generated page
return res.status(500).send('Error revalidating');
}
}
// If the request's body is unknown, tell the requester
return res.status(400).json({ message: 'Invalid body' });
}
I then went into my Prismic settings and created a webhook that pointed to the API endpoint with my secret.
It is triggering and I am getting a 200 response when I update one of my pages but the page does not update.
Any ideas what I am doing wrong?