High bandwidth usage on Gatsby site with Sanity CDN images

29 replies
Last updated: Mar 25, 2021
Hi everyone, back again with a bandwidth question. I have a Gatsby site that builds everything at run time but all of the images are served over the sanity CDN (I’m using the
gatsby-sanity-source
plugin with
gatsby-image
for this) and the bandwidth usage is sky high vs what I’d expect:

https://www.dropbox.com/s/h32v7tjo3nt5ncu/Screenshot%202021-03-17%20at%2009.02.34.png?dl=0
In the last 30 days the website has had around 1,200 visitors, so not exactly
huge traffic. My usage on Netlify is in the MB’s so it has a tiny footprint there. It’s just Sanity usage which I assume is coming from the images?
Can anyone help me figure this out? I’ve tried lowering the resolution of the images but can’t go much lower without the quality suffering.

I have a theory that users might be having to re-download images every time a new update/build is made to the site, but I have no understanding of how to prove that…
AI Update

Based on what you're describing, I can help explain what's likely happening with your bandwidth usage. The good news is that Sanity's image CDN has very aggressive caching that should work in your favor, but there are a few potential issues with the Gatsby setup that might be causing problems.

How Sanity's CDN Caching Works

Sanity's Asset CDN uses content-addressable URLs with SHA-1 hashes, which means the URLs include a hash of the asset contents. This is actually great for caching because:

  • Assets are cached indefinitely once processed
  • The CDN supports both Brotli and Gzip compression
  • Images up to 10 MB can be cached on edge servers globally
  • Cache invalidation only happens if you delete the entire dataset or project

The CDN sets appropriate cache headers that should allow browsers to cache images aggressively, so users shouldn't need to re-download images on every visit.

Why Your Bandwidth Might Be High

Your theory about re-downloads on every deploy could be partially correct, but it's likely related to how gatsby-source-sanity and gatsby-image are handling the image URLs. Here are the most likely culprits:

  1. Image transformation parameters changing: If Gatsby is generating different query parameters for image transformations on each build (even slightly different sizes, quality settings, or formats), each variation creates a new CDN request and counts toward bandwidth.

  2. Multiple image sizes: gatsby-image typically generates multiple responsive image sizes (srcset). For a single image, you might be serving 3-5 different sizes, which multiplies your bandwidth usage. With 1,200 visitors viewing multiple images each, this adds up quickly.

  3. Auto-format parameters: If you're using auto=format in your image URLs, the CDN might be serving different formats (WebP, JPEG, PNG) to different browsers, which would count as separate requests.

  4. Browser cache vs CDN bandwidth: Even though browsers cache the images locally, every unique visitor still needs to download them once. Your Sanity bandwidth measures total CDN delivery, not just uncached requests.

How to Debug This

  1. Check your actual image URLs: Look at your built site's HTML and see what image URLs are being generated. Are they consistent between builds? Do you see lots of different size variations?

  2. Monitor your CDN requests: Use your browser's Network tab to see what's actually being requested. Check the response headers for cache-control and whether requests are returning 304 (cached) or 200 (fresh download).

  3. Calculate expected bandwidth: If you have, say, 10 images per page averaging 100KB each (across all responsive sizes), and 1,200 visitors viewing 5 pages each, that's: 1,200 × 5 × 10 × 100KB = 6GB. That could explain high usage.

Potential Solutions

  • Limit responsive image sizes: Configure gatsby-image to generate fewer size variations
  • Use consistent transformation parameters: Make sure your image queries are consistent across builds
  • Consider using gatsby-plugin-sanity-image: This is a more modern alternative that might handle caching better than the older gatsby-source-sanity + gatsby-image combo
  • Optimize image quality settings: You mentioned trying this, but finding the sweet spot between quality and file size is key

The bandwidth you're seeing is likely legitimate traffic, not a caching problem with Sanity's CDN itself. The CDN is doing its job correctly - the issue is probably the volume of image variants being served and the number of requests per visitor.

Watch your local dev activities, if you disable cache during development & work on a site all day everyday it could really add up
Yeah good shout
user G
— the faint blue line on the image I linked above shows last months bandwidth usage during dev, so I can see it’s minimal comparatively. The site only went live at the start of March
I see.. have you tried strolling through the site to see how much bandwidth it consumes during a typical visit?

I have a theory that users might be having to re-download images every time a new update/build is made to the site, but I have no understanding of how to prove that…
I think if you hit “clear cache & redeploy” it’ll do this, but if you have something like netlify plugin cache the image should be the same on most builds
user G
how could I test that using Firefox developer edition?
Same as in Chromium, Network tab / disable cache / Images
Thanks — this is definitely the issue, just doing a quick test and clicking around to a few articles, scrolling through the homepage and “reading” the content, I’ve hit 7mb
just from images alone
whoops, do you have lots of images?
Yeah it’s an editorial website, I just realised I didn’t share the link in my original post: https://dscvrd.co/
Might be a quick one to set a max-height on the images being fetched, but generally I’m not sure what else I can do here
hhhm maybe you can look into optimizing
sizes
attributes? If the projected cost is an issue for you, imgix is a lot cheaper bandwidth wise vs. sanity 🤫
I was just looking to see if another image handling service might be better actually
You can offload sanity images to something like imgix fairly easily?
maybe try using web proxy for a simple integration https://docs.imgix.com/setup/creating-sources/web-proxy
I just remembered this: https://images.weserv.nl/
Might solve the problem and for free too from what I can tell
what the heck? That’s so cool. Thanks for sharing!
Right?! 😂
I remembered I used it on an instagram hook I set up and I just cached all the images through that
Seems like it has something to do w the fact that the service is powered by cloudflare? How is cloudflare bandwidth always unlimited, I could never understand
Yeah and they’re using it to test out image manip features so I guess you’re handing over your data in that respect
mmhmm, good point
But all the image data is public anyway in my case
Hey
user S
I’m experiencing the exact same problem. It appears that
gatsby-sanity-source
isn’t adding the
auto=format
to the source image
This can be helped by using https://www.sanity.io/plugins/gatsby-plugin-sanity-image , which does add this flag by default
user S
What does that
auto=format
flag do?
Set 
auto=format
 to automatically return an image in 
webp
 formatting if the browser supports it.

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?