-
Notifications
You must be signed in to change notification settings - Fork 822
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
v3 bgSync Queue not queueing failed requests #1266
Comments
So The best bet is to extend Edit: You will have access to |
@prateekbh that isn't the approach we should be encouraging here. @DavidScales I get your usecase, but IMO, this is actually more an issue with DevTools / setting things up and I don't want to add an API just because it can't be tested. If you want - you can dispatch a fake sync event - forcing a replay of the requests OR you can use the Queue class directly, code off the top of my head would be something like: const queue = new workbox.backgroundSync.Queue('myQueueName');
workbox.routing.registerRoute(
({url}) => {
console.log('api hit');
return url.pathname === '/api.txt'
},
() => {
try {
await fetch(request);
} catch (err) {
queue.addRequest(request);
}
}
);
// In message listener or something:
queue.replayReqiests(); The docs for background sync and pretty sketchy re-reading them, so I'll have another attempt at that. |
makes sense, this one is pretty minimal and cleaner |
@DavidScales your code above is a mash up of the plugin and the lower level code, but the lower level code isn't a drop in replacement. I.e. only the Plugin class can be used as a plugin, the Queue is designed so that you can you it on it's own (i.e. without all the other workbox modules). I've updated the doc some more for plugin / advanced usage (Plugin / Queue) and I've added testing to doc as well so you should be able to use one of them in your codelab to do what you are looking for. https://developers.google.com/web/tools/workbox/modules/workbox-background-sync I would love any more feedback on all - and please say if it's still garbage and you want more from the docs |
@gauntface thanks for all your help, I appreciate it and apologies if I'm dragging this out too much. I think I have a better understanding of the differences between the Going forward I think I am going to drop To feel good about closing this issue though, ideally I'd like to know that its possible to use I'm currently trying to use Queue like you've suggested: const bgSyncQueue = new workbox.backgroundSync.Queue(
'dashboardr-queue',
{
callbacks: {
queueDidReplay: showNotification
}
}
);
const addEventRoute = new workbox.routing.Route(
({url}) => url.pathname === '/api/add',
args => {
fetch(args.url.href)
.catch(err => {
bgSyncQueue.addRequest(args.url.href);
});
},
'POST'
); But getting an error:
Any ideas? The docs are good, thanks for updating them! 😁 The testing section will be very helpful for codelabs. |
The error you are getting is because addRequest doesn't expect a URL string, expects a Request instance. Maybe @prateekbh do you have any bandwidth to add typechecking to |
yep will do that |
Ah ok. For posterity, changing to
Because I forgot to
But yeah it works like magic. Thanks again guys! :D This issue is good to close by me, but I'll leave it open if @prateekbh wants to use it to track his typechecking fix. |
Can we keep this open for the following:
|
Do you want the demo to not use a |
@prateekbh sorry - went auto-pilot - I meant change the docs to show off a fetch. |
Library Affected:
v3 workbox-sw
Browser & Platform:
Google Chrome v64.0.3
I'm trying to implement background sync on an API route, such that failed requests to the route are stored in a bgSync queue (IndexedDB), and resent later when online.
I originally tried using
workbox.backgroundSync.Plugin
, based on the v3 guide for Using Workbox Background Sync with other Workbox Packages. I had done the same thing previously in a v2 app, based on the v2 QueuePlugin documentation.However, I learned in #1248 that v3's
Plugin
does not supportreplayRequest
, which I would like to use**. So I am trying to use theQueue
class, which does supportreplayRequest
.My
Queue
use looks like the following:The the full app code here & hosted sample here.
Currently failed requests are not being queued, and Workbox is not throwing any errors, so I'm having trouble debugging. Any ideas?
** @gauntface I originally wanted
replayRequests
in Workbox codelabs because sometimes bgSync can be slow to occur, and I wanted a way for students to be able to test quickly. But I learned in #1249 that toggling the wifi might be sufficient to force bgSync, so I'm less confident in my particular use case, but I assume there are still other similar use cases.The text was updated successfully, but these errors were encountered: