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

webpack-pwa-sw-plugin

v1.0.0

Published

A Webpack plugin to easily add PWA and Service Worker support to your application with no configuration.

Downloads

21

Readme

WebpackPwaSwPlugin

Overview

WebpackPwaSwPlugin is a highly customizable Webpack plugin that simplifies the integration of service workers in Progressive Web Applications (PWAs). It facilitates the auto-registration of service workers and enables various caching strategies, including runtime caching, background synchronization, and broadcast updates. With this plugin, users can effortlessly enhance their web applications with offline capabilities and ensure their assets are served efficiently.

Features

  • Automatic Service Worker Registration: Automatically registers a service worker in your application.
  • Customizable Runtime Caching: Supports multiple caching strategies for specific URL patterns, including CacheFirst, StaleWhileRevalidate, NetworkFirst, and NetworkOnly.
  • Manifest Injection: Automatically injects the manifest.json file into your HTML.
  • Background Sync: Supports background synchronization for offline scenarios.
  • Broadcast Updates: Allows synchronization of updates between different tabs or clients using BroadcastUpdatePlugin.
  • Versioned Caches: Automatically handles cache versioning and cache cleanup.
  • Auto-Reload on Reconnect: Optionally reloads the application when the network connection is restored.

Installation

npm install --save-dev webpack-pwa-sw-plugin workbox-webpack-plugin webpack-virtual-modules

Usage

In your webpack.config.js:

const WebpackPwaSwPlugin = require("webpack-pwa-sw-plugin");

module.exports = {
  // Other webpack configuration
  plugins: [
    new WebpackPwaSwPlugin({
      manifest: {
        name: "My PWA",
        short_name: "PWA",
        icons: [{ src: "/icon.png", sizes: "192x192", type: "image/png" }],
        start_url: ".",
        display: "standalone",
        background_color: "#ffffff",
        theme_color: "#000000",
      },
      workboxOptions :{
        // Other workbox options for InjectManifest. visit : https://developer.chrome.com/docs/workbox/modules/workbox-webpack-plugin#injectmanifest_plugin
      }
      runtimeCaching: [
        {
          urlPattern: /\/api\//,
          handler: "NetworkFirst",
          options: {
            cacheName: "api-cache",
            expiration: {
              maxEntries: 50,
              maxAgeSeconds: 60 * 60 * 24, // 1 day
            },
            syncStrategies: ["broadcastUpdate"],
          },
        },
      ],
      autoRegisterServiceWorker: true,
      reloadOnConnectionRestored: true,
      showPromptOnUpdate: true,
      promptText: "New content is available, reload now?",
      onNeedRefresh : () => { console.log("Need to refresh") }
    }),
  ],
};

Configuration Options

manifest

An object that represents your PWA's manifest file. This is automatically injected into the build output as manifest.json.

workboxOptions

Additional options to pass to the InjectManifest plugin

reloadOnConnectionRestored

Enables automatic reloading of the application when the internet connection is restored after going offline.

showPromptOnUpdate

If set to true, the user will be shown a confirmation prompt to reload the page when a new version of the service worker is available.

promptText

Customizes the prompt text shown when showPromptOnUpdate is enabled.

onNeedRefresh

Custom function to run when a new service worker is ready to activate.

runtimeCaching

An array of objects defining runtime caching strategies for specific URL patterns.This plugin option supports various caching strategies for different resource types. The configuration is defined in runtimeCachingConfig.js. It includes built-in patterns for Google Fonts, static assets, and APIs. You can customize it by providing your own runtime caching rules.

  • urlPattern: A regex pattern or a function to match requests for which this caching strategy should apply.

  • handler: The caching strategy to use, can be one of the following:

    1. CacheFirst: Prioritizes the cache over the network. Useful for static assets.

    2. StaleWhileRevalidate: Serves cached content while updating the cache in the background. Useful for frequently changing resources like JavaScript and CSS files.

    3. NetworkFirst: Tries the network first, falls back to cache if the network is unavailable.

    4. NetworkOnly: Uses the network exclusively, no caching. Each object includes:

  • options: Additional options for each strategy, including cache names, expiration rules, and plugins.

(Unique to this plugin is the syncStrategies configuration):

  • syncStrategies: An array that can include the following two options:

    • backgroundSync: Ensures that failed POST requests are retried when the app is back online using the BackgroundSyncPlugin.
    • broadcastUpdate: Updates all open tabs or clients when a cached response is updated by utilizing the BroadcastUpdatePlugin.

Example of syncStrategies:

{
  urlPattern: /\/api\//,
  handler: 'NetworkFirst',
  options: {
    cacheName: 'api-cache',
    syncStrategies: ['backgroundSync', 'broadcastUpdate'],
    expiration: {
      maxEntries: 16,
      maxAgeSeconds: 60 * 60 * 24, // 1 day
    },
  },
}

Service Worker Registration

The service worker registration script (serviceWorkerRegistration.js) is automatically injected into your Webpack entries. The plugin handles:

  • Automatic service worker registration using the Workbox window.
  • Connection status monitoring to reload the app when the connection is restored if offline.
  • Automatic cache management using precacheAndRoute from Workbox.

License

This project is licensed under the MIT License.

Support

If you encounter any issues or have any questions, feel free to open an issue on the GitHub repository.

Acknowledgments

  • Thanks to the JavaScript & Webpack community for their valuable feedback and contributions.

Contact

For any inquiries or support, you can reach out to the maintainer at [email protected].

Happy Coding!