workbox: v3 bgSync replayRequests unsupported
Library Affected: workbox-backgroundSync
Browser & Platform: Google Chrome v63.0.3
I’m trying to set up background sync with Workbox v3. After #1222, I have basic functionality, and I’ve added the queueDidReplay callback to show a notification when bg sync occurs:
const showNotification = () => {
self.registration.showNotification('Background sync success!', {
body: '🎉`🎉`🎉`'
});
};
const bgSyncPlugin = new workbox.backgroundSync.Plugin(
'dashboardr-queue',
{
callbacks: {
queueDidReplay: showNotification
}
}
);
const networkWithBackgroundSync = new workbox.strategies.NetworkOnly({
plugins: [bgSyncPlugin],
});
const addEventRoute = new workbox.routing.Route(
({url}) => url.pathname === '/api/add',
networkWithBackgroundSync,
'POST'
);
workbox.routing.registerRoute(addEventRoute);
This currently works, but bg sync takes about 5 minutes to happen in Chrome, and I’d like to replay the queued requests immediately. To achieve this I tried calling
bgSyncPlugin.replayRequests()
But this doesn’t appear to work, as replayRequests is not a method of backgroundSync.Plugin:
Uncaught (in promise) TypeError: bgSyncPlugin.replayRequests is not a function
at Object.replayAndReturn [as handle] (sw.js:110)
at DefaultRouter.handleRequest (workbox-routing.dev.js:365)
at workbox.routing.self.addEventListener.event (workbox-routing.dev.js:817)
So I changed backgroundSync.Plugin to backgroundsync.Queue, and that allows me to call replayRequests. But after switching to backgroundsync.Queue, failed requests are no longer being queued, and I’m not getting any console errors to indicate what’s going wrong.
I understand that I might not be able to just swap Plugin & Queue interchangably, but if using Queue is not that solution, then how would I implement this functionality with Plugin? If Queue is the answer, what am I doing wrong?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 18 (1 by maintainers)
Yeah, for sure. To re-iterate/summarize, I know that
replayRequestsreplayRequestsisn’t supported inPluginGiven that, my questions are:
replayRequestAPI exist onPlugin?Pluginas being what I’m looking for, e.g.,Queueshould be used, how would I use it to implement my desired functionality?Queue.Queue, other developers might be as well.If I’m just out in left field and not making any sense, I’m always open to hearing that as well 😉
I don’t think so. The reason we originally changed the
Pluginclass to not inherit from theQueueclass was to avoid unintentional naming clashes. I think it mixes concerns too much to have plugin classes implement anything other than plugin lifecycle methods.It sounds to me like @DavidScales should be using the
Queueclass directly and creating his ownfetchDidFailmethod in an object to pass as the plugin (the same way the Plugin class does currently).