Configure Revalidate Route

Revalidate routes in Next.js


Implement on-demand revalidation using an API routes at /revalidate.

If you're using the Basic Starter, revalidate route are already created for you. You can skip this step.

/pages/api/revalidate.ts

pages/api/revalidate.ts

import { NextApiRequest, NextApiResponse } from "next"
export default async function handler(
request: NextApiRequest,
response: NextApiResponse
) {
let slug = request.query.slug as string
const secret = request.query.secret as string
// Validate secret.
if (secret !== process.env.DRUPAL_REVALIDATE_SECRET) {
return response.status(401).json({ message: "Invalid secret." })
}
// Validate slug.
if (!slug) {
return response.status(400).json({ message: "Invalid slug." })
}
try {
await response.revalidate(slug)
return response.json({})
} catch (error) {
return response.status(404).json({
message: error.message,
})
}
}