(Re)connecting to the side panel from the service worker

284 views
Skip to first unread message

Idris Cheikh

unread,
Jul 3, 2025, 5:41:07 PMJul 3
to Chromium Extensions
Hello,

Is there a way to prompt the side panel to reconnect to the service worker using `chrome.runtime.connect`?

I'm using Comlink for remote procedure calls between different extension contexts (webpage, content script in iframe, side panel, service worker; see diagram below). The side panel is responsible for initiating the connection to the service worker, but this breaks when the service worker goes to sleep.

If the side panel is open but inactive, the connection isn't re-established when the service worker wakes up. Since the service worker can't trigger the side panel, RPC calls to the panel fail until the user interacts with it again.

This wasn’t an issue with a popup, which I could message to reconnect. But with the side panel, there's no way to trigger that reconnection externally.

This causes issues in my tab and bookmark manager: for example, new tabs don’t show up in the side panel if the service worker has slept, whereas they do if the service worker has not slept, or the user has interacted with the side panel (triggering a reconnection).

Since the service worker can go to sleep every ~30 seconds, I cannot realistically ask users to interact with the side panel every it does.

Is there a workaround or recommended pattern for this?

Thanks in advance!

                     +-----------------------------+
                     |     Extension Page (UI)     |
                     |-----------------------------|
                     | - Hosts iframe (Web App)    |<------------------------+
                     |                             |                         |
                     |                             |                         |
                     +-----------------------------+                         |
                              ^                                              |
                              |                                              |
                              |                                              |
                              v                                              v
                     +-----------------------------+             +-----------------------------+
                     |       Web App (in iframe)   |             |   Extension Service Worker  |
                     |-----------------------------|             |-----------------------------|
                     | - Origin: app.mywebsite.com |             | - Access to browser APIs    |
                     |                             |             |                             |
                     |                             |             +-----------------------------+
                     +-----------------------------+                         ^
                              ^                                              |
                              |                                              |
                              |                                              |
                              v                                              |
                     +-----------------------------+                         |
                     | Content Script (in iframe)  |<------------------------+
                     |-----------------------------|
                     | - Injected into Web App     |
                     | - Access to browser APIs    |
                     +-----------------------------+


Idris Cheikh

unread,
Jul 3, 2025, 5:43:25 PMJul 3
to Chromium Extensions, Idris Cheikh
Apologies for the diagram's broken formatting: please see the image below

Screenshot 2025-07-03 234236.png

woxxom

unread,
Jul 4, 2025, 2:59:09 AMJul 4
to Chromium Extensions, Idris Cheikh
The service worker can use chrome.runtime.sendMessage at its beginning to notify all extension contexts, which will then reconnect (e.g. via chrome.runtime.connect). A simpler solution would be to not using connections (ports) and switch to one-time messaging via chrome.runtime.sendMessage or web platform messaging via navigator.serviceWorker. An even simpler solution might be to use the API right in the extension page instead of the service worker.

Idris Cheikh

unread,
Jul 4, 2025, 4:02:48 AMJul 4
to Chromium Extensions, woxxom, Idris Cheikh
Hello woxxom,

Many thanks for the pointers! For some reason, I was persuaded that it was not possible to send a message to the side panel, I must have mixed that up with initiating the connection from the service worker.

Thanks again and have a good day woxxom!

Kind regards,
Idris

Reply all
Reply to author
Forward
0 new messages