create-sw

v0.0.1

npm version

create-sw

A tool to generate robust Service Worker for your application. Refactored and improved from sw-precache

Install

npm i -S create-sw

or you can install it globally:

npm i -g create-sw

Usage

1、build service-worker.js

This tool provides a cli tool to help you build your own service-worker.js

create-sw --config=sw-config.js

sw-config.js has the same options in sw-precache and some unique options:

externals [Array]

An array contains the external resources url that you want to precache.

// sw-config.js
module.exports = {
  // ...
  externals: [
      'https://cdn.bootcss.com/jquery/3.2.1/jquery.js',
      'https://cdn.bootcss.com/bootstrap/4.0.0-beta/css/bootstrap.css'
  ]
};

minify [Boolean]

An option to deside if the output sw.js is minified. Defaults to false

// sw-config.js
module.exports = {
  // ...
  minify: true
};

2、runtime

This tool also provides a runtime for you to install Service Worker and set a callback to the Service Worker lifecycle:

// In your Javascript code
var runtime = require('create-sw/runtime');
runtime.install('/path/to/sw.js', {
    onInstalled() {
        console.log('onInstalled')
    },
    onUpdated() {
        console.log('onUpdated')
    },
    onUpdateFailed() {
        console.log('onUpdateFailed')
    }
})

License

MIT

Metadata

Downloads

Maintainers