Open jeffposnick opened 5 years ago
There isn't a race condition here, as there's a microtask checkpoint before the has ever been evaluated flag is set.
When spec land calls JavaScript, microtasks will always happen before the next line in the spec, due to the clean up after running script steps.
Regardless, I can't decide if loadz0r should be sync in workers, or if it should be 'async' for consistency. My gut reaction is that it should be sync, but I don't have any good reasoning.
Yeah, it should probably be sync. I need to update it for some breaking changes from rollup anyway.
Until then: We ship our own loadz0r template anyways, so we can fix it there. I’ll tackle that in the next sprint.
Debug info
Describe the bug (I'm not sure how much of an issue this actually is, and I'd be interested in hearing from @jakearchibald in particular here.)
I was planning on using
rollup-plugin-loadz0r
in a project of my own, and was curious as to how Proxx addressed an issue I first reported at https://github.com/surma/rollup-plugin-loadz0r/issues/3. Here's the summary:From what I can tell by looking at the final, bundled Proxx service worker, the
install
event listener doesn't get registered until afterimportScripts()
is called, and bothimportScripts()
and then theinstall
listener registration happens asynchronously, following a promise resolution.My understanding was that both of those things were problematic to do asynchronously, and instead had to be done during the initial, synchronous execution of the service worker script.
To Reproduce There's inherently a race condition here, and I haven't been able to reproduce a failure, but in theory, the following could happen:
importScripts()
andinstall
event listener happen following a promise resolution, which seems like it could cause issues in certain browsers, as per the service worker specification.Expected behavior The safest thing (to my mind, at least, but correct me if I'm wrong) would be if
rollup-plugin-loadz0r
had a mode in which it detected that you were in theServiceWorkerGlobalScope
and instead of implementing an asynchronousdefine
, instead just implemented a synchronousdefined
that was effectively an alias forimportScripts()
.