-
Notifications
You must be signed in to change notification settings - Fork 312
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
importScripts() should throw? #642
Comments
I guess it would have to be allowed always during the first task after starting up, otherwise it's kind of meaningless... But no reason to allow it elsehwere I think. |
I think should not be applicable now, since the IMO exceptions are a layer on control-flow which is too much overrated in general, and in JavaScript engines are not too concerned about this |
Legacy behavior does not matter for a new environment. |
You are considering that Service Worker is a pure new environment? I don't think so. I think changing the behavior of the same build-in function just for the Service Worker scope is not a reasonable solution. |
It's not a child, it has a completely different lifecycle. It's certainly like other workers, but quite a bit different. |
Well, issue #410 is open to make ServiceWorker not inherit Worker. I still think we should do that to avoid confusion between the two. I think there are many differences in behavior already. It seems making async Is this something blink would consider implementing at this point? |
I think native Web API consistency and coherency should be followed strictly (probably one of a few aspects in software which purism is justified) In case that a new feature is required to satisfy new problems in the Service Worker specific scope, a possible solution is about introducing a new built-in function which can satisfy those needs without breaking existent interfaces. Fortunately the Worker scope allows us to be more relaxed about adding new global functions which doesn't affect to the global Window scope, "poisoning" the global scope |
I don't want to change the behavior of |
To be clear, this bug is not about making |
As an implementer I like the idea of allowing it only at startup and oninstall. Blink is starting to implement timeouts for long-running SW in order to conserve CPU/battery and prevent instances of getting "stuck", and we could easily allow startup/oninstall to have more JS execution time than other events. |
Currently, the spec has changed to allow However, I don't feel like it's a right behavior as even the cached scripts will not have any chance to execute again later when other functional events are dispatched. As the service worker's imported scripts updated flag already guarantees the network fetch will occur only during the update phase and in the later runs the cached scripts are always used (cache miss is a network error), I suggest we remove this restriction that limits the use only in oninstall.
To make it clear, the importScripts in the current spec runs only at startup as Run Service Worker algorithm just returns early when called while it's already running. |
I think this works. Thanks for the attention to detail! |
Wait, looking at the current spec I don't see the import scripts allowed flag set during top level worker script evaluation. Is that correct? It seems we should allow importScripts() to run at the top level in addition to the install event. Otherwise we are going to break a lot of existing service workers out there. |
Yes, this is what I've concerned about too. What I suggested is to remove the step checking the importscripts allowed flag in the importScripts(urls) entirely. Then, for the top level worker script evaluation, importScripts will be allowed to run, and naturally this top level evaluation runs only once as the Run Service Worker algorithm checks whether the worker's already running and returns early there if so. And as such, it seems we don't have to confine the evaluation to oninstall handler as that's just a one time event for certain script version. |
Removed the restriction of allowing it to only oninstall: 319a49e. So now it's allowed anywhere during the top level worker script evaluation, and from the second Run Service Worker that spins off and creates the environment, the imported scripts will be served from the cache. |
@jungkees I think I understand now. Sorry for my confusion. What about for a service worker being restarted from an existing registration? It seems we don't set "imported scripts updated flag" in that case since we don't run the install algorithm. Right? |
For a specific service worker version, its imported scripts updated flag is set only once during its installation phase when the installation succeeded (Install algorithm step 17). So the service worker being restarted from an existing registration should have been set its imported scripts updated flag through the installation already. So to speak, importScripts(urls) will get a new script resources from the network only when the registration updates with a new version. |
But the imported scrips updated flag is on the ServiceWorker, not the ServiceWorkerRegistration. Are the flags on ServiceWorker supposed to be persisted across browser restarts? I thought only the registration was persistent. |
Ok. I guess they are persistent since the state is on ServiceWorker. My mental model was different from the spec. Sorry for my confusion. |
Ah.. I see. They are persistent as you noted. Closing. |
I thought the idea was that
importScripts()
was only available during the installation event as it's a synchronous API. And that outside of that specific context we'd make it throw. It seems sad to allow such a synchronous all the time.The text was updated successfully, but these errors were encountered: