Last active
June 1, 2019 12:51
-
-
Save illvart/19d0de16d417c544d4e51526be6087c2 to your computer and use it in GitHub Desktop.
Example Workbox CLI
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
| { | |
| "devDependencies": { | |
| "babel-minify": "^0.5.0", | |
| "workbox-cli": "^4.3.1" | |
| }, | |
| "scripts": { | |
| "inject-manifest": "workbox injectManifest && minify --mangle.topLevel ./static/sw.js --outFile ./static/sw.js" | |
| } | |
| } |
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
| importScripts("https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js"); | |
| if (workbox) { | |
| console.log("Yay! Workbox is loaded \uD83C\uDF89"); | |
| } else { | |
| console.log("Boo! Workbox didn't load \uD83D\uDE2C"); | |
| } | |
| // Inject precache-manifest | |
| workbox.precaching.precacheAndRoute([]); | |
| // gstatic CDNs | |
| workbox.routing.registerRoute( | |
| /.*\.gstatic\.com\//, | |
| new workbox.strategies.StaleWhileRevalidate({ | |
| cacheName: "gstatic" | |
| }) | |
| ); | |
| // Cache Google Fonts stylesheets | |
| workbox.routing.registerRoute( | |
| /^https:\/\/fonts\.googleapis\.com\//, | |
| new workbox.strategies.StaleWhileRevalidate({ | |
| cacheName: "google-fonts-stylesheets" | |
| }) | |
| ); | |
| // Cache jsDelivr | |
| workbox.routing.registerRoute( | |
| /^https:\/\/cdn\.jsdelivr\.net\//, | |
| new workbox.strategies.StaleWhileRevalidate({ | |
| cacheName: "jsdelivr" | |
| }) | |
| ); | |
| // Handle any images | |
| workbox.routing.registerRoute( | |
| /\.(?:jpg|jpeg|png|gif|webp|ico|svg)$/, | |
| new workbox.strategies.CacheFirst({ | |
| cacheName: "illvart-images", | |
| plugins: [ | |
| new workbox.expiration.Plugin({ | |
| maxEntries: 60, | |
| maxAgeSeconds: 60 * 60 * 24 * 30 // 30 Days | |
| }) | |
| ] | |
| }) | |
| ); | |
| // CSS & JS | |
| workbox.routing.registerRoute( | |
| /\.(?:js|mjs|css)$/, | |
| new workbox.strategies.StaleWhileRevalidate({ | |
| cacheName: "illvart-static" | |
| }) | |
| ); | |
| // Force service worker to update immediately after installing | |
| self.addEventListener("install", function(e) { | |
| e.waitUntil(self.skipWaiting()); | |
| }); | |
| self.addEventListener("activate", function() { | |
| self.clients.claim(); | |
| }); |
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
| module.exports = { | |
| globDirectory: "./static/", | |
| globPatterns: [ | |
| "**/*.{html,css,js,mjs,map,jpeg,jpg,png,gif,webp,ico,svg,woff2,woff,eot,ttf,otf,json,webmanifest}" | |
| ], | |
| swDest: "./static/sw.js", | |
| swSrc: "precache-manifest.js" | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment