service-worker

Service worker lifecycle, caching strategies, background sync, push notifications, and static routing for progressive web apps. Covers Workbox v7.4 strategies (CacheFirst, NetworkFirst, StaleWhileRevalidate), workbox-precaching for app shells, Background Sync API with cross-browser fallbacks, Push API with VAPID keys, and the Service Worker Static Routing API (Chrome 123+). Use when registering service workers, implementing offline caching, configuring background sync, adding push notifications, optimizing fetch handling with static routes, or integrating service workers with local-first sync engines.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "service-worker" with this command: pnpm dlx skills add oakoss/agent-skills

Service Worker

Overview

Service workers are event-driven scripts that run in a separate thread from the main page, intercepting network requests, managing caches, and enabling offline functionality. Workbox v7.4 (maintained by the Chrome Aurora team) provides a production-ready abstraction over the low-level Cache API and fetch event handling.

When to use: Progressive web apps needing offline support, apps requiring push notifications, background data synchronization, app shell caching, network request optimization with static routing.

When NOT to use: Simple static sites served from a CDN, server-rendered apps with no offline requirements, apps where stale data is unacceptable (use network-only), prototypes where caching complexity is premature.

Quick Reference

PatternAPI / ToolKey Points
Registrationnavigator.serviceWorker.register()Register in window context, scope defaults to path
Precachingworkbox-precachingRevision-hashed app shell, injected at build time
Cache-firstCacheFirst strategyStatic assets, fonts, images
Network-firstNetworkFirst strategyAPI responses needing freshness
Stale-while-revalidateStaleWhileRevalidate strategyBalance between speed and freshness
Background syncworkbox-background-syncReplay failed requests when back online
Push notificationsPush API + Notifications APIVAPID keys, server-sent push, offline-first display
Static routingevent.addRoutes() in installBypass fetch handler for known routes
Skip waitingself.skipWaiting()Activate new SW immediately, use with caution
Clients claimself.clients.claim()Control existing tabs without reload
Navigation preloadnavigationPreload.enable()Parallel network request during SW startup
Update promptworkbox-window Workbox classDetect updates, prompt user, postMessage to SW
Offline fallbackworkbox-recipes offlineFallbackServe fallback page when cache and network fail
Cache expirationworkbox-expirationmaxEntries and maxAgeSeconds per cache

Browser Support Quick Reference

FeatureChromeFirefoxSafariEdge
Service Workers40+44+11.1+17+
Background Sync49+NoNo79+
Periodic Sync80+NoNo80+
Push API50+44+16.4+17+
Navigation Preload59+99+15.4+18+
Static Routing API123+NoNo123+
Background Fetch74+NoNo79+
Storage Persistence52+55+15.2+79+

Common Mistakes

MistakeCorrect Pattern
Caching API responses with CacheFirstUse NetworkFirst or StaleWhileRevalidate for dynamic data
Calling skipWaiting unconditionallyPrompt user before activating to avoid breaking in-flight navigations
Not versioning precache manifestsUse workbox-build or workbox-webpack-plugin to inject revision hashes
Ignoring cache storage limitsImplement cache expiration with workbox-expiration plugin
Using sync access handle in service workerOPFS createSyncAccessHandle is dedicated-worker only; use async methods
Assuming Background Sync works everywhereChrome/Edge only (~80%); implement navigator.onLine fallback
Caching opaque responses without size limitsOpaque responses pad cache quota; limit with maxEntries
Not handling service worker update racesUse workbox-window to coordinate update lifecycle with the page
Registering SW before page load completesRegister in window load event to avoid competing for bandwidth
Letting sync engine data flow through SW cacheLet the sync engine handle data; SW handles app shell and static assets
Not cleaning up old caches on activateDelete outdated caches in activate event to free storage
Missing offline fallback for navigationsPrecache an offline.html and serve it when network and cache both fail

Workbox Package Reference

PackagePurpose
workbox-precachingBuild-time asset caching with revision management
workbox-routingURL pattern matching and strategy routing
workbox-strategiesCacheFirst, NetworkFirst, StaleWhileRevalidate
workbox-expirationCache size and age limits
workbox-cacheable-responseFilter responses by status or header
workbox-background-syncQueue and replay failed requests
workbox-windowPage-side registration and update management
workbox-recipesPre-built patterns (offline fallback, warm cache)
workbox-buildBuild tool for generating precache manifests
workbox-navigation-preloadEnable navigation preload in activate

Delegation

If the local-first skill is available, delegate architecture decisions, sync engine selection, and offline data strategy to it. Otherwise, recommend: pnpm dlx skills add oakoss/agent-skills -s local-first -a claude-code -y If the electricsql skill is available, delegate sync engine patterns, shape subscriptions, and real-time data replication to it. Otherwise, recommend: pnpm dlx skills add oakoss/agent-skills -s electricsql -a claude-code -y

  • Caching strategy review: Use Task agent to audit route-to-strategy mappings
  • Offline testing: Use Task agent to simulate offline scenarios and verify fallback behavior
  • Push notification design: Use Plan agent to design notification payload and action strategies

References

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Automation

playwright

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

ui-ux-polish

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

tanstack-form

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

find-skills

No summary provided by upstream source.

Repository SourceNeeds Review