Skip to content

Enable tag based cache revalidation via webhook #11629

@pbright

Description

@pbright

As part of the addition of the live events API, the SanityClient.fetch method returns syncTags which can be used to cache fetch requests. The fetch request cache can then be selectively invalidated based on events emitted by the Live Content API, but there is no way outside of that API to determine the correct tags to invalidate.

I would like to be able to selectively invalidate the fetch request cache via Sanity webhook. Possible approaches that come to mind are:

  1. Adding a GROQ delta function which calculates the syncTags based on the changes to the document, which could then be used in a document webhook projection, or
  2. Adding an additional type of webhook which triggers on any document create/update/delete and returns only the applicable syncTags.

As an alternative I have looked at Live Content API based invalidation via the defineLive function provided by next-sanity/live, but:

  1. Fetch cache invalidation is integrated with other functionality which provides live updates to content on the page. This is undesirable in my case due to layout shift and I would therefore prefer to avoid using the live content API at all rather than using the workaround suggested in next-sanity of wrapping the rendering of all data in a useDeferredLayoutShift hook.
  2. The fetch cache is only invalidated if an active user is on the site. The initial connection created by next-sanity/live does not send the last-event-id header so any changes made in the studio when there is no active user are never processed, leading to outdated content. I considered the two workarounds suggested in next-sanity but as noted the first workaround is a "heavy hammer" and the second workaround requires running a separate long-running server to forward live events to a Next.js route (essentially a webhook with extra steps).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions