How to use a Custom Domain Name for the CDN

6 replies
Last updated: Oct 20, 2020
Hey! I am wondering if I can set a custom domain name for the CDN instead of cdn.sanity.io ? Or do I need to use another CDN and a custom Sanity CMS Asset source to support this?
AI Update

Good news! Custom domain names for Sanity's CDN (cdn.sanity.io) are available, but they require an Enterprise plan. If you're on a different plan tier, you'll need to implement a workaround using a reverse proxy.

Enterprise Plan Option

On the Enterprise plan, Sanity can configure a custom domain for your asset CDN. This is the cleanest solution as it's handled at the platform level, and all your asset URLs will automatically use your custom domain.

Workaround for Other Plans

If you're not on an Enterprise plan, you can achieve custom domain URLs using a redirect/reverse proxy approach:

For Images

The @sanity/image-url library supports a spec.baseUrl parameter that lets you specify a custom base URL for your images. You'd set up a reverse proxy on your domain that forwards requests to cdn.sanity.io.

For Other Assets (PDFs, etc.)

Here's a practical solution that works particularly well with hosting platforms like Netlify:

  1. Extract the filename from the Sanity asset URL:
const fileName = url.substring(url.lastIndexOf('/') + 1)
  1. Use your custom domain in your links:
`https://your-domain.com/files/${fileName}`
  1. Set up a redirect rule on your server to proxy requests to Sanity's CDN:
/files/* → https://cdn.sanity.io/files/[your-project-id]/[your-dataset-name]/:splat

This approach works for any asset type and doesn't require changes to how you query assets from Sanity. The GraphQL/GROQ queries will still return the standard cdn.sanity.io URLs, but you transform them client-side before rendering.

The redirect happens server-side, so your users see your custom domain while the actual assets are still served from Sanity's globally distributed CDN, maintaining performance and reliability. This is a common pattern that many Sanity users have successfully implemented, as discussed in this community thread.

It’s available on an enterprise plan. Or you just reverse proxy it behind a cdn.
Right, I am using the Advanced plan.Reverse proxy it would work? Wouldn't the links to the asset from GraphQL point to Sanity's CDN?
Unsure about the Graphql angle of it. The
user U
/image-url supports a
spec.baseUrl

https://github.com/sanity-io/image-url/blob/master/src/urlForImage.ts#L81 - not sure how that could work for you
Do other assets use the same method? I am hosting PDFs.
You could solve this with a simple redirect• Get the asset url
• Extract the filename
const fileName = url.substring(url.lastIndexOf('/') + 1)
• In links you use something like
<https://your-domain.com/files/${fileName}>
• Redirect
/files/*
to
<https://cdn.sanity.io/files/[your-project-id]/[your-dataset-name]/:splat>
on the server
Cheers
user J
! I am using Netlify too so that is the approach I will take. Thanks
user A
for your assistance 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?