-
Notifications
You must be signed in to change notification settings - Fork 313
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
How should import() work in service workers? #1585
Comments
xref #1356 which is the previous discussion in this space I was thinking of. Option 1.5 seems like a great approach. I like the strong policy decision about the modulePreload/import install boundary and think this makes sense in a module world and could make for a more sane mental model for developers, but am not deeply invested in that. Whereas I am deeply invested in avoiding option 2 letting ServiceWorkers process their own fetch events! As always, thank you for your truly fantastic write-ups. |
Option 1.5 seems pretty reasonable to me! Something I'm curious about is if/how modules loaded via |
Small bikeshed: I think Also, should we make this work for importScripts as well? I mean, why should importScripts have to parse/compile/execute for offline caching? Another strawperson: self.addEventListener('install', evt => {
evt.waitUntil(async function() {
await evt.installScript('foo.js', 'classic');
await evt.installScript('bar.js', 'module');
}());
}); Note, I'd prefer not to hang this method off of |
This is why I went with Putting this on the install event is obviously a better place for this! Edit: I've updated the example to use |
@jakearchibald Is there a timeline on when this will be done? Thanks! |
I don't have a specific timeline for this, sorry |
I can work on this in Q3, with some guidance from the content layer. Do you have an estimate on how many engineering hours is needed for this? Thanks! |
I can try and get the spec and tests ready for then. @mfalken any idea on the eng hours? |
I would estimate about 1 month of development. |
Whatever exceptions we make for 'local' URLs in #1595 should be made here too. |
Has any progress been made on this front recently? Would be great to be able to properly import a module. |
Guess I will have to still resort to using a custom version of shimport + a custom loader and i wish that i could just |
please allow imports for service workers soon! |
@jakearchibald are there any updates on supporting dynamic import() for service workers? With the adoption of service workers for manifest v3 browser extensions, there is increasing interest from extension developers and browser vendors to support this functionality for extension service workers. However I think this would be useful to support for all service workers in general. |
It isn't something I personally have time for right now. If someone else wants to step in and do the work, I'm happy to review. |
So is it possible to load third party modules in service workers right now? I looked at Shimport per @jimmywarting 's suggestion but shimport creates |
@mikemaccana Shimport fallbacks to eval if it's inside a worker that do not have DOM. so shimport still works in service workers |
Wait I think I've confused |
does firefox not support imports in service workers? here is how i do it: It works in google chrome, but not in mozilla firefox: MDN says it should work:
caniuse says it should work: |
We blocked
import()
in service workers because we weren't sure how they should work, but it wasn't intended to be a forever-fix. Now that modules are becoming more popular, and browsers now support module service workers, how shouldimport()
work?Some thoughts:
Some ideas:
Other ideas considered
Option 1
Before the service worker is "installed", calls to
import()
are fetched, bypassing all service workers. These module resources are cached along with the service worker script.After the service worker is installed, calls to
import()
will only use resources that are cached along with the service worker script, otherwise they will fail.Eg:
importScripts
worksAnother idea:
Option 2
Before the service worker is "activated", calls to
import()
just go to the network, bypassing all service workers.After the service worker is "activated", calls to
import()
will go via that service worker's"fetch"
event.We could overcome the dependencies issue with something like
cache.addModule('./big-script.js')
, which would crawl the tree similar tomodulepreload
. That means we're having to parse the modules, but it doesn't need to execute them.I preferred "option 2" when it was just in my head, but now I've written it down and thought it through, I don't think I like it. So, what about:
Option 1.5
Before the service worker is "installed", calls to
import()
fail.Before the service worker is "installed", calls to
installEvent.installModule(url)
crawl a module tree similar tomodulepreload
, and cache the script along with the service worker script. This returns a promise that indicates success. It isn't necessary to pass this towaitUntil
, but it doesn't hurt to do so.After the service worker is "installed", calls to
import()
will only use resources that are cached along with the service worker script, otherwise they will fail.After the service worker is "installed", calls to
installEvent.installModule(url)
reject.Eg:
Thoughts @wanderview @asutherland @jeffposnick @mfalken @youennf?
The text was updated successfully, but these errors were encountered: