乐闻世界logo
搜索文章和话题

What is Workbox in Service Worker and what features does it provide?

3月6日 21:56

Workbox Explained

Workbox is a set of JavaScript libraries developed by Google to simplify Service Worker development, providing powerful caching strategies and tools.

Workbox Introduction

What is Workbox

Workbox is a modular library that helps developers:

  • Simplify Service Worker writing
  • Provide preset caching strategies
  • Automatically generate Service Workers
  • Provide development debugging tools
bash
# Install Workbox npm install workbox-sw workbox-cli --save-dev # Or use CDN importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.5.4/workbox-sw.js');

Core Modules

ModuleFunction
workbox-routingRequest routing matching
workbox-strategiesCaching strategies
workbox-precachingPrecaching
workbox-cacheable-responseCache response management
workbox-expirationCache expiration control
workbox-background-syncBackground sync
workbox-google-analyticsGoogle Analytics offline support

Basic Usage

1. Import via CDN

javascript
// sw.js importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.5.4/workbox-sw.js'); // Workbox automatically loads core modules workbox.setConfig({ debug: true // Development mode }); // Use routing and strategies workbox.routing.registerRoute( ({ request }) => request.destination === 'image', new workbox.strategies.CacheFirst() );

2. Use npm and build tools

javascript
// sw.js import { precacheAndRoute } from 'workbox-precaching'; import { registerRoute } from 'workbox-routing'; import { CacheFirst, NetworkFirst, StaleWhileRevalidate } from 'workbox-strategies'; import { ExpirationPlugin } from 'workbox-expiration'; // Precaching precacheAndRoute(self.__WB_MANIFEST); // Images use Cache First strategy registerRoute( ({ request }) => request.destination === 'image', new CacheFirst({ cacheName: 'images', plugins: [ new ExpirationPlugin({ maxEntries: 60, maxAgeSeconds: 30 * 24 * 60 * 60 // 30 days }) ] }) );

Core Features Explained

1. Routing

javascript
import { registerRoute } from 'workbox-routing'; // Method 1: String matching registerRoute( '/api/users', new NetworkFirst() ); // Method 2: Regex matching registerRoute( new RegExp('/api/.*'), new NetworkFirst() ); // Method 3: Callback function matching registerRoute( ({ url, request, event }) => { return url.pathname.startsWith('/api/') && request.method === 'GET'; }, new NetworkFirst() ); // Method 4: Use Route class import { Route } from 'workbox-routing'; const apiRoute = new Route( ({ url }) => url.pathname.startsWith('/api/'), new NetworkFirst() ); registerRoute(apiRoute);

2. Caching Strategies

javascript
import { CacheFirst, NetworkFirst, StaleWhileRevalidate, NetworkOnly, CacheOnly } from 'workbox-strategies'; // Cache First registerRoute( ({ request }) => request.destination === 'style' || request.destination === 'script', new CacheFirst({ cacheName: 'static-resources' }) ); // Network First registerRoute( ({ url }) => url.pathname.startsWith('/api/'), new NetworkFirst({ cacheName: 'api-cache', plugins: [ new ExpirationPlugin({ maxEntries: 50, maxAgeSeconds: 5 * 60 // 5 minutes }) ] }) ); // Stale While Revalidate registerRoute( ({ request }) => request.destination === 'image', new StaleWhileRevalidate({ cacheName: 'images', plugins: [ new ExpirationPlugin({ maxEntries: 60, maxAgeSeconds: 30 * 24 * 60 * 60 }) ] }) );

3. Precaching

javascript
import { precacheAndRoute, cleanupOutdatedCaches } from 'workbox-precaching'; // Method 1: Manually specify resources precacheAndRoute([ { url: '/index.html', revision: '1.0.0' }, { url: '/styles.css', revision: '1.0.0' }, { url: '/app.js', revision: '1.0.0' } ]); // Method 2: Use build tool generated manifest // webpack config const { InjectManifest } = require('workbox-webpack-plugin'); module.exports = { plugins: [ new InjectManifest({ swSrc: './src/sw.js', swDest: 'sw.js' }) ] }; // Use in sw.js precacheAndRoute(self.__WB_MANIFEST); // Clean outdated caches cleanupOutdatedCaches();

4. Cache Expiration Control

javascript
import { ExpirationPlugin } from 'workbox-expiration'; registerRoute( ({ request }) => request.destination === 'image', new CacheFirst({ cacheName: 'images', plugins: [ new ExpirationPlugin({ maxEntries: 50, maxAgeSeconds: 30 * 24 * 60 * 60, purgeOnQuotaError: true }) ] }) );

5. Cache Response Management

javascript
import { CacheableResponsePlugin } from 'workbox-cacheable-response'; registerRoute( ({ url }) => url.pathname.startsWith('/api/'), new NetworkFirst({ cacheName: 'api-cache', plugins: [ new CacheableResponsePlugin({ statuses: [0, 200], headers: { 'X-Cache-Allowed': 'true' } }) ] }) );

6. Background Sync

javascript
import { BackgroundSyncPlugin } from 'workbox-background-sync'; const bgSyncPlugin = new BackgroundSyncPlugin('api-queue', { maxRetentionTime: 24 * 60 }); registerRoute( ({ url }) => url.pathname.startsWith('/api/'), new NetworkFirst({ plugins: [bgSyncPlugin] }), 'POST' );

7. Range Request Support

javascript
import { RangeRequestsPlugin } from 'workbox-range-requests'; registerRoute( ({ url }) => url.pathname.endsWith('.mp4'), new CacheFirst({ cacheName: 'videos', plugins: [ new RangeRequestsPlugin() ] }) );

Advanced Usage

Custom Strategy

javascript
import { Strategy } from 'workbox-strategies'; class CustomStrategy extends Strategy { async _handle(request, handler) { const cachedResponse = await handler.cacheMatch(request); if (cachedResponse) { handler.fetchAndCachePut(request).catch(() => {}); return cachedResponse; } return handler.fetchAndCachePut(request); } } registerRoute( '/custom-route', new CustomStrategy() );

Plugin Development

javascript
const loggingPlugin = { cacheWillUpdate: async ({ request, response }) => { console.log(`Cache update: ${request.url}`); return response; }, cacheDidUpdate: async ({ request, cacheName }) => { console.log(`Cache updated: ${request.url} in ${cacheName}`); }, fetchDidFail: async ({ request, error }) => { console.error(`Request failed: ${request.url}`, error); } }; registerRoute( '/api/', new NetworkFirst({ plugins: [loggingPlugin] }) );

Runtime Caching

javascript
import { warmStrategyCache } from 'workbox-recipes'; warmStrategyCache({ urls: ['/about', '/contact'], strategy: new StaleWhileRevalidate({ cacheName: 'pages' }) }); import { offlineFallback } from 'workbox-recipes'; offlineFallback({ pageFallback: '/offline.html', imageFallback: '/images/offline.png', fontFallback: false });

Configuration Options

Global Configuration

javascript
import { setConfig } from 'workbox-core'; setConfig({ debug: process.env.NODE_ENV === 'development', logger: customLogger });

Log Level

javascript
import { setLogLevel, LOG_LEVELS } from 'workbox-core'; setLogLevel(LOG_LEVELS.debug);

Integration with Build Tools

Webpack

javascript
// webpack.config.js const { GenerateSW, InjectManifest } = require('workbox-webpack-plugin'); module.exports = { plugins: [ new GenerateSW({ clientsClaim: true, skipWaiting: true, runtimeCaching: [ { urlPattern: /\.(?:png|jpg|jpeg|svg|gif)$/, handler: 'CacheFirst', options: { cacheName: 'images', expiration: { maxEntries: 60 } } } ] }) ] };

Vite

javascript
// vite.config.js import { VitePWA } from 'vite-plugin-pwa'; export default { plugins: [ VitePWA({ workbox: { runtimeCaching: [ { urlPattern: /\.(?:png|jpg|jpeg|svg|gif)$/, handler: 'CacheFirst', options: { cacheName: 'images' } } ] } }) ] };

Workbox vs Native Service Worker

FeatureWorkboxNative Service Worker
Learning curveLowHigh
Code volumeLessMore
Caching strategiesMultiple presetsManual implementation
Debugging toolsCompleteBasic
FlexibilityHighVery high
Package sizeAdds ~20-50KBNo extra overhead

Best Practices

  1. Choose appropriate strategy: Select best caching strategy based on resource type
  2. Set cache limits: Use ExpirationPlugin to prevent storage overflow
  3. Version control: Set revision correctly when using precaching
  4. Error handling: Add appropriate error handling and fallback solutions
  5. Performance optimization: Avoid caching too much data, clean regularly
标签:Service Worker