[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to unload the background script from SW code [regression from MV2] #311

Open
tophf opened this issue Oct 29, 2022 · 5 comments
Open
Labels
supportive: chrome Supportive from Chrome supportive: firefox Supportive from Firefox supportive: safari Supportive from Safari

Comments

@tophf
Copy link
tophf commented Oct 29, 2022

In MV2 it was possible to use window.close() in the background script of an event page ("persistent": false in manifest.json) to immediately unload the background page.

It allows freeing up memory when you are absolutely sure that there's no point in keeping the background context alive e.g. if the extension only uses chrome.alarms and doesn't listen to any other unpredictably occurring events. These are simple timer or scheduler extensions that wake up every X minutes to spend just a few milliseconds in the background script to do things like updating the badge/icon/title of the extension action icon or pinging a remote server or other maintenance.

The problem is that service workers don't have a terminate method available for normal dedicated/shared workers because SW is a special kind of worker for a web site, but since it doesn't serve this purpose in an extension this restriction is inappropriate [1]. If an extension wakes up every minute then a half of every minute will be wasted on keeping 20MB of an unused worker.

A possible solution would be to add something like chrome.runtime.terminateBackgroundScript().

P.S. I don't suggest reinstating terminate method inside ServiceWorker object because Chromium team has been against altering the web platform behavior for extensions, exemplified in its decision to add chrome.dom API.

Note [1]: It's inappropriate because extensions don't need specifically an SW to do their job, which is to process chrome API events (completely unrelated to SW specifics) or to show their chrome-extension:// pages (already local). It's one of the many inherent restrictions of the SW technology that's not suited for what extensions do, as we can see from #72 that lists a lot of similarly inappropriate restrictions.

@tophf tophf changed the title Ability to unload the background script from code [regression from MV2] Ability to unload the background script from SW code [regression from MV2] Oct 29, 2022
@carlosjeurissen carlosjeurissen added the agenda Discuss in future meetings label Oct 31, 2022
@oliverdunk
Copy link
Member

Beyond the use case of freeing up resources when your work is done, one thing we'd be really excited about at 1Password is the ability to reliably terminate our execution context when a user tries to "lock" our extension. We store lots of sensitive user data in memory and this would give us additional confidence that when a user is finished using the extension that data has been cleared.

@xeenon xeenon added the supportive: safari Supportive from Safari label Dec 8, 2022
@Rob--W Rob--W added follow-up: chrome Needs a response from a Chrome representative supportive: chrome Supportive from Chrome supportive: firefox Supportive from Firefox labels Dec 8, 2022
@Rob--W
Copy link
Member
Rob--W commented Dec 8, 2022

@dotproto Please link the new issue in the SW repo or the thread/response once you've reached out to the spec or your platform folks.

@Rob--W Rob--W removed the agenda Discuss in future meetings label Dec 8, 2022
@rdcronin rdcronin removed the follow-up: chrome Needs a response from a Chrome representative label Sep 13, 2023
@rdcronin
Copy link
Contributor

I'm supportive of adding this functionality, but I think ideally we'd add it to the service worker spec instead of just for extension service workers.

We'll take an action item to talk to the service worker folks and see if there's appetite, but for triage, I'm removing the follow-up:chrome (which is mostly used for flagging issues that we haven't determined if we're supportive of).

@JWorthe
Copy link
JWorthe commented Oct 16, 2023

Another use case that would be served by an extension API to unload the service worker is testing that an extension continues to work correctly in MV3 after the service worker stops and starts up again.

It would be nice to have an easy path to write a unit test which does the following:

  1. The service worker starts up, adds event listeners, and does some work.
  2. The service worker is suspended. <- This is currently difficult, and would be easier with some sort of terminate API.
  3. The service worker is sent an event, and responds correctly.

See #140 for a more full explanation of this use case.

@oliverdunk
Copy link
Member

I've just filed an issue in the w3c/ServiceWorker repository to discuss this further: w3c/ServiceWorker#1696

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
supportive: chrome Supportive from Chrome supportive: firefox Supportive from Firefox supportive: safari Supportive from Safari
Projects
None yet
Development

No branches or pull requests

7 participants