Scripts2026年5月30日·1 分钟阅读

Workbox — JavaScript Libraries for Progressive Web Apps

A set of production-ready JavaScript libraries from Google Chrome that simplify service worker caching, routing, and offline support for Progressive Web Apps.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Workbox
直接安装命令
npx -y tokrepo@latest install 2236abe1-5c65-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

Workbox is a collection of JavaScript libraries maintained by the Google Chrome team that make it easier to build offline-capable Progressive Web Apps. It abstracts complex service worker patterns into simple, composable modules for caching, routing, and background sync.

What Workbox Does

  • Provides pre-built caching strategies (Cache First, Network First, Stale While Revalidate)
  • Generates a service worker with precaching of static assets at build time
  • Offers runtime caching with flexible route matching via URL patterns or regex
  • Handles background sync for deferred network requests when offline
  • Integrates with webpack, Rollup, and other bundlers via plugins

Architecture Overview

Workbox is modular: each concern (routing, caching, precaching, background sync, broadcast updates) lives in its own npm package. The workbox-build module generates a service worker file with a precache manifest at build time. At runtime, workbox-routing intercepts fetch events and delegates to strategy modules like workbox-strategies. This modular design allows tree-shaking to keep the service worker bundle small.

Self-Hosting & Configuration

  • Install workbox-cli or the bundler plugin for your build tool
  • Run workbox wizard to generate a configuration file interactively
  • Use generateSW for a fully generated service worker or injectManifest for custom logic
  • Configure caching strategies per route in workbox-config.js
  • Deploy the generated service worker file alongside your application

Key Features

  • Battle-tested caching strategies with expiration and cache size limits
  • Precaching with revision hashing for reliable static asset updates
  • Background sync queues requests and replays them when connectivity returns
  • Navigation preload support for faster page loads
  • Workbox Window module for communicating between page and service worker

Comparison with Similar Tools

  • sw-precache / sw-toolbox — predecessors now deprecated in favor of Workbox
  • Vite PWA Plugin — uses Workbox under the hood with Vite-specific configuration
  • next-pwa — Next.js wrapper around Workbox for easier integration
  • Service Worker API — raw browser API; Workbox provides higher-level abstractions
  • Remix PWA — Remix-specific offline support; Workbox is framework-agnostic

FAQ

Q: Can I use Workbox with any framework? A: Yes. Workbox is framework-agnostic and works with React, Vue, Angular, vanilla JS, and any build tool.

Q: What is the difference between generateSW and injectManifest? A: generateSW creates the entire service worker for you; injectManifest lets you write custom service worker code and just injects the precache manifest.

Q: Does Workbox handle push notifications? A: Workbox focuses on caching and offline. Push notifications are handled by the Push API directly.

Q: How do I update cached assets? A: Workbox uses revision hashes in the precache manifest and automatically updates stale entries.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产