mason
mason-log.

mason

mason.

안녕하세요. mason 입니다.

performance

Service Worker와 PWA.

SW lifecycle, cache strategies, offline, manifest, install prompt

Service Worker는 브라우저와 네트워크 사이 proxy로, offline cache, push, background sync를 가능하게 합니다. PWA는 SW + manifest로 앱-like 경험.

🔄 Service Worker lifecycle

Service Worker 수명주기와 요청 가로채기

  1. register (navigator.serviceWorker.register)
  2. install — static cache preload
  3. activate — old cache cleanup
  4. fetch — request intercept
  • HTTPS 필수 (localhost 예외)
  • DOM 접근 불가, postMessage 통신

📦 Cache strategies (Workbox)

전략설명
Cache Firstcache → network (static asset)
Network Firstnetwork → cache (API, fresh content)
Stale While Revalidatecache 즉시 + background update
Network Onlyno cache
Cache Onlyoffline shell

📱 PWA manifest

{
  "name": "My App",
  "short_name": "App",
  "start_url": "/",
  "display": "standalone",
  "icons": [{ "src": "/icon-192.png", "sizes": "192x192", "type": "image/png" }]
}
  • install prompt (beforeinstallprompt)
  • iOS: Add to Home Screen (제한적)

⚠️ 주의

  • cache versioning, update prompt (skipWaiting, clients.claim)
  • stale API cache — TTL, mutation invalidate
  • Next.js: next-pwa 또는 manual (App Router는 설정 주의)

🔗 참고 자료