Last active
March 3, 2020 17:59
-
-
Save nip10/83a3b0e70ffa571ea0587b77feef9ba1 to your computer and use it in GitHub Desktop.
workbox background sync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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