Skip to content
Gaël Métais edited this page Jul 29, 2016 · 2 revisions

If you followed the installation documentation, then you already used the API when you copied sw-standalone.js. You can jump to the Settings chapter.

Use the API

sw-delta is a front-end npm package. This means that it needs to be required and called. The API is very basic. Here is an example:

// Require the package
var SwDelta = require('sw-delta');

// Configuration (see the settings chapter below)
var settings = {
    files: [
        '/assets/*.js',
        '/assets/*.css'
    ],
    removeCookies: true,
    indexedDB: {
        name: 'sw-delta',
        version: 1
    }
};

// Create an instance of SwDelta and inject the settings
var swDeltaInstance = new SwDelta(settings);

// Sw-delta doesn't automatically listen to the `fetch` event.
// You need to do it like this:
self.addEventListener('fetch', swDeltaInstance.onFetch);

Settings

[Array of Strings] files

Enter here the assets that will be controlled by sw-delta. Each outgoing request from the browser will be matched with the minimatch algorithm. Both absolute and relative urls are accepted.

[Boolean] removeCookies

There's generally no need to send cookies when requesting static assets. By turning removeCookies to true (default is false), they will be removed to save some upload bandwidth.

[Object] indexedDB

You normally don't have to change this. This object contains the name of the database that is used as a caching storage, as well as its version number. Be careful, version numbers will be changed by the sw-delta project, so this should only be used for debugging purpose, by using high numbers (>1000 for example). Only integers are accepted as version numbers.

Clone this wiki locally