SVGs Are Not Caching When Serving Over CDN

1 replies
Last updated: Jun 18, 2021
Hi all! We’re having some issues with SVGs uploaded to the image library are not cached when served over the CDN. The response is not returning a
cache-control
header that lets the browser cache the file. PNGs get the correct
Cache-Control: public, max-age=31536000, s-maxage=2592000
header, but SVGs get no such thing.
According to the documentation the CDN used by Sanity is Google CDN. The Google CDN Caching Overview page (
https://cloud.google.com/cdn/docs/caching ) states that, among other MIME types, “Any 
Content-Type
 matching 
image/*
” will be cached. The SVGs does indeed have
Content-Type: image/svg+xml
when returned from the CDN, so they seem like highly likely candidates for a proper
Cache-Control
header.
Is this a known issue? Something implemented specifically by Sanity for security reasons or other? Or just an unintended side effect? And have anyone bumped into this issue and solved it somehow? Any pointers and suggestions are welcome here.
🙏
AI Update

Good catch! This was a bug that has been fixed. According to the Sanity Answers thread from June 2021, a Sanity team member confirmed this was indeed a bug and mentioned it would be fixed in a pending release.

The issue you described is exactly right - PNGs and other image formats were getting proper Cache-Control: public, max-age=31536000, s-maxage=2592000 headers, but SVGs weren't receiving any cache-control headers despite having the correct Content-Type: image/svg+xml header.

Since this bug was acknowledged and fixed back in 2021, if you're still experiencing this issue today, here are a few things to check:

  1. Verify the URL structure: Make sure your SVGs are being served through Sanity's Asset CDN at cdn.sanity.io. The CDN should automatically handle caching for all image types including SVGs.

  2. Check if it's a recent upload: The Asset CDN caches responses indefinitely once processed. If you recently uploaded the SVG, try accessing it a few times to ensure it gets cached properly.

  3. Browser caching: Clear your browser cache and test again to ensure you're not seeing old cached responses without headers.

  4. Test the actual CDN response: Use curl or browser dev tools to inspect the actual response headers from cdn.sanity.io to confirm whether the headers are present.

If you're still seeing missing cache-control headers on SVGs after checking these items, I'd recommend reaching out to Sanity support or posting in the community Discord as this should definitely be working now. The Asset CDN is built on Google Cloud CDN infrastructure and should treat all image/* content types consistently with proper caching headers.

Good catch! This is a bug.Will be fixed in a pending release, thanks for reporting!

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?