-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
:(
- Loading branch information
Showing
2 changed files
with
36 additions
and
63 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,47 @@ | ||
importScripts('https://cdn.jsdelivr.net/npm/workbox-cdn/workbox/workbox-sw.js'); | ||
// This is the "Offline page" service worker | ||
|
||
const HTML_CACHE = "html"; | ||
const JS_CACHE = "javascript"; | ||
const STYLE_CACHE = "stylesheets"; | ||
const IMAGE_CACHE = "images"; | ||
const FONT_CACHE = "fonts"; | ||
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js'); | ||
|
||
const CACHE = "pwabuilder-page"; | ||
|
||
// TODO: replace the following with the correct offline fallback page i.e.: const offlineFallbackPage = "offline.html"; | ||
const offlineFallbackPage = "https://alplox.github.io/la-tele/"; | ||
|
||
self.addEventListener("message", (event) => { | ||
if (event.data && event.data.type === "SKIP_WAITING") { | ||
self.skipWaiting(); | ||
} | ||
}); | ||
|
||
workbox.routing.registerRoute( | ||
({event}) => event.request.destination === 'document', | ||
new workbox.strategies.NetworkFirst({ | ||
cacheName: HTML_CACHE, | ||
plugins: [ | ||
new workbox.expiration.ExpirationPlugin({ | ||
maxEntries: 10, | ||
}), | ||
], | ||
}) | ||
|
||
); | ||
self.addEventListener('install', async (event) => { | ||
event.waitUntil( | ||
caches.open(CACHE) | ||
.then((cache) => cache.add(offlineFallbackPage)) | ||
); | ||
}); | ||
|
||
workbox.routing.registerRoute( | ||
({event}) => event.request.destination === 'script', | ||
new workbox.strategies.StaleWhileRevalidate({ | ||
cacheName: JS_CACHE, | ||
plugins: [ | ||
new workbox.expiration.ExpirationPlugin({ | ||
maxEntries: 15, | ||
}), | ||
], | ||
}) | ||
); | ||
if (workbox.navigationPreload.isSupported()) { | ||
workbox.navigationPreload.enable(); | ||
} | ||
|
||
workbox.routing.registerRoute( | ||
({event}) => event.request.destination === 'style', | ||
new workbox.strategies.StaleWhileRevalidate({ | ||
cacheName: STYLE_CACHE, | ||
plugins: [ | ||
new workbox.expiration.ExpirationPlugin({ | ||
maxEntries: 15, | ||
}), | ||
], | ||
}) | ||
); | ||
self.addEventListener('fetch', (event) => { | ||
if (event.request.mode === 'navigate') { | ||
event.respondWith((async () => { | ||
try { | ||
const preloadResp = await event.preloadResponse; | ||
|
||
workbox.routing.registerRoute( | ||
({event}) => event.request.destination === 'image', | ||
new workbox.strategies.StaleWhileRevalidate({ | ||
cacheName: IMAGE_CACHE, | ||
plugins: [ | ||
new workbox.expiration.ExpirationPlugin({ | ||
maxEntries: 15, | ||
}), | ||
], | ||
}) | ||
); | ||
if (preloadResp) { | ||
return preloadResp; | ||
} | ||
|
||
workbox.routing.registerRoute( | ||
({event}) => event.request.destination === 'font', | ||
new workbox.strategies.StaleWhileRevalidate({ | ||
cacheName: FONT_CACHE, | ||
plugins: [ | ||
new workbox.expiration.ExpirationPlugin({ | ||
maxEntries: 15, | ||
}), | ||
], | ||
}) | ||
); | ||
const networkResp = await fetch(event.request); | ||
return networkResp; | ||
} catch (error) { | ||
|
||
const cache = await caches.open(CACHE); | ||
const cachedResp = await cache.match(offlineFallbackPage); | ||
return cachedResp; | ||
} | ||
})()); | ||
} | ||
}); |
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