fix: align service worker background-sync routes with real offline mutation endpoints (#946) - #1082
Merged
RUKAYAT-CODER merged 1 commit intoJul 29, 2026
Conversation
|
@dedukpe 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! 🚀 |
Contributor
|
Thank you for contributing to the project |
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
Aligns the service worker's
BackgroundSyncPluginroutes with all real offline mutation endpoints and enables actual request replay via workbox-background-sync's built-in queue replay mechanism.Closes #946
Problem
The service worker had two critical issues preventing offline mutations from being properly queued and replayed:
1. Route ordering bug — background sync route never triggered
Workbox routes are evaluated in registration order, first-match-wins. The generic
/api/NetworkFirstroute was registered before the specificPATCH /api/lessons/[id]/progressbackground sync route, meaning the generic route caught ALL API requests first — preventing theBackgroundSyncPluginfrom ever capturing failed mutations.2. Only one endpoint was registered
Only
PATCH /api/lessons/[id]/progresshad a background sync route. Mutations to notes, bookmarks, user progress, quiz results, and course progress were never queued for replay when offline.3. No-op sync handler blocked replay
The
syncevent listener calledevent.waitUntil(Promise.resolve())— effectively doing nothing. While workbox-background-sync'sQueueauto-registers its own sync listener to replay queued requests, the manual handler was redundant and confusing.Solution
Changes to
src/serviceWorker.tsReordered routes: Moved all background sync routes above the generic
/api/NetworkFirstroute so they match first for mutation requests.Registered background sync for all mutation endpoints using a single
BackgroundSyncPlugininstance ('teachLinkSyncQueue'):/api/lessons/[id]/progressPATCH/api/notesPOST,PATCH,DELETE/api/bookmarksPOST,PATCH,DELETE/api/user/progressPOST/api/quiz-resultsPOST/api/course-progressPOSTRemoved the no-op
syncevent handler — workbox-background-sync'sQueueclass automatically registers asyncevent listener on construction that callsreplayRequests(), so manual handling is unnecessary and was previously doing nothing.Switched from
NetworkFirsttoNetworkOnlystrategy for mutation routes — there's no value in caching POST/PATCH/DELETE responses. When online, the request goes through normally. When offline, theBackgroundSyncPluginqueues it in IndexedDB for replay when connectivity returns.Changes to
src/app/services/offlineSync.tsprogressendpoint mapping insimulateApiCallfrom/api/progressto/api/lessons/[id]/progressto match the actual API route.How It Works
POST /api/notes), theNetworkOnlystrategy fails with a network error.BackgroundSyncPlugin'sfetchDidFailcallback captures the failedRequestand stores it in workbox's internal IndexedDB queue.syncevent with tag'teachLinkSyncQueue', workbox'sQueue.replayRequests()replays all queued requests in FIFO order.maxRetentionTimeof 24 hours).Testing
npx tsc --noEmit— passes with zero errorsnpx eslint src/serviceWorker.ts src/app/services/offlineSync.ts— passes with zero warningsPOST/PATCH/DELETErequests to the registered endpoints route through the background sync plugin, whileGETrequests and other API paths fall through to the genericNetworkFirstcache route.