Workbox CLI

7.4.0 · active · verified Wed Apr 22

workbox-cli is the command-line interface for the Workbox library, a set of JavaScript modules for adding offline support to web applications. It streamlines the process of generating service workers and precaching assets, simplifying the integration of Workbox into projects without requiring complex custom build scripts. The current stable version is 7.4.0. Workbox CLI's release cadence generally aligns with the broader Workbox project, seeing frequent patch updates for dependency maintenance and security, with major versions introducing significant changes like Node.js version bumps. Its key differentiator is providing a user-friendly entry point for common Workbox use cases through a series of interactive wizards and declarative configuration files, abstracting away the underlying `workbox-build` module for a smoother developer experience.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `workbox-cli` with a configuration file to generate a service worker that precaches assets and handles runtime caching for Google Fonts.

/* workbox-config.js */
module.exports = {
  globDirectory: 'dist/',
  globPatterns: [
    '**/*.{html,js,css,png,jpg,gif,svg,json,webmanifest}'
  ],
  swDest: 'dist/sw.js',
  clientsClaim: true,
  skipWaiting: true,
  navigateFallback: '/index.html',
  runtimeCaching: [{
    urlPattern: new RegExp('^https://fonts.(?:googleapis|gstatic).com/'),
    handler: 'StaleWhileRevalidate',
    options: {
      cacheName: 'google-fonts',
      expiration: {
        maxEntries: 50,
        maxAgeSeconds: 60 * 60 * 24 * 30,
      },
    },
  }],
};

// To run this example:
// 1. Create a directory named 'dist' in your project root.
// 2. Place some static files (e.g., index.html, style.css) into 'dist'.
// 3. Save the above content as 'workbox-config.js' in your project root.
// 4. Open your terminal in the project root and run:
//    npx workbox-cli generateSW workbox-config.js
// This will generate 'dist/sw.js' based on your configuration.

view raw JSON →