Reload service worker dynamically

529 views
Skip to first unread message

Ibrahim

unread,
Apr 5, 2022, 10:58:12 AM4/5/22
to Chromium Extensions
Hello,

In MV2 I used to reload the background page whenever the user updated settings through the options page. I was doing the reload with the window.location.reload() method so it was always starting clean with the updated settings.

Is there any quick alternative for location.reload() for service workers in MV3?

Simeon Vincent

unread,
Apr 5, 2022, 7:02:31 PM4/5/22
to Chromium Extensions, Ibrahim
There's no directly equivalent capability to window.location.reload() in service workers, but chrome.runtime.reload() is available. Note that this method will reload the entire extension, not just the background context. As such, it may have unexpected consequences. That said, it shouldn't be necessary to reload the extension to pick up options changes.

A more common pattern is to persist user settings with chrome.storage.local and to have your background observe storage changes using chrome.storage.onChanged. Alternatively, if you use a different storage mechanism like IndexedDB, you can use message passing to notify your extension's background context when an option changes.

Simeon - @dotproto
Chrome Extensions DevRel

Cuyler Stuwe

unread,
Apr 5, 2022, 11:02:52 PM4/5/22
to Simeon Vincent, Chromium Extensions, Ibrahim
What we really really really could benefit from is a way to restart content scripts (especially on e.g. extension updates or config updates).

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/d66bde3c-cad7-4310-921d-77e3ebc675e9n%40chromium.org.

hrg...@gmail.com

unread,
Apr 6, 2022, 12:08:03 AM4/6/22
to Chromium Extensions, Simeon Vincent, Ibrahim
 chrome.runtime.reload() . . . . may have unexpected consequences.

Indeed, one consequence is that all extension tabs are forcefully closed. So it's not possible to use this method to reload settings when the user is working on the options page.

It would be very handy to have a chrome.runtime.reloadServiceWorker() method.

Simeon Vincent

unread,
Apr 8, 2022, 6:32:28 PM4/8/22
to hrg...@gmail.com, Chromium Extensions, Ibrahim
What we really really really could benefit from is a way to restart content scripts (especially on e.g. extension updates or config updates). - Cuyler

There are significant challenges with trying to "restart" content scripts on a page at the platform level. Developers are better suited to make decisions about how to react to extension update events and (re)inject content scripts into pages than the browser, as we can't remove injected scripts and can't predict the side effects script injection will have.  

That said, it should be possible to create an opinionated library/framework that uses the current capabilities of the extension platform to implement something like this. 

Simeon - @dotproto
Chrome Extensions DevRel

Cuyler Stuwe

unread,
Apr 8, 2022, 6:36:35 PM4/8/22
to Simeon Vincent, Chromium Extensions, Ibrahim, hrg...@gmail.com
Firefox definitely unbinds event listeners on content scripts on extension restart (while Chrome does not).

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

Simeon Vincent

unread,
Apr 8, 2022, 6:42:20 PM4/8/22
to Cuyler Stuwe, Chromium Extensions, Ibrahim, hrg...@gmail.com
That's a good point. Would you mind opening a feature request on crbug.com?

Simeon - @dotproto
Chrome Extensions DevRel

Reply all
Reply to author
Forward
0 new messages