AGENT SKILL

ams-service-worker-pro

暂无简介

License 许可
MIT
所属仓库
TanNhatCMS/jarvis
最近更新
2026-11-07
Stars
★ 0
Forks
1

03 SKILL.md 正文

Service Worker Pro

A practical guide to Service Workers: the programmable network proxy in the browser — lifecycle, caching strategies, offline architectures, background sync, and the update flows that keep users on fresh code without breaking them.

Overview

A Service Worker (SW) sits between your page and the network: it intercepts requests and decides — cache, network, or both. This enables offline support, instant repeat loads, and background sync. It's also the foundation of push notifications. The SW runs in its own thread with its own lifecycle (install → activate → fetch), independent of any page — which is both its power and its sharp edges (stale caches, update races).

When to use

  • Offline-capable web apps (see also pwa-patterns).
  • Caching strategies for assets and API responses.
  • Background sync for queued mutations.
  • Push notification handling.
  • Advanced prefetching/prerendering strategies.

Core concepts

  • Lifecycle. install (precache) → activate (cleanup old caches) → fetch (intercept). A new SW waits until all tabs using the old one close (skipWaiting() + clients.claim() to take over immediately — use deliberately).
  • Scope. SW controls pages under its registration path. Register at root (/sw.js) for full-site control.
  • Cache API. caches.open('v1') — programmatic cache storage, separate from HTTP cache. Version cache names; delete old versions on activate.
  • Strategies. Cache-first (immutable assets), network-first (fresh data, cache fallback), stale-while-revalidate (fast + fresh eventually), network-only, cache-only. Match strategy to resource type.
  • Precache vs runtime. Precache the app shell at install (known URLs); cache runtime requests as they happen (API responses, images).
  • Background Sync. sync.register('send-queue') → SW retries when online — for queued mutations. One-shot; for periodic, Periodic Background Sync (limited support).
  • Update flow. Byte-difference check on navigation → new SW installs → waits → activates. UX: notify "new version available" → user refreshes, or auto-skipWaiting for non-critical apps.

Practical workflow

1. Register.

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js').then(reg => {
    reg.addEventListener('updatefound', () => notifyUpdateAvailable());
  });
}

2. Precache + strategies (vanilla).

const SHELL = 'shell-v3';
const PRECACHE = ['/', '/app.css', '/app.js', '/offline.html'];

self.addEventListener('install', (e) => {
  e.waitUntil(caches.open(SHELL).then(c => c.addAll(PRECACHE)).then(() => self.skipWaiting()));
});
self.addEventListener('activate', (e) => {
  e.waitUntil(caches.keys().then(keys =>
    Promise.all(keys.filter(k => k !== SHELL).map(k => caches.delete(k)))
  ).then(() => self.clients.claim()));
});
self.addEventListener('fetch', (e) => {
  const { request } = e;
  if (request.url.includes('/api/')) {
    e.respondWith(networkFirst(request, 'api-v1'));       // fresh data, offline fallback
  } else if (request.destination === 'image') {
    e.respondWith(cacheFirst(request, 'images-v1'));      // immutable-ish assets
  } else {
    e.respondWith(staleWhileRevalidate(request, SHELL));  // shell + pages
  }
});

(Or use Workbox — recommended over hand-rolling for production.)

3. Offline fallback. Navigation requests failing → serve cached /offline.html. API failing → serve cached response with a flag, or a structured offline error the app handles.

4. Background sync for mutations.

// page: queue failed POSTs in IndexedDB, then
navigator.serviceWorker.ready.then(reg => reg.sync.register('outbox'));
// sw.js: self.addEventListener('sync', e => { if (e.tag === 'outbox') e.waitUntil(flushOutbox()); });

5. Update UX. updatefound → show "Update available — refresh" snackbar → on click, registration.waiting.postMessage({ type: 'SKIP_WAITING' }) → controllerchange → location.reload().

Common pitfalls

  • Caching the SW file itself. If /sw.js is cached with a long TTL, updates never arrive. Serve it Cache-Control: no-cache (revalidate every time).
  • Over-aggressive precaching. Precaching everything = slow install, wasted bandwidth. Precache the shell; runtime-cache the rest.
  • Stale-forever bugs. Cache-first on HTML/API without versioning = users stuck on ancient content. Version caches; prefer SWR/network-first for HTML.
  • Update races. skipWaiting mid-session can break in-flight app state (old page, new cached assets). For complex apps, prompt the user instead of forcing.
  • Opaque responses. Cross-origin no-cors responses are opaque — cacheable but inscrutable (can't check status). Prefer CORS-enabled endpoints.
  • Quotas. Cache + IndexedDB share origin storage (browser-managed, evictable). Don't treat it as infinite; handle QuotaExceededError.
  • Dev confusion. SW caching during development = "my changes aren't showing". Unregister in dev, or use dev-only bypass. Chrome DevTools → Application → Service Workers → "Update on reload" + "Bypass for network".
  • Scope mistakes. Registering /app/sw.js controls only /app/*. Root registration for site-wide control.
  • Untested offline. "Works offline" claimed, never tested. DevTools offline mode + real airplane-mode tests on device.

04 同仓库更多 Skills

TanNhatCMS
TanNhatCMS/jarvis

web-scrape

Turn web pages into LLM-ready markdown via the Firecrawl API (renders JS, anti-bot handling). Use when browser_open/fetch fails on JS-heavy pages, or when you need clean markdown/structured extraction from a site. Requires FIRECRAWL_API_KEY.

★ 0
TanNhatCMS
TanNhatCMS/jarvis

design-taste-frontend

tasteskill v2, the anti-slop frontend skill. Art direction for websites, distilled from a study of 247 curated gallery sites (One Page Love, Awwwards Sites of the Day). Deals researched directions (idea, composition, type, palette, photographs, section blueprints) from a library, enforces a strict no-slop house style with a real-browser inspector, and runs a look-and-fix loop on screenshots. Use this whenever you build, design, redesign, restyle, polish or review a landing page, marketing site, portfolio, product page, one-pager, hero or any public-facing web page, in any stack (HTML/CSS, React, Next.js, Vue, Svelte, Astro, Tailwind) — also when the user says a page looks generic, templated, "AI-made", sloppy, or asks to make it look designed, premium, award-level or less like every other site. Not for dashboards or app screens.

★ 0
TanNhatCMS
TanNhatCMS/jarvis

test-driven-development

Use when implementing any feature or bugfix, before writing implementation code

★ 0
TanNhatCMS
TanNhatCMS/jarvis

systematic-debugging

Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes

★ 0
TanNhatCMS
TanNhatCMS/jarvis

requesting-code-review

Use when completing tasks, implementing major features, or before merging to verify work meets requirements

★ 0
TanNhatCMS
TanNhatCMS/jarvis

brainstorming

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).

★ 0