Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Strategies for revalidating pages after site-wide changes in Sanity discussed in Slack thread

3 replies
Last updated: May 25, 2023
Hi folks, I’m interested to hear what strategies are being used for revalidating pages when site-wide changes are necessary (eg an update to a
navigation
document in Sanity).
For example, I have a site with ~45 pages that are statically rendered and a global navigation schema in Sanity. When the navigation is updated I need to revalidate all pages to propagate the changes, I’m doing this with a webhook and API route:

// pages/api/revalidate.ts

const revalidateUrls = [
// list of URLs to revalidate, ~45 pages
'/', '/about', '/courses'
// ...
] 

  try {
    // Revalidate the URLs for affected documents
    let revalPromises: Promise<void>[] = [];

    for (const url of revalidateUrls) {
      const urlPath = url.startsWith('/') ? `${url}` : `/${url}`;
      const promise = res.revalidate(urlPath);
      revalPromises.push(promise);
      await promise;
    }

    await Promise.all(revalPromises);
    return res.status(200).json({ revalidated: true });
  } catch (err) {
    // If there was an error, Next.js will continue to show
    // the last successfully generated page
    console.log('Error revalidating: ', err);
    return res.status(500).send('Error revalidating');
  }
The issue I’m running into is that awaiting the promises to verify the revalidation is causing my Vercel function to time out.

One other solution I’m looking at is just triggering a full rebuild with a Sanity webhook (filtered to trigger on changes to the
navigation
document type), and a deploy hook in Vercel.
Does anyone have any other ideas?
May 25, 2023, 12:03 AM
I think a webhook is the simplest. In Next.js app directory menu could be it's own server component which fetches the data directly with it's own cache.
May 25, 2023, 1:42 PM
Hi
user H
, thanks for the reply. I’m thinking webhook/rebuild might be the best option too. Using a server component sounds like the ideal solution, unfortunately I’m not at a point where I can migrate the project to the App dir. Hopefully some time soon!
May 25, 2023, 8:41 PM
https://www.sanity.io/plugins/vercel-deploy This plugin offers quick solution too.
May 25, 2023, 9:49 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?