svgxuse icon indicating copy to clipboard operation
svgxuse copied to clipboard

It downloads svg sprite twice in chrome and three times in IE11. How can I fix it?

Open goldenmaya opened this issue 6 years ago • 4 comments

image Sometimes downloads three time in chrome. Also I see many loading errors.

goldenmaya avatar Dec 29 '18 03:12 goldenmaya

I noticed the same, for me it was the difference in the href value ./images/sprite.svg vs ./Images/sprite.svg vs Images/sprite.svg. I got around that by adding the following code:

function normalizeUrl(url) {
    if (url) {
        url = url.toLowerCase();
        if (url.lastIndexOf("./", 0) === 0) {
            url = url.substr(2);
        }
    }
    return url;
}
href = normalizeUrl(href);

seems to work so far.

PS: Just noticed the hash part is used by getElementById so maybe its safer to only replace the base part, base = normalizeUrl(url[0]);

Nevermind, use with caution, casing seems to matter, not for IIS though.

Epicycle23 avatar Mar 13 '19 18:03 Epicycle23

I pretty much ended up refactoring the whole plugin:

  • only the cache uses the normalized url/key now since casing may matter depending on the server so if the issue of loading/adding the svg mutliple times is related to the href format this may help Unrelated to this problem but maybe worth mentioning:
  • processed elements get marked with a data attribute so they won't get updated every 100 ms
  • instead of getElementsByTagName it uses querySelectorAll now and only searches for use elements without the data attribute (svg > use:not([data-svgxused]) - IE9+ is fine for me
  • element processing can be called from anywhere which allows me to process elements that have been loaded via ajax, this prevents "flickering" of icons in IE10 at least as long as the svg has already been added/requested

Epicycle23 avatar Mar 16 '19 09:03 Epicycle23

feel like sharing that code somewhere so I don't have to do the same? :)

I pretty much ended up refactoring the whole plugin:

  • only the cache uses the normalized url/key now since casing may matter depending on the server so if the issue of loading/adding the svg mutliple times is related to the href format this may help Unrelated to this problem but maybe worth mentioning:
  • processed elements get marked with a data attribute so they won't get updated every 100 ms
  • instead of getElementsByTagName it uses querySelectorAll now and only searches for use elements without the data attribute (svg > use:not([data-svgxused]) - IE9+ is fine for me
  • element processing can be called from anywhere which allows me to process elements that have been loaded via ajax, this prevents "flickering" of icons in IE10 at least as long as the svg has already been added/requested

daporro avatar Apr 11 '19 20:04 daporro

feel like sharing that code somewhere so I don't have to do the same? :)

I pretty much ended up refactoring the whole plugin:

  • only the cache uses the normalized url/key now since casing may matter depending on the server so if the issue of loading/adding the svg mutliple times is related to the href format this may help Unrelated to this problem but maybe worth mentioning:
  • processed elements get marked with a data attribute so they won't get updated every 100 ms
  • instead of getElementsByTagName it uses querySelectorAll now and only searches for use elements without the data attribute (svg > use:not([data-svgxused]) - IE9+ is fine for me
  • element processing can be called from anywhere which allows me to process elements that have been loaded via ajax, this prevents "flickering" of icons in IE10 at least as long as the svg has already been added/requested

sure just keep in mind that it is more restrictive now regarding browser compatibility (querySelectorAll) but for us that's fine.

svgxuse.zip

Epicycle23 avatar Apr 12 '19 12:04 Epicycle23