Created
August 12, 2018 11:18
-
-
Save masalib/fdc68f46b40a536d25c4b2c23670bae6 to your computer and use it in GitHub Desktop.
PWA:Workboxのサンプル2
This file contains 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/3.4.1/workbox-sw.js'); | |
if (workbox) { | |
// Force development builds | |
workbox.setConfig({ debug: true }); | |
// Force production builds | |
//workbox.setConfig({ debug: false }); | |
console.log(`Yay! Workbox is loaded 🎉`); | |
workbox.routing.registerRoute( | |
new RegExp('.*\.js'), | |
workbox.strategies.networkFirst() | |
); | |
workbox.routing.registerRoute( | |
new RegExp('.*\.html'), | |
workbox.strategies.networkFirst() | |
); | |
workbox.routing.registerRoute( | |
// Cache CSS files | |
/.*\.css/, | |
// Use cache but update in the background ASAP | |
workbox.strategies.staleWhileRevalidate({ | |
// Use a custom cache name | |
cacheName: 'css-cache', | |
}) | |
); | |
workbox.routing.registerRoute( | |
// Cache image files | |
/.*\.(?:png|jpg|jpeg|svg|gif)/, | |
// Use the cache if it's available | |
workbox.strategies.cacheFirst({ | |
// Use a custom cache name | |
cacheName: 'image-cache', | |
plugins: [ | |
new workbox.expiration.Plugin({ | |
// Cache only 20 images | |
maxEntries: 20, | |
// Cache for a maximum of a week | |
maxAgeSeconds: 7 * 24 * 60 * 60, | |
}) | |
], | |
}) | |
); | |
} else { | |
console.log(`Boo! Workbox didn't load 😬`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment