How to disable caching and fetch latest data from Sanity?
I can see the issue! You're fetching data but getting stale content even though it's updated in the Studio. This is a common caching issue with Sanity's CDN, and there are a few solutions.
The main problem is that Sanity's Content Lake uses a CDN that caches responses for performance. Your force: true parameter isn't actually a valid Sanity client option, so it's not doing anything to bypass the cache.
Here are the proper solutions:
1. Use the perspective parameter (Recommended)
The perspectives feature lets you control which version of content you're querying. By default, you might be hitting cached published content. Try adding the perspective parameter to your client configuration or query:
const getUserCart = async (req, res) => {
try {
const currentUserId = req.userInfo.userId;
const carts = await client.fetch(
`*[_type == "carts" && userId == $userId]`,
{
userId: currentUserId,
},
{
perspective: 'previewDrafts', // or 'published' depending on your needs
}
);
res.json(carts);
} catch (error) {
console.log({ message: "Failed to get user cart" });
}
};Note: As of API version 2025-02-19, the default perspective changed to published (previously it was raw). If you need the absolute latest data including drafts, use previewDrafts.
2. Use the CDN bypass option
If you need real-time data and want to bypass the CDN entirely, configure your client with useCdn: false:
import { createClient } from '@sanity/client';
const client = createClient({
projectId: 'your-project-id',
dataset: 'your-dataset',
useCdn: false, // Bypass CDN for fresh data
apiVersion: '2025-02-19',
token: 'your-token', // Required for authenticated requests
});3. Consider using Sanity Functions
If you're building serverless endpoints that need real-time data, consider using Sanity Functions instead of external API routes. They have native integration with the Content Lake and better performance for real-time data access.
Why this happens
When you see updated content in the Studio but stale data in your fetch, it's because:
- The Studio shows you draft content in real-time
- Your API queries might be hitting the CDN cache which serves published content
- The CDN can cache responses for up to 60 seconds by default
Setting useCdn: false or using the appropriate perspective should resolve your issue!
Show original thread11 replies
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.