Using on-demand revalidation with Next JS and Sanity, how to revalidate all paths when a specific schema type changes.
Last updated: May 3, 2023
Hey all, I am trying to use on-demand revalidation with Next JS and Sanity.I have most of this setup so far, but I have one issue. My goal is to have the entire app revalidate when one schema type, navigation, is changed. Because they are schema for the nav links on all pages in the app. In the docs, it asks that you explicitly tell Next which routes to revalidate. Like so:
However, because I want to revalidate every path, I’m not sure what would be the best approach to take. If anyone has any ideas here, could you help me out?
May 3, 2023, 2:34 PM
I cannot account for whether they're good ideas but if that was my need or use case, I would try either having all the pages on a revalidation timer instead or, to more tightly track/mirror you content updates, trigger a redeploy (you might not be hosted at Vercel but most places that can run Next have a similar mechanism). Since it doesn't make your site experience any downtime, and just switches things out once it's complete, and absolutely will change all the paths, that might be the cleanest thing.
I don't believe there's a built-in mechanism to target multiple pages, or even pattern-match paths for this one feature.
May 3, 2023, 2:49 PM
That is really interesting about the Deploy Hook - thank you
May 3, 2023, 2:54 PM
It would be especially nice if I could trigger this to happen only when two specific field changes, slug or title
May 3, 2023, 2:59 PM
One thing to think about as well, if it's a possible alternative and to your tastes, is rendering a nav conventionally by default, and then when 'hydrating' after the fact update it a bit. For example, on the Headspace website (at least as of a month ago) if you're logged out it's one thing, but if you're logged in and return to the homepage, it's normal at first and then account-specific options load in.
As someone else pointed out, too, the https://www.sanity.io/ homepage itself, in the very bottom footer, keeps high-level organization mostly the same but loads in the live values for submenu items on hover. Perhaps there's a graceful approach like that, if you can find a way to make it not too distracting, that could accommodate your use case.
May 3, 2023, 3:03 PM
I will look into that as well. Thank you for all of your help 😄