JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 48250
  • Score
    100M100P100Q166447F
  • License ISC

Webpack plugin for using service workers

Package Exports

  • sw-precache-webpack-plugin

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sw-precache-webpack-plugin) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

sw-precache-webpack-plugin

NPM

Webpack plugin for using service workers. Will generate a service worker file using sw-precache and add it to your build directory.

Install

npm install --save-dev sw-precache-webpack-plugin

Usage

var path = require('path');
var SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin')


module.exports = {
  context: __dirname,

  entry: {
    main: path.resolve(__dirname, 'src/index'),
  },

  output: {
    path: path.resolve(__dirname, 'src/bundles/'),
    filename: '[name]-[hash].js',
  },

  plugins: [
    new SWPrecacheWebpackPlugin({
      cacheId: 'my-project-name',
      filename: 'my-service-worker.js',
    }),
  ]
}

This will generate a new service worker at src/bundles/my-service-worker.js. Then you would just register it in your application:

(function() {
  if('serviceWorker' in navigator) {
    navigator.serviceWorker  
             .register('/my-service-worker.js')
             .then(function() {
               console.log('Service worker registered');
             })
             .catch(function(error) {
               console.error('Error registering service worker: ', error);
             });
  }
})();