fix: use pagehide instead of beforeunload for tracking closing pages#2720
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthroughThe pull request changes the client shutdown signal from the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/mockServiceWorker.js (1)
30-45: Avoid callingself.clients.matchAll()on every message.
allClientsis only used inside theCLIENT_CLOSEDbranch, but it now runs for every inbound message — includingKEEPALIVE_REQUESTevery 5s and everyINTEGRITY_CHECK_REQUEST/MOCK_ACTIVATE. Move thematchAllcall inside the branch to restore prior behavior.♻️ Proposed fix
- const allClients = await self.clients.matchAll({ - type: 'window', - }) - if (event.data === 'CLIENT_CLOSED') { activeClientIds.delete(clientId) + const allClients = await self.clients.matchAll({ + type: 'window', + }) + const remainingClients = allClients.filter((client) => { return client.id !== clientId }) // Unregister itself when there are no more clients if (remainingClients.length === 0) { self.registration.unregister() } }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/mockServiceWorker.js` around lines 30 - 45, Move the expensive self.clients.matchAll() call into the CLIENT_CLOSED branch so it only runs when handling 'CLIENT_CLOSED'; currently const allClients is computed for every message. Inside the if (event.data === 'CLIENT_CLOSED') block, call const allClients = await self.clients.matchAll({ type: 'window' }) and then compute remainingClients by filtering out clientId, delete from activeClientIds, and call self.registration.unregister() if remainingClients.length === 0; remove the top-level allClients declaration so KEEPALIVE_REQUEST/INTEGRITY_CHECK_REQUEST/MOCK_ACTIVATE paths no longer invoke matchAll.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/mockServiceWorker.js`:
- Around line 30-45: Move the expensive self.clients.matchAll() call into the
CLIENT_CLOSED branch so it only runs when handling 'CLIENT_CLOSED'; currently
const allClients is computed for every message. Inside the if (event.data ===
'CLIENT_CLOSED') block, call const allClients = await self.clients.matchAll({
type: 'window' }) and then compute remainingClients by filtering out clientId,
delete from activeClientIds, and call self.registration.unregister() if
remainingClients.length === 0; remove the top-level allClients declaration so
KEEPALIVE_REQUEST/INTEGRITY_CHECK_REQUEST/MOCK_ACTIVATE paths no longer invoke
matchAll.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 8502e0e3-58c7-4811-8ec1-bc18d29611ad
📒 Files selected for processing (3)
src/browser/sources/service-worker-source.tssrc/mockServiceWorker.jstest/browser/msw-api/unregister.test.ts
Root cause
beforeunloadgives no means to know if upstream listener has prevented it.Changes
pagehideevent instead ofbeforeunload. This will respect the developer's decision to prevent page closure and will not send theCLIENT_CLOSEDmessage to the worker in that case.