Sanity cache not updating in Next.js 13 despite useCdn prop changes

4 replies
Last updated: Sep 29, 2023
Hi all!I have an issue regarding cache. Im using Sanity v3 embedded in Next.js 13 and a very simple schema with a Post containing a title, image and portable text - pretty much like the example apps. Whenever i make any changes, they are not reflected in the in response of the query. I've tried both enabling and disabling the useCdn prop of
createClient from the next-sanity library, but so far it has made no change. Any ideas what I need to do to? Thanks!
AI Update

This is a common issue with Next.js 13+ App Router and it's actually related to Next.js's aggressive caching behavior, not Sanity itself. The useCdn prop won't help here because Next.js is caching your data on the server side.

Here are the solutions that work with Next.js 13 and the App Router:

Option 1: Disable caching per fetch (recommended for development) Add { cache: 'no-store' } to your Sanity client fetches:

const data = await client.fetch(query, params, { cache: 'no-store' })

Option 2: Add route segment config (better for production) At the top of your page.tsx or layout.tsx files, add:

export const revalidate = 10; // revalidate every 10 seconds

Or for completely dynamic routes:

export const dynamic = 'force-dynamic';

Option 3: Use the modern sanityFetch helper The next-sanity package provides a sanityFetch helper that's designed specifically for Next.js App Router with built-in cache configuration:

import { sanityFetch } from '@/lib/sanity.fetch'

const posts = await sanityFetch({
  query: POSTS_QUERY,
  tags: ['post'], // enables tag-based revalidation
})

Why this happens: Next.js 13+ App Router caches everything by default for performance. When you query Sanity, Next.js caches the response, so even though Sanity has fresh content, Next.js serves the cached version. The useCdn setting only controls whether Sanity uses its CDN vs API endpoint—it doesn't affect Next.js's own caching layer.

Best practice for production: Use time-based revalidation (export const revalidate = 60) or tag-based revalidation with Sanity webhooks and Next.js revalidateTag for optimal performance while keeping content fresh.

Show original thread
4 replies
Hey
user U
. Although this will sound like a simple question, are you publishing the document? The queries should return the correct result for non-drafts, and it should be Next caching your page in case you're using something like Next's ISR .You can also
review caching strategies , such as opting out of caching through
{ cache: 'no-store' }
It was in fact next causing the issue. Exporting const dynamic = 'force-dynamic' solved it.Thank you
🙂
Awesome. Glad it's sorted. Have a great day
You too!

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?