Permalink
Browse files

better readme instructions

  • Loading branch information...
Scott Jehl
Scott Jehl committed Feb 13, 2017
1 parent 25e0e1c commit 051abca704fde0f2aeb820bd2ff1148fe1ebb4c6
Showing with 39 additions and 111 deletions.
  1. +39 −111 README.md
View
150 README.md
@@ -1,47 +1,70 @@
# loadCSS
A function for loading CSS asynchronously
[c]2016 @scottjehl, [Filament Group, Inc.](https://www.filamentgroup.com/)
[c]2017 @scottjehl, [Filament Group, Inc.](https://www.filamentgroup.com/)
Licensed MIT
## Why loadCSS?
Referencing CSS files with `link[rel=stylesheet]` or `@import` will cause most browsers to delay page rendering while the stylesheet loads. This is desirable in many cases, but when loading stylesheets that are not critical to the initial rendering of a page, loadCSS (and upcoming web standards mentioned below) allows you to load stylesheets asynchronously, so they don’t block page rendering.
Referencing CSS stylesheets with `link[rel=stylesheet]` or `@import` causes browsers to delay page rendering while a stylesheet loads. When loading stylesheets that are not critical to the initial rendering of a page, this blocking behavior is undesirable. The new `<link rel="preload">` standard enables us to load stylesheets asynchronously, without blocking rendering, and loadCSS provides a JavaScript polyfill for that feature to allow it to work across browsers, as well as providing its own JavaScript method for loading stylesheets.
* Latest release: https://github.com/filamentgroup/loadCSS/releases
* NPM: https://www.npmjs.com/package/fg-loadcss
## Basic Usage
## Recommended loadCSS Usage, explained
With the [`loadCSS` function](https://github.com/filamentgroup/loadCSS/blob/master/src/loadCSS.js) referenced in your page, simply call the `loadCSS` function and pass it a stylesheet URL:
LoadCSS is designed for loading CSS that is **not critical** to the initial rendering of the page, and desirable to load in an asynchronous manner. (_For the critical CSS rules, we recommend either inlining that CSS in a `style` element, or referencing it externally and server-pushing it using http/2. [Read more here](https://www.filamentgroup.com/lab/modernizing-delivery.html)_)
```css
loadCSS( "path/to/mystylesheet.css" );
The standard markup pattern for loading files asynchronously is: `<link rel="preload">` ([W3C Spec](https://www.w3.org/TR/2015/WD-preload-20150721/)). We recommend using this markup pattern to reference your non-critical CSS files. `loadCSS` and its rel=preload polyfill are designed to enable this markup to work in browsers that don't yet support this feature.
For each CSS file you'd like to load asynchronously, use a `link` element like this:
```html
<link rel="preload" href="path/to/mystylesheet.css" as="style">
```
The code above will insert a new CSS stylesheet `link` *after* the last stylesheet or script that loadCSS finds in the page, and the function will return a reference to that `link` element, should you want to use it later in your script. Multiple calls to loadCSS will reference CSS files in the order they are called, but keep in mind that they may finish loading in a different order than they were called depending on network conditions.
In browsers that support it, the `rel=preload` attribute will cause the browser to fetch the stylesheet, but it will not **apply** the CSS once it is loaded (it merely fetches it). To address this, we recommend using an `onload` attribute on the `link` that will do that for us as soon as the CSS finishes loading.
## Recommended Usage Pattern
```html
<link rel="preload" href="path/to/mystylesheet.css" as="style" onload="this.rel='stylesheet'">
```
Browsers are beginning to support a standard markup pattern for loading CSS (and other file types) asynchronously: `<link rel="preload">` ([W3C Spec](https://www.w3.org/TR/2015/WD-preload-20150721/)). We recommend using this markup pattern to reference any CSS files that should be loaded asynchronously, and using `loadCSS` to polyfill support browsers that don't yet support this new feature.
This step requires JavaScript to be enabled, so we recommend including an ordinary reference to your stylesheet inside a `noscript` element as a fallback.
```html
<link rel="preload" href="path/to/mystylesheet.css" as="style" onload="this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="path/to/mystylesheet.css"></noscript>
```
The markup for referencing your CSS file looks like this:
After linking to your asynchronous stylesheet(s) this way, include the [loadCSS script](https://github.com/filamentgroup/loadCSS/blob/master/src/loadCSS.js), as well as the [loadCSS rel=preload polyfill script](https://github.com/filamentgroup/loadCSS/blob/master/src/cssrelpreload.js) in your page. These can be inlined or linked and http/2-pushed if possible.
Here's how they would look inlined in the page:
```html
<link rel="preload" href="path/to/mystylesheet.css" as="style" onload="this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="path/to/mystylesheet.css"></noscript>
<script>
/*! loadCSS. [c]2017 Filament Group, Inc. MIT License */
(function(){ ... }());
/*! loadCSS rel=preload polyfill. [c]2017 Filament Group, Inc. MIT License */
(function(){ ... }());
</script>
```
These scripts will automatically detect if a browser supports `rel=preload`. 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`). In browsers that do not support `rel=preload`, they will find CSS files referenced this way in the DOM and load and apply them asynchronously using the loadCSS function.
Note: regardless of whether the browser supports `rel=preload` or not, a CSS file will be referenced from the same location in the source order as your original `link` element. Keep this in mind, as you may want to place the `link` in a particular location in your `head` element so that the CSS loads with an expected cascade order. Also, any `media` attribute value on the original link element will be retained when the polyfill is in play.
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.
You can view a demo of this `rel=preload` pattern here: http://filamentgroup.github.io/loadCSS/test/preload.html
With that markup in the `head` of your page, include the [loadCSS script](https://github.com/filamentgroup/loadCSS/blob/master/src/loadCSS.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).
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`).
## Manual CSS loading with loadCSS
Note: regardless of whether the browser supports `rel=preload` or not, your CSS file will be referenced from the same spot in the source order as the original `link` element. Keep this in mind, as you may want to place the `link` in a particular location in your `head` element so that the CSS loads with an expected cascade order.
The [loadCSS.js](https://github.com/filamentgroup/loadCSS/blob/master/src/loadCSS.js) file exposes a global `loadCSS` function that you can call to load CSS files programmatically, when needed.
You can view a demo of this `rel=preload` pattern here: http://filamentgroup.github.io/loadCSS/test/preload.html
```css
loadCSS( "path/to/mystylesheet.css" );
```
The code above will insert a new CSS stylesheet `link` *after* the last stylesheet or script that it finds in the page, and the function will return a reference to that `link` element, should you want to reference it later in your script. Multiple calls to loadCSS will reference CSS files in the order they are called, but keep in mind that they may finish loading in a different order than they were called.
## Function API
@@ -74,102 +97,7 @@ onloadCSS( stylesheet, function() {
### Browser Support
loadCSS attempts to load a css file asynchronously in any JavaScript-capable browser. However, some older browsers will block rendering while the stylesheet is loading. This table outlines css loading support and async loading support.
<table>
<tr>
<th>Browser</th>
<th>CSS Loads Successfully</th>
<th>CSS Loads without Blocking Render</th>
</tr>
<tr>
<th>Chrome Mac (latest and many recent versions)</th>
<th>Yes</th>
<th>Yes</th>
</tr>
<tr>
<th>Firefox Desktop (latest and many recent versions)</th>
<th>Yes</th>
<th>Yes</th>
</tr>
<tr>
<th>Opera Mac (latest and many recent versions)</th>
<th>Yes</th>
<th>Yes</th>
</tr>
<tr>
<th>Safari Mac (latest and many recent versions)</th>
<th>Yes</th>
<th>Yes</th>
</tr>
<tr>
<th>Safari iOS (latest and many recent versions)</th>
<th>Yes</th>
<th>Yes</th>
</tr>
<tr>
<th>Chrome Android 5.x</th>
<th>Yes</th>
<th>Yes</th>
</tr>
<tr>
<th>Chrome Android 4.x</th>
<th>Yes</th>
<th>Yes</th>
</tr>
<tr>
<th>Android Browser 2.3</th>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<th>Kindle Fire HD</th>
<th>Yes</th>
<th>Yes</th>
</tr>
<tr>
<th>Windows Phone IE 8.1</th>
<th>Yes</th>
<th>Yes</th>
</tr>
<tr>
<th>IE 11</th>
<th>Yes</th>
<th>Yes</th>
</tr>
<tr>
<th>IE 10</th>
<th>Yes</th>
<th>Yes</th>
</tr>
<tr>
<th>IE 9</th>
<th>Yes</th>
<th>Yes</th>
</tr>
<tr>
<th>IE 8</th>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<th>IE 7</th>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<th>IE 6</th>
<th>Yes</th>
<th>No</th>
</tr>
</table>
### Usage Tips
We typically use `loadCSS` to load CSS files that are non-critical to the initial rendering of a page. See the [EnhanceJS project Readme](https://github.com/filamentgroup/enhance#enhancejs) for examples of how we typically use it to improve page loading performance (note: the newest `rel=preload` pattern is not yet in that readme, but the concepts are the same).
loadCSS attempts to load a css file asynchronously in any JavaScript-capable browser. However, some older browsers such as Internet Explorer 8 and older will block rendering while the stylesheet is loading. This merely means that the stylesheet will load as if you referenced it with an ordinary link element.
#### Contributions and bug fixes

0 comments on commit 051abca

Please sign in to comment.