npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

service-worker-precache-webpack-plugin

v1.3.5

Published

create service worker file by webpack manifest and sw precache

Downloads

407

Readme

service-worker-precache-webpack-plugin

NPM version build status Test coverage David deps npm download

create service worker javascript file and manifest by webpack manifest and sw-precache, you can use with service-worker-register

Install

npm i service-worker-precache-webpack-plugin --save-dev

Usage

create sw-precache-webpack-plugin service worker file

const ServiceWorkerWebpackPlugin = require('service-worker-precache-webpack-plugin');
module.exports = {
  plugins:[
    new ServiceWorkerWebpackPlugin({
        cacheId: 'my-project-name',
        dontCacheBustUrlsMatching: /\.\w{8}\./,
        filename: 'service-worker.js',
        minify: true,
        navigateFallback: PUBLIC_PATH + 'index.html',
        staticFileGlobsIgnorePatterns: [/\.map$/, /sw-manifest\.json$/],
    })
  ]  
}

more configuration sw-precache-webpack-plugin

create sw-precache-webpack-plugin service worker file by webpack manifest

create service worker file based on the specified webpack entry

module.exports = {
  plugins: [
    new ServiceWorkerWebpackPlugin({
      prefix: 'sw',
      strategy: [
        {
          name: 'index',
          entry: 'index/index',
        },
        {
          name: 'category',
          entry: ['category/category', 'about/about'],
          options: {
            runtimeCaching: [
              {
                urlPattern: /^https:\/\/category\.com\/api/,
                handler: 'fastest'
              }
            ]
          }
        }
      ],
      manifest: 'config/manifest.json'
    }
  ]
}

support create an independent service worker file for webpack each entry

module.exports = {
    plugins:[
        new ServiceWorkerWebpackPlugin({
            prefix: 'sw',
            strategy: 'multiple'
        }
    ] 
}

Configuration

Extended Configuration

new ServiceWorkerWebpackPlugin(option);
  • option.prefix : {String}, optional - Service worker file prefix, default: sw. such as:
new ServiceWorkerWebpackPlugin({
    prefix: 'sw',
    filename: 'home.js'
});

The final generated file is sw-home.js

  • option.manifest {Object}, optional - The format is webpack-manifest-resource-plugin. when create service worker by webpack manifest, you must set this config. if config/manifest.json exists, will use this manifest config.

  • option.strategy {String|Array} - How to create service worker file by what strategy. The value optional : single, multiple, and Array config. default: single

single: create a service worker file by all webpack entry
multiple: create an independent service worker file for webpack each entry
array type: if the value is array, create service worker file based on the specified webpack entry
{
  strategy: [
    {
        name: 'index',
        entry: 'index/index',
    },
    {
        name: 'category',
        entry: ['category/category', 'about/about'],
        options: {
        runtimeCaching: [
            {
             urlPattern: /^https:\/\/category\.com\/api/,
             handler: 'fastest'
            }
        ]
      }
    }
 ]
}

the option.strategy[] array item config:

  • name: {String} - service worker file name.
  • entry: {String|Array} - the webpack entry name.
  • options: {Object}, optional - the sw-precache-webpack-plugin option.

Manifest

  • The plugin will create service worker manifest file, solve the service worker cache problem by manifest.
// sw-manifest.js
{
  "config": {
    "localPublicPath": "/public/",
    "publicPath": "/public/"
  },
  "service-worker.js": "/public/service-worker.567ddfd3.js"
}
const serviceWorkerRegister = require('service-worker-register');
// The service-worker.js name will get really url address by sw-mapping.json file
serviceWorkerRegister.register('service-worker.js');

Relation

fork sw-precache-webpack-plugin

License

MIT