✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Next.js Adds Revalidation, But Doesn't Allow Sub-Queries in Webhooks?

2 replies
Last updated: Sep 5, 2022
NextJS added revalidation, which is amazing because you can re-build specific pages when a sanity webhook is triggered, HOWEVER, Sanity doesn't allow sub-queries in webhooks. So this:

{
  "slug":slug,
  "type":_type,
  "revalidate_relevant_items":true,
  "related_programs": *[_type == "programma"]
}
doesn't work. When one post gets updated, I want all other posts that reference that post to get updated as well, does anyone have a workaround for this? For example: I have a 'theme' document type. Then I have lots of 'blog' documents that reference different themes. When I edit the title of a theme, all these blog posts need to get revalidated in NextJS. NextJS doesn't have the option to do dynamic revalidation yet, so I was hoping that Sanity could send the relevant blog post urls to the revalidation API, but apparently that doesn't work?

In other words: is there a way to do sub-queries/groq queries in a webhook?
Sep 5, 2022, 8:07 PM
A bit more work on your end, but can the endpoint receiving the Sanity webhook do the query for all relevant documents (
related_programs
in this case) that need to be revalidated, then feed those to the revalidate function in Next?
Sep 5, 2022, 8:21 PM
yeah that's certainly possible, thanks for the suggestion! I was hoping I could do it without any extra api calls, but I think this is the only way at the moment.
Sep 5, 2022, 8:33 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?