Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit 31eddda

Browse files
committed
restore state from 1.0 after some confusion around an outdated spec - 1.1 will be deprecated.
1 parent 9ff4ae2 commit 31eddda

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ Browsers are beginning to support a standard markup pattern for loading CSS (and
2525
The markup for referencing your CSS file looks like this:
2626

2727
```html
28-
<link rel="preload" href="path/to/mystylesheet.css" as="stylesheet" onload="this.rel='stylesheet'">
28+
<link rel="preload" href="path/to/mystylesheet.css" as="style" onload="this.rel='stylesheet'">
2929
<noscript><link rel="stylesheet" href="path/to/mystylesheet.css"></noscript>
3030
```
3131

32-
Since `rel=preload` does not apply the CSS on its own (it merely fetches it), there is an `onload` handler on the `link` that will do that for us as soon as the CSS finishes loading. Since this step requires JavaScript, you may want to include an ordinary reference to your CSS file as well, using a `noscript` element to ensure it only applies in non-JavaScript settings.
32+
Since `rel=preload` does not apply the CSS on its own (it merely fetches it), there is an `onload` handler on the `link` that will do that for us as soon as the CSS finishes loading. Since this step requires JavaScript, you may want to include an ordinary reference to your CSS file as well, using a `noscript` element to ensure it only applies in non-JavaScript settings.
3333

34-
With that markup in the `head` of your page, include the [loadCSS script](https://github.com/filamentgroup/loadCSS/blob/master/src/onloadCSS.js), as well as the [loadCSS rel=preload polyfill script](https://github.com/filamentgroup/loadCSS/blob/master/src/cssrelpreload.js) in your page (inline to run right away, or in an external file if the CSS is low-priority).
34+
With that markup in the `head` of your page, include the [loadCSS script](https://github.com/filamentgroup/loadCSS/blob/master/src/onloadCSS.js), as well as the [loadCSS rel=preload polyfill script](https://github.com/filamentgroup/loadCSS/blob/master/src/cssrelpreload.js) in your page (inline to run right away, or in an external file if the CSS is low-priority).
3535

3636
No further configuration is needed, as these scripts will automatically detect if the browsers supports `rel=preload`, and if it does not, they will find CSS files referenced in the DOM and preload them using loadCSS. In browsers that natively support `rel=preload`, these scripts will do nothing, allowing the browser to load and apply the asynchronous CSS (note the `onload` attribute above, which is there to set the `link`'s `rel` attribute to stylesheet once it finishes loading in browsers that support `rel=preload`).
3737

src/cssrelpreload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
var links = w.document.getElementsByTagName( "link" );
1919
for( var i = 0; i < links.length; i++ ){
2020
var link = links[ i ];
21-
if( link.rel === "preload" && link.getAttribute( "as" ) === "stylesheet" ){
21+
if( link.rel === "preload" && link.getAttribute( "as" ) === "style" ){
2222
w.loadCSS( link.href, link );
2323
link.rel = null;
2424
}

test/preload.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<title>CSS link[rel=preload] Polyfill</title>
55
<meta charset="utf-8">
66

7-
<link rel="preload" href="http://scottjehl.com/css-temp/slow.php" as="stylesheet" onload="this.rel='stylesheet'">
7+
<link rel="preload" href="http://scottjehl.com/css-temp/slow.php" as="style" onload="this.rel='stylesheet'">
88
<noscript><link rel="stylesheet" href="http://scottjehl.com/css-temp/slow.php"></noscript>
99
<script>
1010
/*! loadCSS: load a CSS file asynchronously. [c]2016 @scottjehl, Filament Group, Inc. Licensed MIT */
@@ -109,7 +109,7 @@
109109
var links = w.document.getElementsByTagName( "link" );
110110
for( var i = 0; i < links.length; i++ ){
111111
var link = links[ i ];
112-
if( link.rel === "preload" && link.getAttribute( "as" ) === "stylesheet" ){
112+
if( link.rel === "preload" && link.getAttribute( "as" ) === "style" ){
113113
w.loadCSS( link.href, link );
114114
link.rel = null;
115115
}
@@ -154,7 +154,7 @@ <h1>Async CSS w/ link[rel=preload]</h1>
154154
<p>This is a test page that references a slow-loading stylesheet using the new standard <code>link[rel=preload]</code>.<p>
155155

156156
<p>That markup looks like this:</p>
157-
<pre><code>&lt;link rel="preload" href="http://scottjehl.com/css-temp/slow.php" as="stylesheet" onload="this.rel='stylesheet'"&gt;</code></pre>
157+
<pre><code>&lt;link rel="preload" href="http://scottjehl.com/css-temp/slow.php" as="style" onload="this.rel='stylesheet'"&gt;</code></pre>
158158

159159
<p>In supporting browsers (such as Chrome Canary at time of writing), this markup will cause the browser to fetch the CSS file in an asynchronous, non-render-blocking manner, and once loaded, its onload event handler will change its rel property to "stylesheet" causing it to apply visibly in the page (the CSS file will color the page background green once loaded).</p>
160160

test/qunit/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta id="before-test" name=""></meta>
77
<!-- Load local QUnit. -->
88
<link rel="stylesheet" href="./libs/qunit/qunit.css" media="screen">
9-
<link rel="preload" href="files/preloadtest.css" id="preloadtest" as="stylesheet" onload="this.rel='stylesheet'">
9+
<link rel="preload" href="files/preloadtest.css" id="preloadtest" as="style" onload="this.rel='stylesheet'">
1010
<script src="./libs/qunit/qunit.js"></script>
1111
<!-- Load local lib and tests. -->
1212
<script src="../../src/loadCSS.js" id="loadCSS"></script>

0 commit comments

Comments
 (0)