Sanity API CDN staleness vs live queries for a low-traffic site
0 reputation · 14 Mar 2021, 21:10 UTC
0 reputation · 14 Mar 2021, 21:10 UTC
We run a low-traffic marketing site backed by a single Sanity dataset (assume a current Sanity v3-era project and a static frontend). To stay within free-plan API allowances, we want to cut runtime API usage as much as possible without breaking the editing experience.
The documented options seem to be: query the cached CDN endpoint (apicdn.sanity.io) at runtime, or fetch content at build time and rebuild via webhook on publish. Both reduce origin API load, but CDN responses are eventually consistent, and build-time snapshots go stale until the next deploy. Meanwhile, preview/draft mode must bypass the CDN and hit the live API, which we expect but have not budgeted for.
We have not yet measured our actual monthly request volume against current plan quotas, and we know pricing and limits change, so we will verify those on the official pricing page before committing.
35275 reputation · 15 Mar 2021, 07:56 UTC
For a marketing site that only serves a few thousand page‑views a month, routing all public requests through apicdn.sanity.io is usually sufficient to stay comfortably within the free‑plan API limits. The CDN is billed as a separate, lower‑cost tier and is designed to cache published content aggressively. However, if you need absolute freshness—e.g. a live “latest news” block that must reflect every publish within seconds—then a build‑time fetch plus webhook rebuild is the safer option, because it removes runtime CDN queries entirely.
After a document is published, Sanity invalidates the relevant cache entries. In practice the first request after a publish usually receives fresh data within a few seconds. The CDN, however, is documented as eventually consistent, so a very small window (often <10 s) can still return the old version. You can observe this by:
curl -i https://apicdn.sanity.io/v2023-12-01/data/query/production?query=*&token=… before and after a publish.Age header and the Sanity client flag cdn in the response body.There is no hard‑coded TTL in the public docs; the Age header is the only reliable indicator of how long a response has been cached.
Preview/draft mode forces useCdn: false and requires a viewer token, so those requests hit api.sanity.io directly. They count against the same API quota as any other origin request. In practice preview traffic is usually negligible because only editors trigger it, but you should still monitor the API usage dashboard for your project to confirm.
useCdn: true in your client for all public reads.GET /_build endpoint on your host and point the Sanity webhook to it.Could you share the approximate number of page‑views per month you expect? That would help determine whether the CDN alone is statistically safe for your quota.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.