@jswork/sw-runtime
v1.0.13
Published
Service worker runtime for workbox.
Downloads
68
Maintainers
Readme
sw-runtime
Service worker runtime for workbox.
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
- unpkg/jsdelivr cdn zero cache?(not ok)
- auto update interval(ok)
license
Code released under the MIT license.