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

eleventy-plugin-pwa-v2

v1.0.1

Published

An Eleventy plugin to generate service worker for PWA using Google Workbox, compatible with Eleventy v2.x

Downloads

461

Readme

eleventy-plugin-pwa-v2

An Eleventy plugin to generate service worker, compatible with Eleventy v2.0. Using Google Workbox v6 to generate service-worker.js based on your dir.output.

This is an independent fork of @pkvach/eleventy-plugin-pwa, originally at version 1.4.0.

Compared to previous forks, this plugin relies on eleventy.after event rather than monkey-patching the Eleventy installation.

Demo

Demo repository (Eleventy starter template with the plugin): https://github.com/lwojcik/eleventy-plugin-pwa-v2-demo

Demo site: https://eleventy-plugin-pwa-v2-demo.lkmt.us/

Getting started

Installation

npm install eleventy-plugin-pwa-v2

Setup

  • Add plugin to your eleventy config file:
const pluginPWA = require("eleventy-plugin-pwa-v2");

module.exports = function (eleventyConfig) {
  eleventyConfig.addPlugin(pluginPWA, {
    cacheId: "your-app-id", // change this to your application id
    globIgnores: [
      // any files you don't want service worker to cache go here
      "share-*.jpg",
    ],
    runtimeCaching: [
      {
        // we always want fresh copy of the index page
        urlPattern: /\/$/,
        handler: "NetworkFirst",
      },
      {
        // we also want fresh copies of any HTML page
        urlPattern: /\.html$/,
        handler: "NetworkFirst",
      },
      {
        // we serve stale copies of static assets while they're refreshed
        urlPattern:
          /^.*\.(jpg|png|mp4|gif|webp|ico|svg|woff2|woff|eot|ttf|otf|ttc|json)$/,
        handler: "StaleWhileRevalidate",
      },
    ],
  });
};

Read more on Workbox cache strategies.

  • Register a Service Worker in your template

In the scenario below, your site will refresh automatically when new service worker is installed. However, you may want to implement an UI element to prompt user to refresh the page (e.g. a button labelled Update available. Refresh? that triggers window.location.reload() on click).

// in your head section template
<script>
  if ("serviceWorker" in navigator) {
    navigator.serviceWorker
      .register("/service-worker.js")
      .then((registration) => {
        registration.addEventListener("updatefound", () => {
          const newWorker = registration.installing;
          newWorker.addEventListener("statechange", () => {
            if (newWorker.state === "installed") {
              if (navigator.serviceWorker.controller) {
                // New service worker installed and registered, we reload the page
                window.location.reload();
              }
            }
          });
        });
      })
      .catch((error) => {
        console.error("Service Worker registration failed:", error);
      });
  }
</script>
  • Add web application manifest

The fastest way to get a working application manifest is using Eleventy Favicon Generation plugin.

If you want to build the manifest manually, see the guide on How to add a web app manifest.

Uninstalling the plugin

Please note that when a service worker is installed, and you decide to remove the plugin, the service worker will still remain on your visitor's device.

To ensure that the service worker is unregistered on the user's device during their next visit, add the following code to your site:

<script>
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.getRegistrations().then(function(registrations) {
    for(let registration of registrations) {
        registration.unregister();
    }
  });
}
</script>

Options

You can pass Workbox generateSW options directly into the plugin:

const pluginPWA = require("eleventy-plugin-pwa-v2");

module.exports = function (eleventyConfig) {
  eleventyConfig.addPlugin(pluginPWA, {
    swDest: "./build/sw.js",
    globDirectory: "./build",
  });
};

All available options are listed on Workbox generateSW module page.

Contributions

Contributions of any kind are welcome.

You can contribute by:

  • submiting bug reports
  • improving documentation
  • submitting pull requests

Before contributing be sure to read Code of Conduct.

License

This code is available under the MIT license.

Acknowledgements