I have created a nextJS project with 366 pages where each page is statically generated using getStaticProps. Each page receives new content twice a day and I am using on-demand revalidation to update the cache for each single page. The revalidation api call takes around 5 seconds to run, but on refreshing the actual pages it sometimes takes hours before it is updated. Any thoughts as to why this is happening?
My revalidation code looks like this:
import type { NextApiHandler } from 'next';
import pages from './pages.json';
const secret = process.env.REVALIDATE_SECRET || '';
const handler: NextApiHandler<Data> = async (req, res) => {
if (req.method !== 'POST') {
return res.status(401).json({ message: 'Must be a POST request' });
}
if (req.headers.signature !== secret) res.status(401).json({ message: 'Invalid signature' });
try {
const revalidateResult = await Promise.allSettled([
res.revalidate('/'),
...pages.map(({ slug }) => res.revalidate(`/${slug}`)),
]).then(
(results) => results.filter(({status}) => status === 'rejected'),
);
if (revalidateResult.length === 0) {
return res.status(200).send({message: 'Successfully revalidated all pages'})
}
return res.status(500).send({message: `Error: ${revalidateResult.length} / ${pages.length} pages were rejected upon revalidation`})
} catch (err) {
return res.status(500).send({ message: `Error revalidating: ${err}` });
}
};
export default handler;
Pages is a list of objects. The objects contain slug among other things:
// pages.json
[
{
'slug': 'slug1',
...
}, {
'slug': 'slug2',
...
}
]
I expected this to successfully update all 366 pages instantly, but it takes more than an hour before the new content is visible upon refresh. It happens regularly that one or two of the pages fail to revalidate where the api for instance returns: Error: 1 / 366 pages were rejected upon revalidation
. Not sure yet why that is happening, but I would expect the remaining 365 pages to be updated immediately anyways.