
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeConstantly refetching data during development can definitely slow things down. Here's how to optimize your Sanity + Next.js setup for better caching:
The most important setting is enabling Sanity's CDN when creating your client. The Sanity CDN provides unlimited rate for cached content and is much faster than querying the API directly:
import { createClient } from '@sanity/client'
export const client = createClient({
projectId: 'your-project-id',
dataset: 'production',
apiVersion: '2024-01-01',
useCdn: true, // Enable CDN for faster responses
})Setting useCdn: true is a sensible default for most situations. The CDN cache is flushed every time you publish content, so there's only a brief delay between publishing and seeing updates.
sanityFetch Helper FunctionIf you're using next-sanity, you can create a helper function that wraps your Sanity client with Next.js caching configuration:
import { createClient, type QueryParams } from 'next-sanity'
export const client = createClient({
projectId: 'your-project-id',
dataset: 'production',
apiVersion: '2024-01-01',
useCdn: true,
})
export async function sanityFetch<const QueryString extends string>({
query,
params = {},
revalidate = 60, // default revalidation time in seconds
tags = [],
}: {
query: QueryString
params?: QueryParams
revalidate?: number | false
tags?: string[]
}) {
return client.fetch(query, params, {
next: {
revalidate: tags.length ? false : revalidate,
tags,
},
})
}Then use it in your components:
const posts = await sanityFetch({
query: POSTS_QUERY,
revalidate: 60, // Cache for 60 seconds
})To see what's actually being cached during development, enable fetch logging in your next.config.ts:
const nextConfig = {
logging: {
fetches: {
fullUrl: true,
},
},
}This will show you cache hits/misses in your terminal, helping you understand what's working.
You should only query the API directly (useCdn: false) when responses need to be guaranteed fresh, such as during static site generation. For example, you can override the CDN setting per-request:
export async function generateStaticParams() {
const slugs = await client
.withConfig({ useCdn: false })
.fetch(POSTS_SLUGS_QUERY)
return slugs
}For production, you have several options:
revalidate: 60)revalidatePathThe key takeaway: use useCdn: true for development and most production scenarios. This gives you fast responses with the Sanity CDN's global caching, plus you can layer Next.js caching strategies on top for even better performance.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store