Skip to content

Instantly share code, notes, and snippets.

@masalib
Created August 12, 2018 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masalib/fdc68f46b40a536d25c4b2c23670bae6 to your computer and use it in GitHub Desktop.
Save masalib/fdc68f46b40a536d25c4b2c23670bae6 to your computer and use it in GitHub Desktop.
PWA:Workboxのサンプル2
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