Skip to content

Instantly share code, notes, and snippets.

@cnp96
Last active November 9, 2020 18:18
Show Gist options
  • Save cnp96/4849d488d74276ed9f6791280c3f8baa to your computer and use it in GitHub Desktop.
Save cnp96/4849d488d74276ed9f6791280c3f8baa to your computer and use it in GitHub Desktop.
src/sw-custom.js
try {
if ("function" === typeof importScripts) {
importScripts(
"https://storage.googleapis.com/workbox-cdn/releases/3.5.0/workbox-sw.js"
);
// Global workbox
if (workbox) {
// console.log("Workbox is loaded");
// Disable logging
workbox.setConfig({ debug: false });
//`generateSW` and `generateSWString` provide the option
// to force update an exiting service worker.
// Since we're using `injectManifest` to build SW,
// manually overriding the skipWaiting();
self.addEventListener("install", event => {
self.skipWaiting(); // Skip to activation step - taken care in serviceWorker.ts
});
// Manual injection point for manifest files.
// All assets under build/ and 5MB sizes are precached.
try {
workbox.precaching.precacheAndRoute([]);
} catch (e) {
console.error(e);
}
// Font caching
workbox.routing.registerRoute(
new RegExp("https://fonts.(?:.googlepis|gstatic).com/(.*)"),
workbox.strategies.cacheFirst({
cacheName: "googleapis",
plugins: [
new workbox.expiration.Plugin({
maxEntries: 30,
}),
],
})
);
} else {
console.error("Workbox could not be loaded. No offline support");
}
}
} catch (e) {
console.error("Unable to install service worker. Possible network error.", e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment