On this page
An SVG external sprite URL can fail on another domain even when the icon worked locally. The cause may be a changed base path, a mismatched fragment ID, a redirect, or a browser restriction on external SVG references. Start by comparing the resolved URL and response in each environment—not just the source markup.
This guide includes a local reproduction and a practical fallback. The reproduction was not run for this article, and current browser documentation and SVG references were not available for review. Check the behavior in your supported browsers before relying on a particular cross-origin configuration.
Key Takeaways
- An external reference such as
/assets/icons.svg#searchcontains both a sprite-file URL and a case-sensitive fragment identifier. Check both. - Relative paths depend on the document’s base URL. A leading slash starts at the origin root, not necessarily at your application’s deployment path.
- Compare the page’s origin with the sprite’s final origin after redirects. Cross-origin external
<use>behavior can vary by browser and context; a successful network response alone does not prove the icon can be rendered. - A same-origin sprite or an inline symbol is a useful fallback when external references are blocked or inconsistent.
Compare the deployed URL and fragment
Suppose local markup contains:
<svg aria-hidden="true">
<use href="/assets/icons.svg#search"></use>
</svg>
If the page is served from http://127.0.0.1:8000/, the reference points to the sprite at that origin’s /assets/icons.svg path and to the search ID inside it. On a production page, the same root-relative path points to the production origin instead. It does not automatically preserve a local development host or an application subdirectory.
Also compare these forms:
assets/icons.svg#searchresolves relative to the document’s base URL./assets/icons.svg#searchresolves from the origin root.https://static.example.test/icons.svg#searchnames a separate host explicitly.
For an app deployed under /dashboard/, check whether the sprite is actually at /dashboard/assets/icons.svg or /assets/icons.svg. A document <base> element can also change how relative references resolve. Differences in scheme or port matter too: origins are determined by scheme, host, and port.
In the browser console, inspect the literal and resolved references for the current document:
for (const element of document.querySelectorAll('svg use')) {
const raw = element.getAttribute('href') ||
element.getAttributeNS('http://www.w3.org/1999/xlink', 'href');
if (!raw) continue;
try {
const resolved = new URL(raw, element.baseURI);
console.log({
raw,
base: element.baseURI,
sprite: resolved.href.split('#')[0],
fragment: resolved.hash,
spriteOrigin: resolved.origin,
pageOrigin: location.origin
});
} catch (error) {
console.warn('Could not resolve SVG reference:', raw, error);
}
}
Run this on both the working and failing pages. Compare the full paths, hosts, schemes, ports, and fragments. This reports the URL before any HTTP redirect; inspect the network request to find the final destination.
Advertisement
Reproduce the host change locally
This is a local reproduction, not a report of an observed test. It uses two ports on the same host, which makes them different origins. You need Python 3 and a browser; no application framework is required.
Create this directory structure:
site/
index.html
assets/
icons.svg
Put this sprite in site/assets/icons.svg:
<svg xmlns="http://www.w3.org/2000/svg">
<symbol id="status" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="9" fill="teal" />
</symbol>
</svg>
Put this page in site/index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>External SVG sprite check</title>
</head>
<body>
<p>Same-origin reference:</p>
<svg width="48" height="48" aria-label="Same-origin status">
<use href="/assets/icons.svg#status"></use>
</svg>
<p>Different-port reference:</p>
<svg width="48" height="48" aria-label="Cross-origin status">
<use href="http://127.0.0.1:8001/assets/icons.svg#status"></use>
</svg>
</body>
</html>
From the directory containing site, start each command in a separate terminal:
python3 -m http.server 8000 --bind 127.0.0.1 --directory site
python3 -m http.server 8001 --bind 127.0.0.1 --directory site
Open http://127.0.0.1:8000/. The first reference is same-origin; the second points to a different origin because its port differs. Inspect both the rendered icons and the browser’s console and Network panel. Depending on the browser and its external-resource rules, the second reference may be blocked or behave differently. A rendered second icon in one browser is not proof that every supported browser will render it.
Stop both servers with Ctrl+C when finished. Use HTTP rather than opening the page as a file: URL, whose origin and resource behavior can differ from a deployed site.
Inspect the response, redirects, and SVG ID
In developer tools, reload the failing page with the Network panel open. Find the sprite request and check:
- Status and response body: Is the response actually the SVG sprite, rather than a login page, application fallback, or error page? Authentication requirements can differ between environments.
- Redirect chain: Does the request move from the application host to a CDN, another scheme, or a login endpoint? Compare the page origin with the final destination, not just the original URL.
- Response headers: Check the
Content-Type, any cross-origin headers, and caching headers. Also review relevant Content Security Policy violations in the console. Header changes may matter, but do not assume adding a CORS header alone will make an external<use>work in every browser. - Fragment ID: Inspect the returned SVG for an exact match.
#statusand#Statusare different IDs. The ID must identify the intended element, commonly a<symbol>in a sprite. - Rendering details: Confirm the referencing
<svg>has usable dimensions and that the symbol’s geometry,viewBox, and styling make the icon visible. A valid request and ID can still produce an invisible or clipped graphic.
Fragments identify content inside the resource; they are not sent as part of the HTTP request. Therefore, a successful request for the file does not verify that the requested ID exists. If deployment files or IDs differ only by letter case, check both explicitly, including on a case-sensitive production filesystem.
You can also open the sprite-file URL without the fragment to inspect its contents. That checks the resource itself, but it does not reproduce the page’s <use> embedding context or all of its policy restrictions.
Advertisement
Choose a fallback
Serve the sprite from the page’s origin
Deploy the sprite with the application, or configure a fixed server route that serves the approved sprite on the page’s origin. Reference the correct deployment path and confirm that it does not redirect back to another origin. This keeps an external sprite file while avoiding the cross-origin boundary for that request.
If a server fetches the sprite from elsewhere, prefer a fixed, trusted upstream rather than an endpoint that accepts arbitrary URLs. A general-purpose fetcher needs additional SSRF protections and should not be introduced just to fix an icon.
Inline the symbol
If a separate request is unnecessary or unreliable, put the symbol in the document and reference it locally:
<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0"
aria-hidden="true" style="position: absolute; overflow: hidden">
<symbol id="inline-status" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="9" fill="teal" />
</symbol>
</svg>
<svg width="48" height="48" aria-label="Status">
<use href="#inline-status"></use>
</svg>
Keep IDs unique in the document. If the page uses a <base> element, verify how the fragment reference resolves; it may need adjustment to point to the current document. Inlining avoids a separate sprite request but increases the page markup and changes how the sprite is shared and cached.
Common Mistakes
- Testing only the file URL. Opening an SVG directly does not establish that an external
<use>can reference it from a different page or origin. - Treating all SVG embeddings alike. An SVG loaded as an
<img>, opened as a document, or embedded in an iframe has a different context from a<use>reference. Do not infer identical restrictions or rendering behavior from one context to another. - Ignoring redirects. A same-origin-looking URL can redirect to a different origin or an HTML login response.
- Assuming a 200 response proves the icon is valid. Check the response body, fragment ID, dimensions, and console messages.
- Fixing only development paths. Test the actual base path and generated markup in staging and production, including any CDN or reverse-proxy configuration.
Best Practices
Document where the sprite is deployed and how its URL is constructed. For each supported browser, test a representative external icon in the real deployment context, including redirects and security policies. Keep an inline or same-origin option available if a separate-host sprite is not reliable across those environments.
Conclusion
Compare the resolved file URL and fragment first, then inspect the final response and browser diagnostics. If the host change introduces an incompatible external reference, serving the sprite on the page’s origin or inlining its symbols provides a practical route forward.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.