On this page
- Key Takeaways
- Reproduce the migration locally
- Diagnose an external SVG use link not working after CDN migration
- 1. Check the asset URL and fragment separately
- 2. Inspect the response and redirect chain
- 3. Check the console and origin policy
- Choose a fallback before switching traffic
- Common Mistakes
- Best Practices
- Conclusion
An icon can disappear after a CDN migration even when its SVG file returns successfully. An external SVG <use> reference must resolve both the file and the fragment identifying the icon, and browser security restrictions can affect references that cross origins.
This guide compares same-origin and cross-origin setups and shows what to inspect before moving a shared sprite. Browser documentation and SVG specifications could not be consulted for this article, and the examples were not executed here. Check the behavior in the browsers and embedding contexts your application supports.
Key Takeaways
- A page and sprite on different schemes, hosts, or ports are cross-origin, even if both are under your control.
- A successful SVG file response does not prove that a particular
#fragmentrenders. - Cross-origin external
<use>behavior can be restricted by browsers. Do not assume that adding CORS headers will make it work. - Inspect redirects, the final response, the fragment ID, and the browser console. Test the actual page, not just the asset URL.
- If the CDN reference fails in a required browser, keep the sprite same-origin or use inline SVG for affected icons.
Reproduce the migration locally
The following lab uses two local HTTP servers on different ports. The first serves the page and a same-origin sprite; the second acts as a separate asset origin. This is an illustrative example, not a substitute for testing your production CDN and supported browsers.
Create these files:
svg-lab/
site/
index.html
icons.svg
cdn/
icons.svg
Put the following sprite in both site/icons.svg and cdn/icons.svg:
<svg xmlns="http://www.w3.org/2000/svg">
<symbol id="search" viewBox="0 0 24 24">
<circle cx="10" cy="10" r="6" fill="none" stroke="currentColor" stroke-width="2" />
<path d="M14.5 14.5 20 20" fill="none" stroke="currentColor" stroke-width="2" />
</symbol>
</svg>
Put this page in site/index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>SVG sprite origin test</title>
</head>
<body>
<p>Same-origin sprite:</p>
<svg width="32" height="32" viewBox="0 0 24 24" aria-label="Search" role="img">
<use href="/icons.svg#search"></use>
</svg>
<p>Cross-origin sprite:</p>
<svg width="32" height="32" viewBox="0 0 24 24" aria-label="Search" role="img">
<use href="http://localhost:8001/icons.svg#search"></use>
</svg>
</body>
</html>
With Python installed, open two terminals in svg-lab and run one command in each:
python3 -m http.server 8000 --directory site
python3 -m http.server 8001 --directory cdn
Open http://localhost:8000/ and compare the icons. The first reference points to the page's origin; the second uses another port and is therefore cross-origin. Stop each server with Ctrl+C when finished. This lab uses HTTP only for local development; test the deployed HTTPS page separately.
If the same-origin icon appears but the cross-origin icon does not, investigate browser restrictions alongside the response differences described below. If neither appears, first check the markup, file contents, and serving setup.
Advertisement
Diagnose an external SVG use link not working after CDN migration
1. Check the asset URL and fragment separately
In the page's Elements inspector, find the rendered <use> and inspect its href. Confirm that the URL resolves to the intended sprite and that the fragment exactly matches the symbol's id, including capitalization. For example, #search must identify id="search" in the file actually served.
The fragment is not sent as part of the HTTP request for the file. A request for icons.svg#search can therefore return the sprite successfully even if search is absent. Opening the file URL alone is not a complete rendering test.
Also check that the page's <svg> has dimensions and a suitable viewBox, and that CSS does not hide the icon or make its color indistinguishable from the background. In this example, currentColor inherits the surrounding text color.
2. Inspect the response and redirect chain
In the browser's Network panel, reload the page and select the sprite request. Check:
- Status and body: Did the final response contain the expected SVG, rather than an error page or a different sprite?
- Redirects: Did an apparently same-origin URL redirect to a different origin, or did the CDN redirect to another host? Inspect each hop and the final URL.
- Response type: Is the SVG served with
Content-Type: image/svg+xml? An incorrect type or response security headers may affect how the browser handles it. - Caching: Could the browser or CDN be serving an older sprite that lacks the requested ID? Verify the actual response body before changing cache settings.
A 200 response is useful evidence that a file was returned, not proof that the fragment is valid or that the browser rendered it.
3. Check the console and origin policy
Look for console messages about blocked resources, SVG references, content security policy, or mixed content. A page loaded over HTTPS should not reference an HTTP sprite. Console messages can help narrow the issue, but their absence does not prove the reference is permitted or valid.
CORS headers are relevant to some cross-origin resource requests, but they are not a universal fix for external SVG <use>. Browser restrictions on referencing an SVG document can apply separately. Test the exact page and CDN response in each required browser rather than assuming that a permissive Access-Control-Allow-Origin header makes the icon render.
A command-line request can help inspect the file and headers, but it cannot reproduce the browser's complete rendering and security decisions. Similarly, an SVG used through <img> or embedded in another context is not necessarily governed by the same behavior as a fragment referenced by <use>.
Advertisement
Choose a fallback before switching traffic
For browsers or contexts where the CDN reference fails, choose a deployment strategy that does not depend on detecting a failed <use> at runtime:
- Serve the sprite from the page's origin. Keep the external sprite at a same-origin path, and verify that the path does not redirect to the CDN origin. This preserves shared sprite references but means the asset must be available through your site's origin.
- Inline affected icons. Put the SVG shapes directly in the page or component. This avoids the external sprite reference, at the cost of repeating or bundling the markup with the page.
- Use another representation where appropriate. A standalone image may fit some icons, but check accessibility, styling needs, and browser behavior in that embedding context; it is not a drop-in equivalent for every sprite fragment.
For example, the search icon above can be inlined as follows:
<svg width="32" height="32" viewBox="0 0 24 24" role="img" aria-label="Search">
<circle cx="10" cy="10" r="6" fill="none" stroke="currentColor" stroke-width="2"></circle>
<path d="M14.5 14.5 20 20" fill="none" stroke="currentColor" stroke-width="2"></path>
</svg>
Choose an appropriate accessible name for meaningful icons. If an icon is purely decorative, hide it from assistive technology instead of giving it a redundant name.
Common Mistakes
- Testing only the file URL. The file can load while the fragment is missing or external use is blocked.
- Assuming related domains are same-origin. A different subdomain or port changes the origin; redirects can also change the destination.
- Treating CORS as a guaranteed repair. Confirm the actual external
<use>behavior in supported browsers. - Relying on an image or stylesheet test. Those resource contexts do not establish that an SVG fragment will render through
<use>. - Expecting a reliable runtime error event for every failure. Rendering failures may not provide a useful event to trigger a fallback; select the fallback deliberately and verify it visually.
Best Practices
Before migration, inventory the sprite URLs and fragment IDs used by the application. Test representative icons on the real HTTPS page against the proposed CDN, including redirects, caching, and your supported browser and embedding-context matrix. Compare the rendered result as well as the network response.
Keep a same-origin or inline option available for required icons. Retest after CDN configuration changes, especially changes to redirects, content types, or security headers.
Conclusion
A CDN move changes more than the sprite's address: it can change the origin and the browser's ability to use its fragments. Verify actual rendering and keep a fallback ready before switching shared icons to the new URL.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.