Skip to content

Commit 8bc8acf

Browse files
Merge pull request michael-misshore#9 from michael-misshore/v1.2.1-update
Add cssrelpreload.js
2 parents 36f7dfc + 090cf52 commit 8bc8acf

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The loadCSS and onloadCSS files will be added to the asset pipeline and availabl
1818

1919
```js
2020
//= require loadCSS
21+
//= require cssrelpreload
2122
//= require onloadCSS
2223
```
2324

lib/loadcss/rails/version.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module LoadCSS
22
module Rails
3-
VERSION = '1.2.0'
4-
LOADCSS_VERSION = '1.2.0'
3+
VERSION = '1.2.1'
4+
LOADCSS_VERSION = '1.2.1'
55
end
66
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*! CSS rel=preload polyfill. Depends on loadCSS function. [c]2016 @scottjehl, Filament Group, Inc. Licensed MIT */
2+
(function( w ){
3+
// rel=preload support test
4+
if( !w.loadCSS ){
5+
return;
6+
}
7+
var rp = loadCSS.relpreload = {};
8+
rp.support = function(){
9+
try {
10+
return w.document.createElement( "link" ).relList.supports( "preload" );
11+
} catch (e) {
12+
return false;
13+
}
14+
};
15+
16+
// loop preload links and fetch using loadCSS
17+
rp.poly = function(){
18+
var links = w.document.getElementsByTagName( "link" );
19+
for( var i = 0; i < links.length; i++ ){
20+
var link = links[ i ];
21+
if( link.rel === "preload" && link.getAttribute( "as" ) === "style" ){
22+
w.loadCSS( link.href, link );
23+
link.rel = null;
24+
}
25+
}
26+
};
27+
28+
// if link[rel=preload] is not supported, we must fetch the CSS manually using loadCSS
29+
if( !rp.support() ){
30+
rp.poly();
31+
var run = w.setInterval( rp.poly, 300 );
32+
if( w.addEventListener ){
33+
w.addEventListener( "load", function(){
34+
w.clearInterval( run );
35+
} );
36+
}
37+
if( w.attachEvent ){
38+
w.attachEvent( "onload", function(){
39+
w.clearInterval( run );
40+
} )
41+
}
42+
}
43+
}( this ));

0 commit comments

Comments
 (0)