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

Support dynamic import() in background service worker #212

Open
larry-xu opened this issue May 12, 2022 · 4 comments
Open

Support dynamic import() in background service worker #212

larry-xu opened this issue May 12, 2022 · 4 comments
Labels
supportive: chrome Supportive from Chrome supportive: firefox Supportive from Firefox topic: service worker Related to service worker background scripts

Comments

@larry-xu
Copy link

As documented in w3c/ServiceWorker#1585, dynamically importing modules is not supported in a service worker environment. This limitation applies to background service workers in MV3 as well. Ideally dynamic import() would be supported for service workers generally, but it doesn't look like there has been any progress on that front within the last year. Are there any plans to support this for browser extensions specifically?

This feature would be useful for extensions that require large dependencies that may only be necessary to load in certain contexts. For example, in our extension we dynamically load specific locale files after detecting the user's language.

@tophf
Copy link

tophf commented May 12, 2022

Chromium bug: https://crbug.com/1198822. The only workaround for now is a dynamic importScripts but it executes in global scope, not as a module, and it also requires boilerplate duplication of code in SW install event.

@dotproto
Copy link
Member

dotproto commented Jun 8, 2022

Are there any plans to support this for browser extensions specifically?

On the Chrome side we're considering making an extension-specific allowance for dynamic import in module service workers. I believe @wanderview has made public comments to this effect in service worker issues but I don't have links handy at the moment. I don't anticipate that we will be able to explore this further before the end of 2022.

The only workaround for now is a dynamic importScripts … also requires boilerplate duplication of code in SW install event.

For clarity, service workers cannot call importScripts() after it leaves the "install" phase. https://w3c.github.io/ServiceWorker/#importscripts

You should not need to call importScripts() from within the install event handler. Since this method executes synchronously, you can call it directly from the root scope of a classic service worker or during the service worker's install event. Either works.

@tophf
Copy link

tophf commented Jun 9, 2022

For clarity, service workers cannot call importScripts() after it leaves the "install" phase.

Wrong. It can and it works because the essence of the trick is to have all future code present at the install phase. Once all modules were imported in the install event (when the extension was installed) we can import any of these modules dynamically (inside asynchronous code) at any time.

@hanguokai
Copy link
Member

According to past meeting minutes, I add supportive labels from Chrome and Firefox. I also open an issue at Web service worker, just let them know this WECG proposal for extension service worker.

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 topic: service worker Related to service worker background scripts
Projects
None yet
Development

No branches or pull requests

5 participants