@jswork/sw-runtime
TypeScript icon, indicating that this package has built-in type declarations

1.0.10 • Public • Published

sw-runtime

Service worker runtime for workbox.

version license size download

installation

npm install @jswork/sw-runtime

skipWaiting

// case1: skipWaiting true: will be update immediately
module.exports = {
  maximumFileSizeToCacheInBytes: 100 * 1024 * 1024,
  globDirectory: 'build/',
  globPatterns: ['**/*.{js,png,json,txt,css,map}'],
  globIgnores: ['*/fallback.js'],
  skipWaiting: true,
  clientsClaim: true,
  //...
};

// case2: skipWaiting false: will be update when you click sw-tips `reload` button
module.exports = {
  maximumFileSizeToCacheInBytes: 100 * 1024 * 1024,
  globDirectory: 'build/',
  globPatterns: ['**/*.{js,png,json,txt,css,map}'],
  globIgnores: ['*/fallback.js'],
  skipWaiting: false,
  clientsClaim: false,
  //...
};

usage

import SwRuntime from '@jswork/sw-runtime';

// App Did Mount, config sw
window.onload = function () {
  SwRuntime.install({
    autoUpdate: true,
    autoUpdateInterval: 5 * 1000,
    onAutoUpdate: function () {
      console.log('SW Event:', 'onAutoUpdate');
    },
    onUpdateReady: function ({ context }) {
      console.log('SW Event:', 'onUpdateReady');
      setTimeout(function () {
        console.log('SW Event:', 'do applyUpdate');
        // Need to reload page, or it will be pending.
        context.applyUpdate().then(()=>{
          window.location.reload();
        });
      }, 1000);
    },
  });
};

todos

  1. unpkg/jsdelivr cdn zero cache?(not ok)
  2. auto update interval(ok)

license

Code released under the MIT license.

Package Sidebar

Install

npm i @jswork/sw-runtime

Weekly Downloads

1

Version

1.0.10

License

MIT

Unpacked Size

66.1 kB

Total Files

13

Last publish

Collaborators

  • afeiship