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

@gasket/plugin-workbox

v7.0.9

Published

Gasket Workbox Plugin

Downloads

823

Readme

@gasket/plugin-workbox

Provides precaching and runtimeCaching via Workbox.

Precaching downloads an app's pages, chunks, and assets and stores this on the device, before they are actually needed. This happens asynchronously and results in a faster experience. Additionally, other files and be cached with on-demand strategies, such as cacheFirst, or networkFirst.

This plugin implements service worker configuration using Workbox. Workbox is a library that bakes in a set of best practices and removes the boilerplate every developer writes when working with service workers.

Installation

npm i @gasket/plugin-workbox

Update your gasket file plugin configuration:

// gasket.js

+ import pluginWorkbox from '@gasket/plugin-workbox';

export default makeGasket({
  plugins: [
+   pluginWorkbox
  ]
});

Configuration

Set the Workbox options, in the gasket.js under workbox.

  • outputDir - (string) The path to the directory in which the Workbox libraries should be copied (default: ./build/workbox)
  • basePath - (string) Change the default path to /_workbox endpoint by adding a path prefix here. (default: ''). Used for setting up CDN support for Workbox files.
  • config: (object) Any initial workbox config options which will be merged with those from any workbox lifecycle hooks.

Example config

// gasket.js
export default makeGasket({
  workbox: {
    config: {
      runtimeCaching: [{
        urlPattern: /.png$/,
        handler: 'cacheFirst'
      }]
    }
  }
});

Lifecycles

workbox

This hook allows other Gasket plugins to add to the Workbox config in order to precache files, set runtime cache rules, etc. Hooks should return an object partial which will be deeply merged.

export default {
  name: 'sample-plugin',
  hooks: {
    workbox: function (gasket, config, context) {
      // `config` is the initial workbox config
      // `context` is the service-worker context.
      const { req, res } = context;
      if(req) {
        // adjust config for request-based service workers using headers, cookies, etc.
      }

      // return a config partial which will be merged
      return {
        runtimeCaching: [{
          urlPattern: /https:\/\/some.api.com/,
          handler: 'networkFirst'
        }]
      }
    }
  }
}

For advanced configurations, be sure to read the workbox config options for generateSWString.

How it works

For a service worker request, a config object is constructed by the Workbox lifecycle hook, it is to generateSWString from the workbox-build module. This will generate a string with service worker code for workbox which is then appended to the service worker content in the composeServiceWorker hook from @gasket/plugin-service-worker.

During the build hook, the Workbox libraries are copied to the build output directory so that they can be served by the app. The service worker will then import these scripts, with requests to the /_workbox static files served by the app. These can be set up to edge cache by setting the basePath option.

License

MIT