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
It's a pain to get a handle to the page's SW now that we've moved to events. #174
Comments
Without always-register, this… sw.register('...', {scope: '...'}); …becomes… var sw = navigator.serviceWorker;
var workerUrl = '...';
var workerScope = '...';
if (!sw.active || sw.active.url == workerUrl && sw.active.scope == workerScope) {
sw.register(workerUrl, {scope: scope});
} |
+1 to bringing back register() remember that we changed the register() promise to return the moment the script has been parsed and eval'ed.. so the final availability is gated on oninstall, which is only going to fire if the sw is being installed, so you have to do even more acrobatics to only listen for oninstall when there's no active SW, etc... |
bringing back |
sorry I meant ready() |
Can anyone drop a snippet that resolves the above mentioned problem using the revived var sw = navigator.serviceWorker;
sw.ready().then(function(sw) {
// in this case, the scriptURL and the scope are not passed.
// if (!sw.active): how's the platform supposed to deal with the internal registration?
}); |
I don't understand ready(). How is it different from register()? |
We're going to have to spec ready() a little more specifically - I was roughly interpreting it as "as soon as everything settles" meaning that any in-process registration is complete. But that leaves a lot of room for interpretation. I guess I'd imagine developers want to put some boilerplate on the top of all their pages to the effect of: navigator.serviceWorker.register("foo.js").then(function(sw) {
return sw.ready().then(function(sw) {
// here we're guaranteed to have the above registration complete, whether it was
// already registered or not
});
}); I think another related thing that we've inadvertently decided is that the upgrade case is more or less a completely background situation.. meaning that if foo.js has updated since the last pageload (meaning there already is a registration for foo.js, but it has gone stale since the last pageload) then the above flow would still resolve immediately with the existing stale registration, and upgrading would happen in the background. This is probably part of a larger discussion though, as I don't think we've formalized that. |
@annevk I think given that last paragraph of mine, ready() and register() are very similar, except that register() means "also register a script if it hasn't already been registered" |
Yeah, we agreed that updates would happen in the background and that only if register specified a different URL or scope things would happen. Given that I don't really see how you could avoid having to write register() and use ready() instead. |
if (navigator.serviceWorker.active) {
// this page has a worker
}
navigator.serviceWorker.addEventListener('activate', function() {
// navigator.serviceWorker.active has changed
// which can only happen if it calls .replace() in oninstall
});
navigator.serviceWorker.register("foo.js").then(function(sw) {
if (sw == navigator.serviceWorker.active) {
// registration was most likely a no-op
}
else {
// sw is an installing/installed worker
}
sw.ready().then(function() {
// what does this mean?
});
}); If you want to show installation happening, you'd listen for the You'd use the promise returned by If you want to react to a change in I guess I'm asking, what does To answer the title of the ticket, |
A few things have changed since then. Is this issue still valid? |
Now we have |
Hah, went to close this, but was beaten to it by hours. Should have refreshed. |
It's sort of a pain in the butt to gate an operation on the availability of an SW now that we've removed
::ready()
. E.g.:It's simpler to always register, of course, but it feels wrong somehow. Do I just need to get over this?:
The text was updated successfully, but these errors were encountered: