fix(extension): use offscreen document for persistent WebSocket connection#569
Open
lyfuci wants to merge 1 commit intojackwener:mainfrom
Open
fix(extension): use offscreen document for persistent WebSocket connection#569lyfuci wants to merge 1 commit intojackwener:mainfrom
lyfuci wants to merge 1 commit intojackwener:mainfrom
Conversation
…ction Chrome MV3 Service Workers are suspended by the browser when idle, causing the WebSocket connection to the daemon to drop silently. The existing keepalive alarm re-connects after wake, but there is a window where commands fail and the CLI reports 'not connected'. Fix: move the WebSocket into a chrome.offscreen document whose lifetime is tied to the browser window, not the Service Worker. The SW becomes a thin message-relay layer; the offscreen document owns the connection. Changes: - extension/src/offscreen.ts (new): WebSocket host with auto-reconnect - extension/offscreen.html (new): offscreen document entry point - extension/src/background.ts: delegate WS ops to offscreen via messages - extension/manifest.json: add 'offscreen' permission - extension/vite.config.ts: add offscreen as a build entry Tested: extension stays connected after >2h with Chrome idle / SW suspended on Linux.
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.
Problem
Chrome MV3 Service Workers are suspended by the browser after a short idle period (~30s). When this happens, the WebSocket connection to the opencli daemon drops silently. Although a
keepalivealarm fires every ~24s to wake the SW and callconnect(), there is a gap window where:Users experience this as the extension randomly disconnecting, requiring them to manually click the extension icon to wake it.
Root Cause
The WebSocket connection lives inside the Service Worker. Chrome's MV3 Service Worker lifecycle does not support persistent connections — it is designed to be ephemeral.
Fix
Move the WebSocket connection into a
chrome.offscreendocument. Offscreen documents have a stable lifetime tied to the browser window (not the SW), so the connection survives SW sleep/wake cycles.The Service Worker becomes a thin relay layer:
chrome.runtime.sendMessageChanges
extension/src/offscreen.tsextension/offscreen.htmlextension/src/background.tsextension/manifest.jsonoffscreenpermissionextension/vite.config.tsoffscreenbuild entryTesting
Tested on Linux (Chrome 144) with the browser idle for 2+ hours. Extension remained connected throughout — no manual reconnection required.
Previously this would show
[MISSING] Extension: not connectedafter Chrome suspended the SW.