|
4 | 4 | <title>Test for link[rel=preload]</title>
|
5 | 5 | <meta charset="utf-8">
|
6 | 6 |
|
7 |
| - |
8 |
| - |
9 |
| - <link rel="preload" href="http://scottjehl.com/css-temp/slow.php" as="stylesheet" id="asyncCSS" onload="this.rel='stylesheet'"> |
10 |
| - |
| 7 | + <link rel="preload" href="http://scottjehl.com/css-temp/slow.php" as="style" onload="this.rel='stylesheet'"> |
11 | 8 | <script>
|
12 | 9 |
|
13 |
| - // link rel=preload support test via https://lists.w3.org/Archives/Public/public-whatwg-archive/2015Apr/0013.html |
14 |
| - function preloadSupported() { |
15 |
| - var link = document.createElement('link'); |
16 |
| - link.rel = 'PRELOAD'; |
17 |
| - return link.rel == 'preload'; |
| 10 | + /* CSS rel=preload polyfill */ |
| 11 | + (function( w ){ |
| 12 | + // rel=preload support test |
| 13 | + function support(){ |
| 14 | + try { |
| 15 | + return w.document.createElement("link").relList.supports( "preload" ); |
| 16 | + } catch (e) {} |
18 | 17 | }
|
19 |
| - |
20 |
| - /*! |
21 |
| - loadCSS: load a CSS file asynchronously. |
22 |
| - [c]2015 @scottjehl, Filament Group, Inc. |
23 |
| - Licensed MIT |
24 |
| - */ |
25 |
| - (function(w){ |
26 |
| - "use strict"; |
27 |
| - /* exported loadCSS */ |
28 |
| - var loadCSS = function( href, before, media ){ |
29 |
| - // Arguments explained: |
30 |
| - // `href` [REQUIRED] is the URL for your CSS file. |
31 |
| - // `before` [OPTIONAL] is the element the script should use as a reference for injecting our stylesheet <link> before |
32 |
| - // By default, loadCSS attempts to inject the link after the last stylesheet or script in the DOM. However, you might desire a more specific location in your document. |
33 |
| - // `media` [OPTIONAL] is the media type or query of the stylesheet. By default it will be 'all' |
34 |
| - var doc = w.document; |
35 |
| - var ss = doc.createElement( "link" ); |
36 |
| - var newMedia = media || "all"; |
37 |
| - var ref; |
38 |
| - if( before ){ |
39 |
| - ref = before; |
40 |
| - } |
41 |
| - else { |
42 |
| - var refs = ( doc.body || doc.getElementsByTagName( "head" )[ 0 ] ).childNodes; |
43 |
| - ref = refs[ refs.length - 1]; |
44 |
| - } |
45 |
| - |
46 |
| - var sheets = doc.styleSheets; |
47 |
| - ss.rel = "stylesheet"; |
48 |
| - ss.href = href; |
49 |
| - // temporarily set media to something inapplicable to ensure it'll fetch without blocking render |
50 |
| - ss.media = "only x"; |
51 |
| - |
52 |
| - |
53 |
| - // Inject link |
54 |
| - // Note: the ternary preserves the existing behavior of "before" argument, but we could choose to change the argument to "after" in a later release and standardize on ref.nextSibling for all refs |
55 |
| - // Note: `insertBefore` is used instead of `appendChild`, for safety re: http://www.paulirish.com/2011/surefire-dom-element-insertion/ |
56 |
| - ref.parentNode.insertBefore( ss, ( before ? ref : ref.nextSibling ) ); |
57 |
| - // A method (exposed on return object for external use) that mimics onload by polling until document.styleSheets until it includes the new sheet. |
58 |
| - var onloadcssdefined = function( cb ){ |
59 |
| - var resolvedHref = ss.href; |
60 |
| - var i = sheets.length; |
61 |
| - while( i-- ){ |
62 |
| - if( sheets[ i ].href === resolvedHref ){ |
63 |
| - return setTimeout(cb); |
64 |
| - } |
65 |
| - } |
66 |
| - setTimeout(function() { |
67 |
| - onloadcssdefined( cb ); |
68 |
| - }); |
69 |
| - }; |
70 |
| - |
71 |
| - // once loaded, set link's media back to `all` so that the stylesheet applies once it loads |
72 |
| - if( ss.addEventListener ){ |
73 |
| - ss.addEventListener( "load", function(){ |
74 |
| - this.media = newMedia; |
75 |
| - }); |
| 18 | + // loop preload links and fetch using loadCSS |
| 19 | + function poly(){ |
| 20 | + var links = w.document.getElementsByTagName( "link" ); |
| 21 | + for( var i = 0; i < links.length; i++ ){ |
| 22 | + if( links[ i ].getAttribute( "rel" ) === "preload" ){ |
| 23 | + w.loadCSS( links[ i ].href, links[ i ] ); |
| 24 | + links[ i ].rel = null; |
76 | 25 | }
|
77 |
| - ss.onloadcssdefined = onloadcssdefined; |
78 |
| - onloadcssdefined(function() { |
79 |
| - if( ss.media !== newMedia ){ |
80 |
| - ss.media = newMedia; |
81 |
| - } |
82 |
| - }); |
83 |
| - return ss; |
84 |
| - }; |
85 |
| - // commonjs |
86 |
| - if( typeof module !== "undefined" ){ |
87 |
| - module.exports = loadCSS; |
88 | 26 | }
|
89 |
| - else { |
90 |
| - w.loadCSS = loadCSS; |
91 |
| - } |
92 |
| - }( typeof global !== "undefined" ? global : this )); |
93 |
| - |
94 |
| - |
95 |
| - |
| 27 | + } |
96 | 28 | // if link[rel=preload] is not supported, we must fetch the CSS manually using loadCSS
|
97 |
| - if( !preloadSupported() ){ |
98 |
| - loadCSS( asyncCSS.href ); |
| 29 | + if( !support() ){ |
| 30 | + poly(); |
| 31 | + var run = w.setInterval( poly, 300 ); |
| 32 | + if( w.addEventListener ){ |
| 33 | + w.addEventListener( "load", function(){ |
| 34 | + w.clearInterval( run ); |
| 35 | + } ) |
| 36 | + } |
99 | 37 | }
|
| 38 | + }( this )); |
100 | 39 |
|
101 | 40 | </script>
|
102 | 41 | </head>
|
|
0 commit comments