PHPackages                             styletools/async - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. styletools/async

ActiveProject[Utility &amp; Helpers](/categories/utility)

styletools/async
================

A lightweight and high performance async CSS and script loader for frontend optimization.

2.0.4(4y ago)19483MITJavaScriptCI failing

Since May 18Pushed 4y ago1 watchersCompare

[ Source](https://github.com/pagespeed-pro/async)[ Packagist](https://packagist.org/packages/styletools/async)[ RSS](/packages/styletools-async/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (10)DependenciesVersions (14)Used By (0)

[![Build Status](https://camo.githubusercontent.com/729b9e8992ff0f2fac62d39a9e1d035d88084df6797546baa73f85f66f61d388/68747470733a2f2f7472617669732d63692e636f6d2f7374796c652d746f6f6c732f6173796e632e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/style-tools/async) [![Version](https://camo.githubusercontent.com/985c07f5124fc25cf201cb41c1d085f33e593ef7df10780a0bd6e604c72176af/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f7374796c652d746f6f6c732f6173796e632e737667)](https://github.com/style-tools/async/releases) [![npm version](https://camo.githubusercontent.com/5bb83d31e0df136828849e4cf822ee46f9b4a9a0c895d8ad5747f66d831ad0a6/68747470733a2f2f62616467652e667572792e696f2f6a732f2534307374796c652e746f6f6c732532466173796e632e737667)](http://badge.fury.io/js/%40style.tools%2Fasync) [![Latest Stable Version](https://camo.githubusercontent.com/b678fb8da95ec413173b59f5ddeb1edb2ffb132efeb24025ec7abb83cb0133a3/68747470733a2f2f706f7365722e707567782e6f72672f7374796c65746f6f6c732f6173796e632f762f737461626c652e706e67)](https://packagist.org/packages/styletools/async)

`$async` Async CSS and Script Loader
====================================

[](#async-async-css-and-script-loader)

A lightweight and high performance async CSS and script loader with state of the art features for frontend optimization (FEO).

### Install via npm

[](#install-via-npm)

```
npm install @style.tools/async --save
```

### Install via PHP Composer

[](#install-via-php-composer)

```
composer require styletools/async
```

Examples
--------

[](#examples)

```
$async([
   'sheet.css',
   'script.js'
]).then(function() { /* ready */ });
```

`$async` can be controlled from a HTML attribute which enables strict security.

```

```

The JSON configuration can be compressed to save size in the HTML ([online compressor](https://style.tools/async/)).

```

```

Documentation
-------------

[](#documentation)

Documentation is available on [docs.style.tools/async](https://docs.style.tools/async).

- [Online IIFE generator](https://style.tools/iife/)
- [JSON config editor and compressor](https://style.tools/async/)

Description
-----------

[](#description)

$async is designed as the ultimate CSS and script loader for modern frontend optimization (FEO). It provides state of the art features, the absolute best performance and the tiniest HTML footprint. $async supports all browsers including IE9+.

- 100% JSON control.
- Google Closure Compiler (GCC) with *Advanced mode* script compression (reliable and performant in all browsers).

### Modular

[](#modular)

$async is modular and easy to use: select only the features that are needed to achieve the tiniest script size.

- simply stitch pre-optimized GCC modules together for a performant IIFE. You can wrap the modules in [dist/](./dist/) into an IIFE, e.g. `!function(){/* stitched modules */}();`. Follow the module order in [package.json](./package.json).
- [Online IIFE generator](https://style.tools/iife/) (adds an extra GCC *Advanced mode* compression layer)
- [Node.js/CLI IIFE generator](https://github.com/style-tools/async-iife) (adds an extra GCC *Advanced mode* compression layer)
- PHP IIFE generator (available on request: )

### Chainable

[](#chainable)

```
$async
   .on('load',function(sheet, sheetEl){
      //  sheet.css or other-sheet.css loaded
   })
   .on('sheet-ref',function() { }) // sheet with ref-name loaded
   .on('sheet.css', function() {}); // sheet with href loaded
   .load({
      href: 'sheet.css',
      ref: 'sheet-ref'
   })
   .then(function() { }) // sheet.css loaded
   .load('other-sheet.css');
```

### Security

[](#security)

$async supports a strict Content-Security-Policy (CSP) and SRI security by using a HTML attribute on the script element. The `data-c` attribute accepts JSON config.

```

```

### Advanced download and exec/render timing

[](#advanced-download-and-execrender-timing)

$async provides advanced loading and timing techniques.

- control the insert target.
- time the download and/or exec/render using methods such as `requestAnimationFrame`, `requestIdleCallback` and [$lazy](https://github.com/style-tools/lazy) (Intersection Observer).
- dependency based loading.
- responsive `Media Query` based loading with cross-browser support for viewport changes.
- `just-in-time` loading using a custom javascript method.

```
$async(
   [
      "sheet.css",
      {
         href:"other-sheet.css",
         dependencies: ["sheet.css"], // wait for sheet.css via dependencies
         load_timing: {
            type: "lazy", // use $lazy for timing (Intersection Observer)
            config: [".selector-in-view", 0, "200px"], // visible within 200 pixels
         },
         ref: "other"
      },
      {
         href:"mobile-sheet.css",
         dependencies: "other", // dependency by ref
         target: {
            after: "meta[charset]" // control insert target
         },
         load_timing: {
            type: "media", // download stylesheet based on a media query (works with viewport changes, e.g. viewport rotate)
            media: "screen and (max-width: 600px)"
         }
      },
      {
         inline: "inline_script_with_timing_and_dependency();",
         ref: "inline-code"
      },
      {
         src: "script.js",
         exec_timing: "requestIdleCallback",
         dependencies: "inline-code"
      }
   ],
   /* global options: applied to all stylesheets */
   {
      // base directory for relative sheet URLs
      base: "/long/path/to/css/",

      // render timing: paint sheet with requestAnimationFrame
      render_timing: "requestAnimationFrame"
   }
)
.then(function() { /* ready */ });
```

### `just-in-time` loading

[](#just-in-time-loading)

```
$async(
   {
      href:"popup-css.css",
      load_timing: {
         type: "method", // trigger download using custom javascript method
         method: "load_popup_css"
      }
   },{
      src:"popup-script.js",
      load_timing: {
         type: "method",
         method: "load_popup_js"
      }
   }
);

// just-in-time loading
jQuery('button.popup').on('click', function() {

   // user clicks a button
   // load popup script/css just-in-time

    load_popup_css().then(function() {
      alert('popup CSS loaded');
    });

    load_popup_js().then(function() {
      alert('popup script loaded');
    });
});
```

### API's

[](#apis)

$async provides API's for access to the dependency resolver and timing methods.

```
// dependency resolver
$async.dependencies(['name'], function() { /* dependency loaded */ });

// timing method
$async.time("requestAnimationFrame", function() { /* callback */ });
$async.time(48, function() {}); // the same using the JSON compression index key for 'requestAnimationFrame'
```

### `localStorage` cache

[](#localstorage-cache)

$async enables to load stylesheets and script from `localStorage` or [Cache API](https://developer.mozilla.org/en-US/docs/Web/API/Cache) cache which is much faster than browser cache.

For a demo, see [css-art.com](https://css-art.com).

```
$async({
   href: "sheet.css",
   cache: {
      type: "localstorage",
      max_size: 10000, // cache only
