Skip to content

Commit

Permalink
Refine logic to detect whether link exists already
Browse files Browse the repository at this point in the history
Chrome/Safari seem to wait until the link tag has finished loading to
add it to `document.styleSheets` and that causes the slim loader to
duplicate the link tag.

Using `document.querySelectorAll` works fine in the three browsers

Closes #64
  • Loading branch information
m-mujica committed Jan 24, 2018
1 parent f0fb5ff commit 1d84d18
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions slim.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ function CssModule(address) {
// timeout in seconds
CssModule.waitTimeout = 60;

CssModule.prototype.linkExists = function() {
var styleSheets = document.styleSheets;
// The slim build does not resolve to URLs, so this trick is needed
// to get the url and use it to check if the link was already added
// to the head
CssModule.prototype.getLinkUrl = function() {
var anchor = document.createElement("a");
anchor.href = this.address;
var href = anchor.href;
return anchor.href;
};

for (var i = 0; i < styleSheets.length; ++i) {
if (href === styleSheets[i].href) {
return true;
CssModule.prototype.linkExists = function() {
var styleSheets = document.querySelectorAll('[rel="stylesheet"]');

if (styleSheets != null) {
var href = this.getLinkUrl();
for (var i = 0; i < styleSheets.length; ++i) {
if (href === styleSheets[i].href) {
return true;
}
}
}

Expand Down

0 comments on commit 1d84d18

Please sign in to comment.