-
Notifications
You must be signed in to change notification settings - Fork 83
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
one-shots via background Fetch #100
Comments
@jakearchibald noted offline that My question is, is background fetch/cache.backgroundAdd useful enough that we don't actually need background sync? |
Here's how a chat app might use sync:
A higher level API wouldn't know the "profile" sync and "chat" sync are unrelated to the point where order doesn't matter. A higher level API wouldn't be able to coalesce the "chat" sends into a single send (if we fix #104). |
My wikipedia example requires fetching data, then reading it to find more things to fetch (the images). This would be messy without Aside from my previous post, I don't have much counter-argument aside from chanting "extensible web". Maybe @slightlyoff can talk us down? |
Why not? If we also provide some API to look at and cancel pending background fetches, the chat app could still do the legwork to coalesce the chat sends. Some care should be taken to make sure updates to this background fetch happen in a safe way without race conditions, but that seems like it wouldn't be too hard. |
But how does cache.backgroundAddAll help? Don't you still need to download the html to figure out what images to load? We would need to be able to create more fetches in the event handler, which is problematic from a privacy perspective. |
Yeah, I guess I didn't think it'd be a problem since you can create requests from a SW anyway. I guess the delayed nature makes this a problem. |
Good point |
I really like this, at least in the abstract :) @jkarlin, for your questions:
/* Document */
fetch('https://example.com/download', {
method: 'get',
background: true,
backgroundTag: 'dotcom'
});
fetch('https://example.net/download', {
method: 'get',
background: true,
backgroundTag: 'dotnet'
});
/* Service Worker */
self.addEventListener('backgroundFetchFinished', function(event) {
// whichever one finished, cancel the other
if (event.err == null)
if (event.backgroundTag == 'dotcom') {
registration.sync.getFetch('dotnet').then(function(pendingFetch) {
pendingFetch.cancel();
});
} else if (event.backgroundTag == 'dotnet') {
registration.sync.getFetch('dotcom').then(function(pendingFetch) {
pendingFetch.cancel();
});
}
}
});
That's an excellent question :) It takes care of probably 90% of the use cases that we've seen. They're all around uploading/downloading data gracefully in the offine case. |
A couple of other questions:
|
|
A thought about non-network use-cases for I have a This has to happen as part of the promise returned to You could use This seems like the sort of thing people might try to use I'm sort of imagining that it might be an event that fires when a running service worker is about to be killed (or similar), rather than ever waking a stopped service worker. |
@wibblymat in the spec FetchEvent has a waitUntil, exactly for these kinds of things where you want to do work after responding (at least that's why I think it's there). But yeah, that's currently not implemented in chrome. |
@mkruisselbrink Yes, I guess I've sort of conflated a few things here. I wanted some API that lets me say "I have some work to do. Possibly there is a batch of similar things. Please give me an opportunity to do some background work in the future. Preferably not right away, but rather when things are quiet." Background sync doesn't give me that, but it is close, so could be used to fake it. For the particular use case I mentioned the network availability isn't even that much of a problem - the task is only triggered by a successful network request being stored in the cache, so 99% of the time the sync will fire without much delay. But that isn't really relevant. My point, which I failed to make well, was that any non-network use-cases for background sync would probably be better served by some other API around scheduling background work. |
Let's move the non-network processing to issue #77 . I'm closing this issue as there are some major concerns that Background Fetch (or cache) failed to address.
There are likely many other use cases that a low-level API like Background Sync can address that the background fetch API can't. |
This is a continuation of #18 but focused on the one-shot case.
What if Fetch had a 'background' option that fired a service worker event when complete (if one is registered). There would be some way to bundles fetches together (e.g., by tag) so that only one event would fire per given bundle of fetches.
There are some advantages to this:
For privacy you'd want to restrict background fetches to be HTTPS. You would still need to put time and retry caps on them for privacy sake. For longer downloads you might require a permission or a notification (the notification could be another option to the background fetch).
Open questions:
backgroundFetchFinished
event get to run for?A simple example:
The text was updated successfully, but these errors were encountered: