Sanity client not using CDN URL despite useCdn:true in React

15 replies
Last updated: Jun 23, 2022
Hello Team, I wanted to use Sanity API CDN in my React Project. I'm using @sanity/client in the project. As document suggested I enabled useCdn:true but in the URL I check it is still using api.sanity.io instead if cdnapi.sanity.io please kindly help me in this how to use the CDN with React
AI Update

I can see why you're confused! When you set useCdn: true but still see api.sanity.io in your network requests, there are a few common reasons this happens.

Why You're Seeing api.sanity.io

1. Check Your Client Version

The behavior around tokens and CDN usage changed significantly in @sanity/client v3.0.0. If you're on an older version, the client would bypass the CDN when using tokens. However, in v3.0.0 and later, the client properly supports using tokens with the CDN - authenticated requests are cached and segmented by token.

Check your version:

npm list @sanity/client

If you're below v3.0.0, upgrade to the latest:

npm install @sanity/client@latest

2. Real-time Features Bypass CDN

If you're using .listen() or other real-time subscription features, these always use the direct API endpoint (api.sanity.io) because they require live connections. This is expected behavior and not related to your useCdn setting.

3. Write Operations Use Direct API

Any mutations (create, update, delete operations) always go through api.sanity.io, not the CDN. The Live CDN is only for read operations.

Correct Configuration

Here's how to properly configure your client to use the CDN:

import { createClient } from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'production',
  useCdn: true,
  apiVersion: '2024-01-01'
  // token is optional - works fine with CDN in v3.0.0+
})

How to Verify CDN Usage

Check your browser's Network tab when making query operations. When the CDN is active, you should see requests to:

  • apicdn.sanity.io (this is the correct CDN URL format)

Important note: The CDN URL is apicdn.sanity.io, NOT cdnapi.sanity.io as you mentioned. If you're looking for cdnapi.sanity.io, that's why you're not finding it - the correct URL has "apicdn" not "cdnapi".

If you're still seeing api.sanity.io for read queries after checking these points:

  1. ✅ Verify useCdn: true is set in your client configuration
  2. ✅ Confirm you're on @sanity/client v3.0.0 or later
  3. ✅ Make sure you're inspecting a query operation, not a mutation or listener
  4. ✅ Check that you don't have any custom fetch implementations overriding the URL

The Live CDN provides automatic cache invalidation with approximately 10-minute cache times using a stale-while-revalidate strategy. This gives you a great balance of performance and freshness - the CDN serves cached content while fetching fresh updates in the background.

If you've verified all of the above and are still experiencing issues, double-check that you're looking at the right URL (apicdn.sanity.io) and share your client configuration code (without tokens!) for further troubleshooting!

Show original thread
15 replies
Do you use a token with your client?
Yes. I used
What all parameters in config should I have to enable CDN ?
Nope, not with a token. 🙂
This causes requests to be authenticated, which bypasses the CDN.
So, should I use token or not ?
Is your dataset public or private? If your dataset is private, you can only query data with a token. Because that‘s essentially authentication.
If your dataset is public, you don’t need a token. You can just read data without it. You’d need it to write data though.
This is my config file
My dataset is not a public
user F
so, using token
Right. So no CDN I’m afraid.
user F
useCdn:true will bypass it to Sanity CDN but why I'm seeing url api.sanity.io
No,
useCdn
will not work with a token. That’s what I meant. 😄
okay
user F
Thanks a lot🙏😃

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?