Service worker library that provides an easy entry point to the Workbox ecosystem for Progressive Web Apps
npx @tessl/cli install tessl/npm-workbox-sw@7.3.0Workbox SW is a service worker library that provides an easy entry point to the Workbox ecosystem for Progressive Web Apps. It creates a global workbox object that acts as a proxy to dynamically load and access other Workbox modules, simplifying the integration of service worker functionality without requiring individual module management.
npm install workbox-swFor service worker environments, import via importScripts:
importScripts('https://storage.googleapis.com/workbox-cdn/releases/7.3.0/workbox-sw.js');For module environments:
import 'workbox-sw';// Import the library (creates global workbox object)
importScripts('https://storage.googleapis.com/workbox-cdn/releases/7.3.0/workbox-sw.js');
// Configure Workbox (optional)
workbox.setConfig({
debug: true,
modulePathPrefix: '/custom/path/to/workbox-modules/'
});
// Use Workbox modules through the global object
workbox.precaching.precacheAndRoute([
'/app.js',
'/styles.css',
'/index.html'
]);
workbox.routing.registerRoute(
new RegExp('.*\\.(?:png|jpg|jpeg|svg|gif)'),
new workbox.strategies.CacheFirst({
cacheName: 'image-cache',
plugins: [
new workbox.expiration.ExpirationPlugin({
maxEntries: 50,
}),
],
})
);Workbox SW is built around several key components:
workbox global acts as a proxy that automatically loads Workbox modules on first accessimportScripts() when their properties are accessedConfigure Workbox behavior including debug mode and module loading paths.
/**
* Configure Workbox settings before accessing any modules
* @param {Object} options - Configuration options
* @param {boolean} options.debug - Use dev builds if true, prod builds if false (default: true on localhost)
* @param {string} options.modulePathPrefix - Custom path prefix for module loading
* @param {ModulePathCallback} options.modulePathCb - Custom callback for determining module paths
*/
workbox.setConfig(options);Explicitly load Workbox modules to ensure availability.
/**
* Manually load a Workbox module by name
* @param {string} moduleName - Name of the module to load (e.g., 'workbox-precaching')
*/
workbox.loadModule(moduleName);Access Workbox modules through property access on the global workbox object. Modules are loaded automatically on first access.
Access to workbox-background-sync module for offline background synchronization.
workbox.backgroundSyncAccess to workbox-broadcast-update module for notifying clients of cache updates.
workbox.broadcastUpdateAccess to workbox-cacheable-response module for determining cacheable responses.
workbox.cacheableResponseAccess to workbox-core module providing fundamental Workbox utilities.
workbox.coreAccess to workbox-expiration module for cache expiration management.
workbox.expirationAccess to workbox-google-analytics module for offline Google Analytics support.
workbox.googleAnalyticsAccess to workbox-navigation-preload module for navigation preload functionality.
workbox.navigationPreloadAccess to workbox-precaching module for precaching static assets.
workbox.precachingAccess to workbox-range-requests module for handling HTTP range requests.
workbox.rangeRequestsAccess to workbox-routing module for request routing and interception.
workbox.routingAccess to workbox-strategies module for caching strategies (CacheFirst, NetworkFirst, etc.).
workbox.strategiesAccess to workbox-streams module for handling streaming responses.
workbox.streamsAccess to workbox-recipes module for common Workbox usage patterns.
workbox.recipesCustom callback function for determining module paths.
/**
* Callback function for customizing module paths
* @callback ModulePathCallback
* @param {string} moduleName - The name of the module to load (e.g., 'workbox-core')
* @param {boolean} debug - When true, dev builds should be loaded, otherwise load prod builds
* @returns {string} Path to the module file for importScripts()
*/importScripts('https://storage.googleapis.com/workbox-cdn/releases/7.3.0/workbox-sw.js');
// Precache static assets
workbox.precaching.precacheAndRoute([
'/index.html',
'/app.js',
'/styles.css'
]);
// Cache images with expiration
workbox.routing.registerRoute(
/\.(?:png|jpg|jpeg|svg|gif)$/,
new workbox.strategies.CacheFirst({
cacheName: 'images',
plugins: [
new workbox.expiration.ExpirationPlugin({
maxEntries: 60,
maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
}),
],
})
);importScripts('https://storage.googleapis.com/workbox-cdn/releases/7.3.0/workbox-sw.js');
// Use custom module path
workbox.setConfig({
debug: false,
modulePathPrefix: '/custom/workbox-modules/'
});
// Or use a custom callback
workbox.setConfig({
modulePathCb: (moduleName, debug) => {
const suffix = debug ? 'dev' : 'prod';
return `/my-cdn/${moduleName}.${suffix}.js`;
}
});importScripts('https://storage.googleapis.com/workbox-cdn/releases/7.3.0/workbox-sw.js');
// Preload modules that might be needed later
workbox.loadModule('workbox-background-sync');
workbox.loadModule('workbox-routing');
// Now these modules are available immediately
workbox.backgroundSync.Queue('my-queue');setConfig() throws an error if called after any modules have been loadedloadModule() logs a console error and re-throws if module loading failsimportScripts() for dynamic module loading