-
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
When should "imported scripts updated flag" be unset? #1021
Comments
I quite like the currently specced behaviour, as it means I can do something like this: addEventListener('install', event => {
importScripts('heavy-script.js');
});
addEventListener('fetch', event => {
const url = new URL(event.request.url);
if (url.pathname == '/whatever/') {
importScripts('heavy-script.js');
event.respondWith(heavyThing());
}
}); I can cache some heavy scripts, but avoid the cost of executing them on each service worker startup. I can take that hit only when I need it. Does it make sense to do this? We should think about this in a world with |
Note, we've had a gecko bug to implement this for over a year, but I was never sure if it was really desired: |
Our thought flow over the discussion has been:
(2) sort of assumed that a specific service worker version and its (cached) imported script resources should map 1:1. So, it currently sets the flag upon a successful install.
This is a question about until when we should allow fetching the scripts from the network: either until the first SW run during an Update or until the successful install of a SW version. In addition to that, #839 (comment) should be discussed in this thread as well - whether we should allow the imported scripts to run in async tasks or not. @jakearchibald's example above suggests a usage where cached imported scripts can be used in async tasks while offline. |
Yeah, I think dynamic importing is useful, and probably more likely once |
Personally I'm inclined to keep importScripts() as a sync script eval thing only right now. Reasons:
|
Hmm, although it's really difficult to feature-detect this. @wanderview how do you feel about |
Well, if we want to support I guess it comes down to what people expect to actually implement. If we keep the spec text that allows |
On reflection, I'm leaning toward allowing it only until the initial eval. AFAICT, the purpose of the byte-for-byte check is to examine whether it needs to continue to Install or not. If the spec EDIT: This reasoning is under the decision of #839 (comment). |
@mattto pointed in #839 (comment) that my previous comment is not true. I agree that the byte-by-byte check for Install is orthogonal to the discussion of the timing of setting the flag. |
I think dynamic imports are useful, but given that:
…I'm happy for us to align the spec with browsers for We could achieve this by allowing |
I don't think that would work if A general problem will be how to handle the race between Still, I feel like that can be handled at a later time. I'd like to see |
Yeah, we'd have to come up with some rule there, like it goes to the network before activate.
Basically what the spec says for |
Kind of. But in this future world someone could call Really I don't like this, though, because it could work fine in a fast local dev environment and then fail later in a slower production environment. Seems like a footgun we should try to avoid if we can. Edit: Maybe we could make install completion wait until all outstanding |
With #1021 (comment) and #1021 (comment), I'm also happy for us to align the spec with the implementations. One thing I'd like to check with the current implementations is whether they don't even allow to get a script from the cache when |
With a simple test code: self.oninstall = e => {
importScripts('importedscript1.js');
};
self.onfetch = e => {
importScripts('importedscript2.js');
}; Both importedscript1.js and importedscript2.js execute (without a network error) in both Firefox and Chrome without |
Very interesting @jungkees! Here's an expanded test, and live demo. Results:
So I withdraw "I'm happy for us to align the spec with browsers for importScripts", since the browsers aren't particularly aligned. Also, allowing synchronous network fetches in functional events (which both browsers do) seems like a really bad idea. Aligning to the spec seems like a better idea. |
We don't do this for things like import whatever from "./thing-i-always-need.js";
addEventListener('install', event => {
event.waitUntil(
import('./thing-i-sometimes-need.js')
);
}); |
Mainly because I wasn't aware the syntax made it easy like this. Works for me. |
ServiceWorkerContextRequestHandler should emit a network error instead of falling back to network when it cannot handle a request. Otherwise, a service worker can be spawned that did not load via our custom ServiceWorkerWriteToCacheJob/ServiceWorkerReadFromCacheJob jobs, resulting in a running worker whose ServiceWorkerVersion has not been properly initialized. I suspect this can cause the bug 485900. This patch: - Changes network fallback for failure cases to an ERR_FAILED network error. - As an exception, an installed worker loading an unstored script still results in network fallback. This should be deprecated and removed eventually, see w3c/ServiceWorker#1021. - Changes the behavior for a new worker loading an already stored script (i.e., calling importScripts() for the same script multiple times). Before this patch, we would fallback to network for this script. Now, we read the stored script. This is not yet codified in the spec but is expected to have almost no real-world impact and has support on w3c/ServiceWorker#1041 BUG=485900,678899 Review-Url: https://codereview.chromium.org/2602853002 Cr-Commit-Position: refs/heads/master@{#442590}
ServiceWorkerContextRequestHandler should emit a network error instead of falling back to network when it cannot handle a request. Otherwise, a service worker can be spawned that did not load via our custom ServiceWorkerWriteToCacheJob/ServiceWorkerReadFromCacheJob jobs, resulting in a running worker whose ServiceWorkerVersion has not been properly initialized. I suspect this can cause the bug 485900. This patch: - Changes network fallback for failure cases to an ERR_FAILED network error. - As an exception, an installed worker loading an unstored script still results in network fallback. This should be deprecated and removed eventually, see w3c/ServiceWorker#1021. - Changes the behavior for a new worker loading an already stored script (i.e., calling importScripts() for the same script multiple times). Before this patch, we would fallback to network for this script. Now, we read the stored script. This is not yet codified in the spec but is expected to have almost no real-world impact and has support on w3c/ServiceWorker#1041 BUG=485900,678899 Review-Url: https://codereview.chromium.org/2602853002 Cr-Original-Commit-Position: refs/heads/master@{#442590} Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src Cr-Mirrored-Commit: 815dc48c9d00b0142a2421de8f10b7cb9c5b34ab
We didn't get to a conclusion in this thread, but I close this in favor of #1319. |
importScripts
has two behaviours. One where it fetches from the network and adds to its "script resource map", and another where it fetches only from the "script resource map".This allows
importScripts
during initial execution to implicitly populate the cache while the service worker is not-active, then later work without the network by using this cache.The spec flips the switch once install has completed, but @mattto and @wanderview say implementations flip the switch after the first synchronous run of the service worker script.
Should the spec match implementations?
The text was updated successfully, but these errors were encountered: