[Frontend] Optimize service worker caching strategy for offline-first experience#355
Open
CodingBabe-1 wants to merge 1 commit into
Open
Conversation
…erience Overhaul Workbox caching strategies per AetherEdu#276 acceptance criteria: - HTML navigation: NetworkFirst (was StaleWhileRevalidate) with 5s timeout - API GET: StaleWhileRevalidate (was NetworkFirst) for all API reads - Images: separated into own CacheFirst with 50 MB size limit - SW version bumped v4 -> v5 for automatic cache eviction Enhance OfflineIndicator to show available cached course count alongside pending sync actions with adaptive banner messaging. Closes AetherEdu#276
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📋 Summary
This PR overhauls the Workbox service worker caching strategies to deliver a truly offline-first experience for AetherMint learners, matching all six acceptance criteria defined in #276. It also enhances the offline indicator banner to surface available cached content alongside queued sync actions.
🔄 Caching Strategy Changes (
frontend/public/sw.js)StaleWhileRevalidateNetworkFirst(5s timeout)NetworkFirst(3s timeout)StaleWhileRevalidateCacheFirst+ 50 MB size limitCacheableResponsePluginensures only 0/200 responses cachedStaleWhileRevalidateStaleWhileRevalidateCacheFirst(30d)CacheFirst(30d)CacheFirst(1y, immutable)CacheFirst(1y, immutable)Key Implementation Details
v4→v5; the existingactivatehandler automatically purges allaethermint-*-v4caches on activationcacheableResponsemodule destructured fromself.workboxfor consistency with existing import patternaethermint-images-v5cache, separate fromaethermint-static-v5for scripts/styles/fonts7a/7b/7c/7d→8a/8b/8c/8dto match renumbering🟢 Enhanced Offline Indicator (
frontend/src/components/PWA/OfflineIndicator.tsx)Before (v4)
After (v5)
useOfflineCourseshook — zero new data fetching, zero performance impact when online✅ Acceptance Criteria Mapping
NetworkFirststrategy with 5s timeout on navigation routesCacheFirst(30d) — already existed, unchanged/api/*useStaleWhileRevalidate(specific read-only pattern already did)CacheFirst+maxAgeSize: 50 MB+CacheableResponsePlugin({ statuses: [0, 200] })SW_VERSIONbumped tov5; activate handler evicts all old-version caches🧪 Test Results
New/Updated Tests (
OfflineIndicator.test.tsx)renders nothing when onlinerenders the banner with role="alert" when offlineshows a pending-count chip when there are queued offline actionsshows available cached content count when courses are available offline← NEWshows both pending and available counts when both are present← NEWhides the banner once dismissed and persists the choicehonours a custom storage keydoes not throw when localStorage is unavailableClear → Confirm two-step wipes offline data and shows success copysurfaces a clear-failure message inline when clearAll rejectsPre-existing Failures (Unrelated)
serviceWorkerRegistration.test.tsfailures — jsdom environment limitation (no real Service Worker API), unchanged by this PRLint & TypeCheck
no-consolein hooks)📁 Files Changed
frontend/public/sw.jsfrontend/src/components/PWA/OfflineIndicator.tsxfrontend/src/components/PWA/__tests__/OfflineIndicator.test.tsxTotal: 3 files, +163 / -16 lines
🔍 Reviewer Notes
aethermint-static-v4toaethermint-images-v5. Old cached images in the static cache will be cleaned up automatically by the activate handler.CacheableResponsePluginis destructured fromself.workboxalongsiderouting,strategies, etc. — consistent with the existing pattern (code review feedback addressed).7a→8a, etc.) updated to match the renumbered strategy listing in the header comment.useOfflineCoursesreads from IndexedDB on mount, but theOfflineIndicatoronly renders whenisOnline === false— zero performance impact when connected.🚀 How to Test
Closes #276