Workbox-build is a core module within the Workbox library, designed to integrate seamlessly into JavaScript build processes to facilitate the generation of precache manifests for service workers. Currently at version 7.4.0, this package focuses on the programmatic creation of a list of URLs and their revision details, which `workbox-sw` can then use to precache assets, enabling offline capabilities for web applications. Releases in the 7.x series have primarily focused on critical dependency updates and maintaining compatibility, with v7.0.0 notably raising the minimum Node.js requirement to version 16, and the latest versions further increasing it to Node.js >=20.0.0. It offers functions like `generateSW` for a complete service worker lifecycle management and `injectManifest` for greater customization, distinguishing itself by providing a robust, opinionated, yet flexible way to manage asset caching without direct reliance on specific bundlers, unlike `workbox-webpack-plugin` which is specifically for Webpack. Its steady release cadence primarily addresses security and dependency maintenance.
npm install workbox-buildVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `workbox-build`'s `generateSW` function to create a production-ready service worker. It configures precaching for common static assets and sets up a runtime caching strategy for Google Fonts, suitable for a PWA.
Ensure your build environment uses Node.js version 20 or higher. Update your Node.js runtime and CI/CD configurations accordingly.
Always use `import` statements for `workbox-build` and ensure your Node.js project is configured for ESM (e.g., `"type": "module"` in `package.json` or `.mjs` file extension).
Always keep `workbox-build` updated to the latest stable version to receive critical security patches and dependency updates.
Carefully review the Workbox documentation for each configuration option. Use absolute paths for `globDirectory` and `swDest`, and ensure the `globPatterns` correctly match your assets. Enable Workbox's verbose logging for debugging during development.
Ensure your Node.js script file uses the `.mjs` extension or add `"type": "module"` to your `package.json`. If using older Node.js versions that don't fully support ESM, consider transpilation or updating Node.js.
Ensure `globDirectory` is set to a string representing an absolute path to the root of your web app's static assets, e.g., `path.join(__dirname, 'dist')`.
Ensure `generateSW` is called only once per build. If you have multiple parts of your build process that need a service worker, consolidate them or use `injectManifest` with a pre-existing service worker file.