Skip to content

feat: offline app-shell service worker (PWA) - #147

Merged
therealsama merged 1 commit into
mainfrom
feat/pwa-service-worker
Jun 27, 2026
Merged

therealsama merged 1 commit into
mainfrom
feat/pwa-service-worker

Conversation

@therealsama

@therealsama therealsama commented Jun 11, 2026 •

Copy link
Copy Markdown
Collaborator

Add a minimal, dependency-free service worker so an installed PWA cold-boots offline on the very first visit.

@therealsama
therealsama requested a review from kplachkov as a code owner June 11, 2026 14:40
@therealsama
therealsama force-pushed the feat/pwa-service-worker branch 2 times, most recently from a54f4f1 to eebe997 Compare June 27, 2026 17:01
@therealsama therealsama changed the title Offline app-shell service worker (PWA) feat: offline app-shell service worker (PWA) Jun 27, 2026
@therealsama
therealsama force-pushed the feat/pwa-service-worker branch 4 times, most recently from a68975a to 62f4c5a Compare June 27, 2026 19:41
Add a minimal, dependency-free service worker so an installed PWA cold-boots
offline on the very first visit.

- public/sw.js: network-first navigation (3s timeout + cached-shell fallback)
  and cache-first for content-hashed assets, with an install-time precache of
  the shell plus this build's hashed bundles (parsed from the cached shell HTML)
  so the app boots offline on the FIRST visit, not just after a warm-up load.
  Two caches (install-time shell precache + a FIFO-trimmed runtime cache) and
  skipWaiting + clients.claim for instant takeover. Every cache write is guarded,
  so a QuotaExceededError under storage pressure can't surface as an unhandled
  rejection. Cross-origin requests (Algolia / Firebase) are bypassed entirely,
  so the feed/comments stay live and Playwright's network mocks keep working.
  The shell is refreshed only from a genuine root navigation, so an unknown deep
  link served as 404.html (HTTP 200) can't poison the cache; that refresh clones
  the response synchronously (before it's returned to the page) so it can't
  silently fail on an already-disturbed body and leave the offline shell pinned
  to a stale build across deploys.
- src/main.tsx: register the SW (production only, on window load; pure
  progressive enhancement — registration failures are ignored).
- src/context/NetworkStatusContext.tsx: detect offline on a cold load even when
  navigator.onLine wrongly reports online (a SW-served reload, captive portal, or
  DevTools device-mode "Offline" — no online/offline event fires on a fresh load).
  When a SW controls the page, verify connectivity with a same-origin, cache-busted
  HEAD probe that bypasses the SW (it only handles GET) and can't be answered from
  any HTTP cache, so it genuinely fails offline. A controllerchange listener covers
  first-install loads where the controller isn't set at mount, and while offline the
  probe re-runs every 3s so the bar also clears on reconnect (the same stuck
  navigator.onLine means no online event fires to recover). The poll re-arms even
  after an inconclusive (timeout) probe, so a transient slow link mid-recovery
  can't silently end the loop and strand the bar — only a confirmed reachable
  result stops it.
- playwright.config.ts: block service workers by default so existing specs run
  against the live network; the PWA spec opts back in with test.use().
- e2e/pwa.spec.ts: prove cold-boot offline after a single online visit, and that
  the offline bar appears on a cold reload when navigator.onLine reports online and
  then clears on reconnect (online/offline events suppressed so only the probe can
  move the state).
@therealsama
therealsama force-pushed the feat/pwa-service-worker branch from 62f4c5a to df6e12d Compare June 27, 2026 19:58
@therealsama
therealsama enabled auto-merge June 27, 2026 20:42
@therealsama
therealsama merged commit a6e0c13 into main Jun 27, 2026
4 checks passed
@therealsama
therealsama deleted the feat/pwa-service-worker branch June 27, 2026 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants