Browse Source

Merge pull request #3 from technocapeman/main

Changed service worker to use Precaching
main
Ravi Shah 5 years ago
committed by GitHub
parent
commit
42bb3b5028
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 60
      static/service-worker.js

60
static/service-worker.js

@ -1,21 +1,51 @@
// CREDITS: https://www.pwabuilder.com/serviceworker
// CREDITS: https://github.com/umluizlima/flask-pwa/blob/master/app/static/sw.js
// File Created by [Check Commits for Name]
// This is the service worker with the Cache-first network
console.log('Hello from sw.js');
const CACHE = "pwabuilder-precache";
importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.2.0/workbox-sw.js');
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
if (workbox) {
console.log(`Yay! Workbox is loaded 🎉`);
self.addEventListener("message", (event) => {
if (event.data && event.data.type === "SKIP_WAITING") {
self.skipWaiting();
}
});
workbox.precaching.precacheAndRoute([
{
"url": "/",
"revision": "1"
}
]);
workbox.routing.registerRoute(
new RegExp('/'),
new workbox.strategies.CacheFirst({
cacheName: CACHE
})
);
workbox.routing.registerRoute(
/\.(?:js|css)$/,
workbox.strategies.staleWhileRevalidate({
cacheName: 'static-resources',
}),
);
workbox.routing.registerRoute(
/\.(?:png|gif|jpg|jpeg|svg)$/,
workbox.strategies.cacheFirst({
cacheName: 'images',
plugins: [
new workbox.expiration.Plugin({
maxEntries: 60,
maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
}),
],
}),
);
workbox.routing.registerRoute(
new RegExp('https://fonts.(?:googleapis|gstatic).com/(.*)'),
workbox.strategies.cacheFirst({
cacheName: 'googleapis',
plugins: [
new workbox.expiration.Plugin({
maxEntries: 30,
}),
],
}),
);
} else {
console.log(`Boo! Workbox didn't load 😬`);
}
Loading…
Cancel
Save