Documents update in Studio but API returns old version with certain data

15 replies
Last updated: Dec 12, 2022
Hi,
This is a bit of a shot in the dark, we have some production problems we can't get to the bottom with.

We have a dataset with ~7000 documents, and some of the documents stops working when we add certain data to them.

The problem looks like this:
1. We have a working document
2. Add some data
3. Publish, all looks good in Sanity studio
4. Fetch the document from the API, the changes you made do not appear in the API. The _updateAt date do not change.
5. You can publish how many changes you want and they look like they succeed in the Studio, you get revision history and everything in the Studio. But fetching it from the API returns the version from before the "problematic data" was added.
6. Remove the problematic data, now you get the latest version from the API and everything is fine.


However, the "problematic data" seems to be random.

When we have this unwanted behavior, we discovered that different endpoints gives different data.


Works, we get latest version:
https://<project-id>.<http://api.sanity.io/v1/data/doc/production/|api.sanity.io/v1/data/doc/production/><doc-id>


Broken, we get old version until we remove the "problematic data":
https://<project-id>.<http://api.sanity.io/v2021-10-21/data/query/production?query=*[_id|api.sanity.io/v2021-10-21/data/query/production?query=*[_id> == "<doc-id>"]


(Tested different api versions and it's the same thing)


Anyone experience similar issues that can give some hints?
AI Update

This is a tricky issue! Based on the symptoms you're describing - where one endpoint returns fresh data but query endpoints return stale data - you're likely hitting a CDN cache propagation issue combined with how different API endpoints interact with Sanity's caching infrastructure.

What's Happening

The two endpoints you're testing behave differently:

  1. /v1/data/doc/ endpoint - This is a direct document fetch endpoint that appears to be retrieving fresher data
  2. /v2021-10-21/data/query/ endpoint - This is the Query API endpoint that uses GROQ and is served through the API CDN

The Query API endpoints are served through Sanity's Live CDN, which uses intelligent caching with approximately 10-minute cache times. While the CDN should automatically handle cache invalidation when you publish changes, certain edge cases with document structure or content patterns can cause delays in cache propagation.

Why It Appears Random

The "problematic data" probably isn't inherently broken - instead, certain data patterns might be triggering edge cases in how the CDN invalidates and propagates updates. This could include:

  • Large nested structures or arrays
  • Complex reference chains
  • Documents approaching size limits
  • Specific field types or data patterns that affect cache keys

Immediate Solutions to Try

1. Add explicit perspective parameter

The query endpoint respects perspectives, which control what version of documents you see. Try adding perspective=raw to your query:

https://<project-id>.api.sanity.io/v2021-10-21/data/query/production?query=*[_id=="<doc-id>"]&perspective=raw

Available perspectives:

  • raw - All documents including drafts
  • published - Only published documents (may be the default depending on API version)
  • previewDrafts - Prioritizes drafts over published

2. Bypass the CDN for testing

If you're using @sanity/client, disable CDN caching to rule out cache issues:

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'production',
  apiVersion: '2024-01-01',
  useCdn: false, // Bypass CDN completely
})

This will hit the API directly without caching, helping you determine if it's truly a CDN propagation issue.

Debugging Steps

  1. Check _updatedAt timestamps: Compare the _updatedAt field from both endpoints to confirm one is serving stale data

  2. Look for draft documents: Check if drafts.<doc-id> versions exist that might be causing confusion:

    *[_id == "<doc-id>" || _id == "drafts.<doc-id>"]
  3. Test with different API versions: Try the latest API version (e.g., v2025-02-19) to see if behavior differs

  4. Document size check: Very large documents can sometimes cause issues - check if your "problematic" documents are significantly larger than others

  5. Monitor cache headers: When fetching from the query endpoint, check the response headers for cache-related information (Age, Cache-Control, etc.)

Long-term Solution

Since this affects multiple documents in a seemingly random pattern, I'd recommend:

  1. Contact Sanity Support: This sounds like it could be a CDN edge case that needs investigation. Provide them with:

    • Specific document IDs that reproduce the issue
    • The exact data patterns that trigger the problem
    • Timestamps showing when you publish vs when the cache updates
    • Your project ID and dataset name
  2. Consider using useCdn: false for critical queries: If you need guaranteed fresh data for specific queries, bypass the CDN for those requests

  3. Implement client-side cache busting: As a workaround, you could add a timestamp parameter to force fresh fetches when needed

The fact that the /data/doc/ endpoint works while the query endpoint doesn't strongly suggests this is related to how the CDN handles cache invalidation for GROQ queries versus direct document fetches. This is definitely worth reporting to Sanity support with specific reproduction cases.

Show original thread
15 replies
Could you share some actual query urls with us?
Hi, here is one example:
https://ol1w6ddr.api.sanity.io/v1/data/doc/production/891b828d-fc8c-44a7-bf35-7fbd64d66a3b
https://ol1w6ddr.api.sanity.io/v2021-10-21/data/query/production?query=*[_id%20==%20%22891b828d-fc8c-44a7-bf35-7fbd64d66a3b%22]
The first one gets the latest version:
"_updatedAt":"2022-11-22T14:07:15Z"
The second one gets an old version:
"_updatedAt":"2022-11-15T11:59:04Z"
Here is one more example:
https://ol1w6ddr.api.sanity.io/v1/data/doc/production/e830fa8f-aa23-422f-b668-ee203b8159f0
https://ol1w6ddr.api.sanity.io/v2021-10-21/data/query/production?query=*[_id%20==%20%22e830fa8f-aa23-422f-b668-ee203b8159f0%22]
Response from first endpoint:
"_updatedAt":"2022-11-24T08:14:13Z"
Response from second endpoint:
"_updatedAt":"2022-11-21T07:58:14Z"

Although it's the same document we fetch from the same dataset.


sanity dataset export ...
also returns the correct data, it seems to be just the api that we use to query data that returns old data.
do these things also behave the same in vision (the query playground in the studio)?
Yes, that broken url:
<https://ol1w6ddr.api.sanity.io/v2021-10-21/data/query/production?query=*[_id%20==%20%22e830fa8f-aa23-422f-b668-ee203b8159f0%22]>
Is copied from vision
We made an export yesterday and restored it to a new dataset, and the new dataset works as expected. But we would like to know what caused it because the export and import is not a sustainable solution since it removes the revision history, and takes time.
The dataset I linked to with this unwanted behavior still exists for debug purposes.
Did you save the other dataset?
Hi,
Did you find anything we did wrong with that dataset?

The new dataset that we imported to still works I think, at least no more cases of this behavior has been reported yet.
Hello
user K
sorry for the late reply, we have been super busy with the v3 release and are getting back on track now. I do not have news for you yet, but I am still on it!
Hi Felix - we're checking in with our backend team as it seems like something odd might be going on here indeed. Thank you for providing the details to help us progress this internally. We'll get back to you here on Monday if not earlier.
Great, thank you!
Good morning
user K
- we've implemented a potential fix for this in the backend. May we ask you to check if you are getting more consistent results between the two different endpoints now?
Morning,
Yes now it seems to be working! I also see new validation errors as well, which I assumes points out the cause of the issue, which is great!
Thank you much!
That's great, thanks for confirming!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?