Skip to content

feat(pwa): add stale-while-revalidate service worker for shell and ca… - #1450

Open
forked-dev wants to merge 2 commits into
CalloraOrg:mainfrom
forked-dev:feat/service-worker-swr-cache
Open

forked-dev wants to merge 2 commits into
CalloraOrg:mainfrom
forked-dev:feat/service-worker-swr-cache

Conversation

@forked-dev

Copy link
Copy Markdown

Closes #1382

Summary

Adds a production-only service worker that caches the app shell, hashed static assets, and unauthenticated catalogue GET responses using a stale-while-revalidate (SWR) strategy, and clears cached catalogue responses when switching or removing an account.

Changes

  • public/sw.js:
    • Defines versioned caches (callora-shell-v1, callora-catalog-v1) and precaches / and /index.html on install with skipWaiting().
    • Cleans up outdated cache versions on activate and calls clients.claim().
    • Serves same-origin /assets/*, navigation requests (with /index.html fallback), and unauthenticated catalogue GET /api/apis* requests using stale-while-revalidate.
    • Leaves all other /api/* endpoints, non-GET requests, cross-origin requests, and any Authorization-bearing requests network-only so account-scoped or authenticated responses are never cached across accounts.
    • Handles { type: "CLEAR_API_CACHE" } messages by deleting callora-catalog-v1 (and { type: "SKIP_WAITING" }).
  • src/utils/registerServiceWorker.ts & src/main.tsx:
    • Registers /sw.js only in production builds (import.meta.env.PROD, skipped in dev and Vitest) after window load, safely ignoring unsupported browsers and registration failures.
    • Exports clearServiceWorkerApiCache() to post { type: "CLEAR_API_CACHE" } to the active worker.
  • src/utils/offlineApiCache.ts:
    • Invokes clearServiceWorkerApiCache() inside invalidateAccountCache() so worker-cached catalogue responses are dropped alongside localStorage.
  • vercel.json:
    • Adds Cache-Control: no-cache and Service-Worker-Allowed: / headers for /sw.js.
  • docs/Deployment.md:
    • Documents the caching strategy, cache versioning, account-switch invalidation, and how to bust caches on deploy.
  • src/utils/registerServiceWorker.test.ts:
    • Adds unit tests for registration (dev/test skip, unsupported browser, load-deferred registration, rejection handling), CLEAR_API_CACHE messaging, and a function-scope harness for public/sw.js covering install precache, activate cleanup, SWR hit/miss/offline, /assets/*, non-GET/Authorization/non-catalogue bypass, and CLEAR_API_CACHE.

Verification

  • npx vitest run src/utils/offlineApiCache.test.ts src/utils/registerServiceWorker.test.ts — 26/26 passed (10 in offlineApiCache.test.ts, 16 in registerServiceWorker.test.ts).
  • npm run typecheck — 0 errors in all files touched by this PR.
  • Manual verification checklist (for preview): run npm run build && npm run preview, inspect DevTools → Application → Service Workers & Cache Storage (callora-shell-v1, callora-catalog-v1), toggle Offline mode to confirm shell + last catalogue load, and verify background revalidation on reconnect.

@drips-wave

drips-wave Bot commented Nov 23, 2026

Copy link
Copy Markdown

@forked-dev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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.

Add a service worker with stale-while-revalidate for catalogue GETs and the static shell

1 participant