Sanity 3.21 cacheKey API: stale query results after webhook-triggered document updates
0 reputation · 22 Apr 2020, 01:14 UTC
0 reputation · 22 Apr 2020, 01:14 UTC
After upgrading a project from Sanity 2.x to 3.21, I want to adopt the new cacheKey customization API for client queries while keeping results consistent when documents change. The concern is that queries served from the client-side cache appear to keep returning pre-mutation data even after a document is updated through a webhook-driven pipeline.
The constraints: staleTime and gcTime are currently left at their defaults, and the application relies on webhook notifications from Sanity to signal that content has changed. The interaction between a custom cacheKey and webhook-triggered invalidation is not clearly documented, so it is unclear whether a custom key bypasses the normal invalidation path or merely changes how entries are stored. There is also mention of a skipInvalidation fetch option that may not be stable across releases, which makes relying on it risky.
Assume Sanity client 3.21+ with the default in-memory cache. This can be verified by querying a document, mutating it via the HTTP API, and observing whether the cached payload changes after the expected invalidation window.
Does a custom cacheKey affect how webhook-triggered document updates evict cached query results? What staleTime/gcTime values are recommended when mutations originate outside the client? Is skipInvalidation safe to depend on in 3.21?
1. Custom cacheKey does not automatically change how webhook‑driven updates evict cache entries. The cache still uses the key that the mutation observer receives. If your custom key does not match that key, the entry will survive the invalidation event.
2. Recommended staleTime / gcTime when mutations come from outside the client. Keep staleTime short (e.g., 30 s) and gcTime moderate (e.g., 5 min) to ensure fresh data after a webhook fires but avoid constant refetching.
3. skipInvalidation is not safe to rely on in 3.21. It is marked unstable and may be removed in future releases, so avoid using it in production code.
cacheKey changes the lookup string but the internal invalidation logic still uses the key that comes from the mutation payload. If that payload contains a different key (e.g., the default key that includes the document ID), the cache entry will not be invalidated.skipInvalidation is experimental, its behavior may differ across minor patches.staleTime is 5 minutes; default gcTime is 30 minutes._id and _rev.cacheKey that does not include the document ID.
sanityClient.fetch('*[_id == $id]', {id: 'doc1'}, {cacheKey: 'myKey'})POST https://{dataset}.api.sanity.io/v2023-10-01/data/mutate/{dataset}
{\"mutations\": [{\"createOrReplace\": {\"_id\": \"doc1\", \"title\": \"Updated\"}}]}cacheKey.
sanityClient.fetch('*[_id == $id]', {id: 'doc1'}, {cacheKey: 'myKey'})Check if the result is still the old payload.console.log(sanityClient.cache.entries());cacheKey that includes the document ID (e.g., myKey-doc1) and verify that the entry is invalidated.cacheKey or construct a key that incorporates the document ID (and optionally the revision) so that the mutation observer can match it during webhook invalidation.staleTime to a short value (e.g., 30 s) and gcTime to a moderate value (e.g., 5 min) when you rely on external webhooks for updates.skipInvalidation in production code; rely on the built‑in invalidation flow instead.If your webhook payload does not include the document ID or revision, the cache invalidation will not match any entry. Confirm the webhook payload structure before applying the custom key strategy.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.