1. Understand the Goal: The user wants a "Service Worker Resilience Map" for a "Cross-Device Planner App." This implies a Progressive Web App (PWA) context where the app needs to function smoothly across desktop, tablet, and mobile, even with intermittent or no internet connection.
Deconstruct the Request:
Service Worker Resilience Map: A structured plan or architecture for handling network failures, caching, and synchronization.
Offline Fallbacks: What does the user see/experience when the network is down? (UI, Data).
Cache Partitioning: How is data stored? (Static assets vs. Dynamic user data vs. API responses). Versioning is key here.
Background Sync Policies: How do we handle actions created offline (e.g., adding a task) and sync them when connection returns?
Constraints: Data must be consistent across devices (eventual consistency). Conflict resolution is crucial (last-write-wins vs. manual merge). Performance must be snappy on mobile.
Authentication: Needs to handle logged-out states or token refreshes gracefully.
Drafting - Section by Section:
Introduction/Philosophy: Define the core goal: "Offline First" but "Network First" for critical data updates to ensure freshness, falling back to cache. Actually, for a planner, Stale-While-Revalidate is usually best for reading, and Background Sync for writing.
Bucket B: Static Content (Immutable): Third-party libs (e.g., React/Vue), specific versioned assets. Strategy: Cache-First, never update unless version change.
Bucket C: Dynamic Data (API Responses): User tasks, events. Strategy: Network-First (for freshness) or Stale-While-Revalidate (for speed). Let's go with SWR for read-heavy data (calendar view) and specific handling for write operations.
Bucket D: User Media (Blobs): Images attached to tasks. Strategy: Cache-First with size limits.
2. Offline Fallbacks (The UI Experience):
The "Shell": If offline, the app shell must always load.
The "Skeleton": Loading states.
The "Fallback Page": If the HTML request fails (unlikely if cached), show a generic "You are offline" page.
Data Handling:
Read: Show cached data. Show an indicator "Data might be outdated."
Write: Optimistic UI (show it worked immediately), store in IndexedDB/Local Store, queue for sync.
Conflict Resolution: This is critical for cross-device.
Timestamps vs. Vector Clocks. Let's suggest "Last Write Wins" with server timestamp for simplicity, but acknowledge the need for unique UUIDs generated client-side.
4. Architecture Diagram (Text-based):
Map out the flow: Request -> SW -> Cache vs. Network.
5. Implementation Details (Code snippets/Logic):
Workbox: Mention Google's Workbox library because writing raw SW code is error-prone and verbose.
Routing: Regex for API vs. Static.
Refining the "Resilience" Aspect:
What happens if the sync fails? (Retry logic with exponential backoff).
What happens if the cache is corrupted? (Cleanup step).
Quota management (don't fill the user's hard drive).
Structuring the Output:
Use clear headings.
Use tables for the cache partitioning (easier to read).
Use bullet points for policies.
Include a section on "Edge Cases" (the "Resilience" part).
Self-Correction/Review during drafting:
Initial thought: Use LocalStorage. Correction: No, LocalStorage is synchronous and limited. Use IndexedDB (wrapped by something like idb or Dexie) for the background sync queue.
Initial thought: Cache everything. Correction: No, cache API responses carefully. If the user has 5 years of calendar data, don't cache it all. Only cache "Recent + Upcoming" + "Starred".
Final Polish: Ensure the tone is professional and architectural. (Producing the actual Markdown response now).
This Service Worker Resilience Map outlines the architecture for a Cross-Device Planner App (e.g., a PWA). The goal is to ensure zero data loss during connectivity drops and a seamless user experience across desktop and mobile interfaces.
1. Architecture Overview: The "Offline-First" Paradigm
This app relies on IndexedDB (IDB) as the source of truth for the client, synchronizing with the server API when connectivity is restored. The Service Worker (SW) acts as a network proxy and sync manager.
2. Cache Partitioning Strategy
We will categorize assets into four distinct partitions to manage storage quotas and update cycles efficiently.
Partition Name
Contents
Caching Strategy
Update Policy
A. App Shell
index.html, CSS, JS bundles, UI Framework, Logo.
Cache-First (Stale-While-Revalidate)
Active removal of old versions on new SW activation (activate event).