Reputation: 340
We have a Next.js site that relies heavily on ISR (incremental static regeneration) and uses webhooks to revalidate specific pages on-demand when their content has changed in our CMS.
However, we also want to keep some site-wide data in our CMS. Particularly we want our CMS to be the source of truth for our navigation header and for a global message banner that can show special notifications on occassion. (Ie, "We're closed today it's a special holiday")
If either of these "site-wide" things change in the CMS, we would need to revalidate every single path. Is there a way to do this without trying to keep track of every potential path ourselves and explicitly calling revalidate on them? It seems like the framework should help us here, but I can't find any mechanism that would revalidate everything at once.
Has anyone solved this or found good patterns for leveraging ISR with site-wide data like a navigation or global notification banner?
Thanks
Upvotes: 2
Views: 1063
Reputation: 1568
I believe that you can't revalidate all at the same time, but you can make an api endpoint which iterates over the pages directory, accessing its url and making a request to each page. I doubt this is good practice though.
async function revalidateAllPages() {
const pagesDirectory = path.join(process.cwd(), 'pages');
const pageFiles = await fs.readdir(pagesDirectory);
for (const file of pageFiles) {
if (file.endsWith('.js') && file !== 'revalidate-all.js') {
const pagePath = path.join(pagesDirectory, file);
const { revalidate } = await getStaticProps();
// Trigger revalidation for each page by accessing its URL
await fetch(`https://your-website.com/${file.replace('.js', '')}`);
}
}
}
Upvotes: 0