Skip to content

Instantly share code, notes, and snippets.

@nip10
Last active March 3, 2020 17:59
Show Gist options
  • Save nip10/83a3b0e70ffa571ea0587b77feef9ba1 to your computer and use it in GitHub Desktop.
Save nip10/83a3b0e70ffa571ea0587b77feef9ba1 to your computer and use it in GitHub Desktop.
workbox background sync
/* eslint-disable no-undef */
/* eslint-disable no-restricted-globals */
if ("function" === typeof importScripts) {
importScripts(
"https://storage.googleapis.com/workbox-cdn/releases/5.0.0/workbox-sw.js"
);
//The new installed service worker replaces the old service worker immediately
self.skipWaiting();
self.addEventListener("install", function(event) {
self.skipWaiting();
});
self.addEventListener("sync", function(event) {
if (event.tag === "workbox-background-sync:myQueueName") {
console.log("sync event fired");
}
});
self.addEventListener("message", event => {
if (event.data === "replayRequests") {
console.log("firing replayRequests");
}
});
workbox.setConfig({ debug: true });
const { precaching, routing, backgroundSync, strategies } = workbox;
// injection point for manifest files
precaching.precacheAndRoute(self.__WB_MANIFEST);
const bgSyncPlugin = new backgroundSync.BackgroundSyncPlugin("myQueueName", {
maxRetentionTime: 24 * 60 // Retry for max of 24 Hours
});
routing.registerRoute(
({ url }) => url.pathname.includes("testsw"),
new strategies.NetworkOnly({
plugins: [bgSyncPlugin]
}),
"POST"
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment