🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Question about slow updates with Sanity client API

19 replies
Last updated: Apr 24, 2023
Hi there, I have a question regarding updates made via the sanity client api. I have created essentially an auth flow with sanity for users. I've noticed that the data in sanity does not update immediately when an update is made to the data on the frontend. Sometimes 2 or 3 refreshes is required. As a result I've been optimistically rendering the changes for the current session. Does this seem like expected logic when using the sanity API or is there some sort of configuration I need to do to make the data that has changed appear faster?
Apr 24, 2023, 9:39 PM
To clarify, are the changes not showing up in the Studio or in the frontend?
Apr 24, 2023, 10:00 PM
Hi RD 🙂, seems like the changes are appearing in the studio and not showing up on the frontend.
Apr 24, 2023, 10:01 PM
👋 Got it! Are you using the CDN? It can take a bit for the cache to revalidate. About how long do you think it takes to refresh/see new content?
Apr 24, 2023, 10:02 PM
it takes usually around 15-20 seconds and I set useCdn to false
Apr 24, 2023, 10:02 PM
Hmm, that is a while, even if you were using the CDN. Can you share some example code?
Apr 24, 2023, 10:04 PM
yes of course! So in a serverless function i'm instantiating the sanity client as such:
Apr 24, 2023, 10:05 PM
export const sanityClient = client({
  projectId: process.env.SANITY_PROJECT_ID,
  dataset: process.env.SANITY_PROJECT_DATASET,
  apiVersion: '2022-01-12',
  useCdn: false,
  token: process.env.SANITY_AUTH_TOKEN // Only if you want to update content with the client
Apr 24, 2023, 10:05 PM
and here's a segment the body of the serverless function
Apr 24, 2023, 10:06 PM
const formattedInterests = interests.length ? interests.map(i => ({
            _ref: i._id || i._ref,
            _key: i._id || i._ref,
            _type: 'reference',
            _weak: true
        })) : [];
        const doc = {
            tags: formattedInterests,
        const response = await sanityClient.patch(id).set(doc).commit ();
Apr 24, 2023, 10:06 PM
that response has the updated data from the user document
Apr 24, 2023, 10:06 PM
but if i refresh the page, the data i'm seeing on the frontend is stale
Apr 24, 2023, 10:07 PM
What about where you’re trying to render the data?
Apr 24, 2023, 10:07 PM
ah yeah i'm using nuxt-sanity for that
Apr 24, 2023, 10:08 PM
do i have to set useCdn to false in my nuxt config file as well?
Apr 24, 2023, 10:08 PM
    ** Sanity configuration
    sanity: {
        projectId: process.env.SANITY_PROJECT_ID, 
        dataset: process.env.SANITY_PROJECT_DATASET
Apr 24, 2023, 10:08 PM
I think it depends on which version of the client that’s using. If it’s a more recent one, it would have it set to true by default. It may be worth it to try specifying false to see if it helps.
Apr 24, 2023, 10:09 PM
ok thank you! will try that!
Apr 24, 2023, 10:10 PM
Yep that works! thank you very much, RD!
Apr 24, 2023, 10:14 PM
Apr 24, 2023, 10:19 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?