1.0.1 • Published 4 years ago

fast-service-worker v1.0.1

Weekly downloads
17
License
zonebond
Repository
-
Last release
4 years ago

fast-service-worker 1.0.0 beta

Plugin that simplifies creation of service-worker js files to cache or prefetch asstes and statics data.

Install

npm install --save-dev fast-service-worker

or

yarn add fast-service-worker --save-dev

Usage

In html page template

<script>
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('./fast-service-worker.js', {
        scope: './'
      })
      .then(function (registration) {
        console.log('Service Worker Registered', registration.scope);
      })
      .catch(function (error) {
        console.log('Service Worker Failed to Register', error);
      });
  } else {
    console.log('Service Worker are not supported.');
  }
</script>

In Webpack configuration file

const SWPluign = require('fast-service-worker');

new SWPluign({
  inject2HTML: 'index.html',
  filename: 'fast-service-worker.js',
  // name: String, ’%pref%‘ is a dynamic placeholder,it changes when compiled.
  name: `${name} - 1.0.0%pref%`,
  // prefetch: Array<URLString> or [Function - running into that resolve assets of webpack process]
  prefetch: function(assets) {
    console.log('>>>>', Object.keys(assets).filter(a => /(\.chunk)|(\.min)?\.(css|js)\./.test(a)));
    return [
      './',
      ...(Object.keys(assets).filter(asset => !/(\.chunk)|(\.min)?\.(css|js)$/.test(asset) && !/\.map$/.test(asset)))
    ]
  },
  // cachePatterns: Array<RegExp> or Function
  cachePatterns: [
    /(\.chunk)|(\.min)?\.(css|js)$/
  ],
  // cachePatterns: Array<RegExp> or Function
  racerPatterns: []
}),