Comments (13)
I hit this problem as well when trying to serve an SVG sprite from a CDN. First, there is a fundamental problem: The use
tag's xlink:href
will NEVER load content from a remote server, even with CORS turned on. See the comments on this post.
There is a PARTIAL workaround: configure the CDN to handle CORS (CloudFront instructions, Heroku/Rails instructions) and then instruct svg4everybody to use AJAX to load the remote svg asset by setting polyfill to true: svg4everybody({ polyfill: true; });
While that will successfully load the remote resource, it won't stop the browser from throwing an error about "Unsafe attempt to load URL" for every instance of <use>
on the page, which is ugly.
After going through all those steps I ended up just serving my SVG sprite directly from the server rather than from the CDN. Sometimes the simplest solution is the best.
from svg4everybody.
I'm loving using <use>
within <svg>
to use svg icon sprites rather than icon fonts. I understand that there is currently no way to load them from a CDN even with CORS configured but what I am wondering is if there is a way to make use of some sort of onerror
handler so that today we can attempt to load from a CDN, detect if it didn't work, and then load locally as a fallback.
from svg4everybody.
For anyone who's wondering - https://css-tricks.com/ajaxing-svg-sprite/
The process described is:
a) Requesting the svg resource via XMLHTTPRequest
b) Appending the result to the body of the html page.
From the article:
var ajax = new XMLHttpRequest();
ajax.open("GET", "svg/sprite.svg", true);
ajax.send();
ajax.onload = function(e) {
var div = document.createElement("div");
div.innerHTML = ajax.responseText;
document.body.insertBefore(div, document.body.childNodes[0]);
}
Just pasting it just in case anyone encounters the same issue!
from svg4everybody.
Yes, itβs because <use>
is asking for the resource as a kind of document, which is a no-no. One way to work around this is trigger the polyfill in every browser and setup your CDN to allow XMLHttpRequests.
from svg4everybody.
So, if I'm reading that correctly, this is a limitation of SVG and not of your library. Right?
from svg4everybody.
Hello, I'm currently using AWS cloudfront and came up with this same issue. How exactly did you solve this?
from svg4everybody.
Currently there is no workaround (if you want to use ). The only way to load an external SVG from a CDN is via an Ajax call (see how svg4everybody does, in its code).
from svg4everybody.
A solution might be to proxy-pass your image file using for example nginx, so that your file can be requested at your main domain.
from svg4everybody.
Without setting up your CDN to allow requests, this is a security feature (and in your case limitation) of cross domain requests, and not of SVGs or this library per say.
from svg4everybody.
Thanks, @jonathantneal. We're going to try moving our assets to a subdomain to overcome this.
from svg4everybody.
For IE9 you can always use the XDomainRequest object.
from svg4everybody.
@jmmcduffie I just tried moving the assets to a subdomain but still no luck. Did it work for you?
from svg4everybody.
I'm running into the same issue.
What anyone able to solve?
from svg4everybody.
Related Issues (20)
- Not working for MS Edge HOT 3
- can't make href work using svg4everybody HOT 1
- svg4everybody is not defined
- Add workaround for SVG print bug in Firefox
- svg-chrom HOT 1
- SVG transform animateTransform not working in IE11 and Edge 42
- linearGradient for svg4everybody HOT 2
- run the polyfill only on selected dom element and its children
- Validate Not Running After Page Start
- Inlining External SVG
- Inlining External SVG from sprite, SVG doesn't display correctly HOT 3
- PreserveAspectRatio attribute is stripped out on IE11
- Add option to always replace contents ignoring browser support HOT 2
- Empty XHR embeds after splice(0)
- import does not seem to work properly
- Generate png from svg
- Svg4everybody not working on all svgs in IE11 HOT 2
- Suggestion: Suggest loading the script with the nomodule attribute HOT 2
- Issue with iPhone5 / Safari6
- Link to kartikprabhu.com "Inline SVG Icons" is broken; Archive.org link provided
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from svg4everybody.