feat: Add service worker
All checks were successful
Build Docker / BuildImage (push) Successful in 49s
All checks were successful
Build Docker / BuildImage (push) Successful in 49s
This commit is contained in:
26
pwa/manifest.json
Normal file
26
pwa/manifest.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"short_name": "Nathan.Woodburn/",
|
||||
"name": "Nathan.Woodburn/",
|
||||
"icons": [
|
||||
{
|
||||
"src": "https://nathan.woodburn.au/assets/img/favicon/android-chrome-192x192.png",
|
||||
"type": "image/png",
|
||||
"sizes": "192x192"
|
||||
},
|
||||
{
|
||||
"src": "https://nathan.woodburn.au/assets/img/favicon/android-chrome-512x512.png",
|
||||
"type": "image/png",
|
||||
"sizes": "512x512"
|
||||
}
|
||||
],
|
||||
"start_url": "https://nathan.woodburn.au",
|
||||
"background_color": "#000000",
|
||||
"theme_color": "#000000",
|
||||
"display": "fullscreen",
|
||||
"orientation": "portrait",
|
||||
"id": "nathanwoodburn",
|
||||
"description": "Nathan.Woodburn/",
|
||||
"scope": "https://nathan.woodburn.au",
|
||||
"dir": "ltr",
|
||||
"lang": "en"
|
||||
}
|
||||
53
pwa/sw.js
Normal file
53
pwa/sw.js
Normal file
@@ -0,0 +1,53 @@
|
||||
// This is the service worker with the combined offline experience (Offline page + Offline copy of pages)
|
||||
|
||||
const CACHE = "pwabuilder-offline-page";
|
||||
|
||||
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
|
||||
|
||||
const offlineFallbackPage = "404";
|
||||
|
||||
self.addEventListener("message", (event) => {
|
||||
if (event.data && event.data.type === "SKIP_WAITING") {
|
||||
self.skipWaiting();
|
||||
}
|
||||
});
|
||||
|
||||
self.addEventListener('install', async (event) => {
|
||||
event.waitUntil(
|
||||
caches.open(CACHE)
|
||||
.then((cache) => cache.add(offlineFallbackPage))
|
||||
);
|
||||
});
|
||||
|
||||
if (workbox.navigationPreload.isSupported()) {
|
||||
workbox.navigationPreload.enable();
|
||||
}
|
||||
|
||||
workbox.routing.registerRoute(
|
||||
new RegExp('/*'),
|
||||
new workbox.strategies.StaleWhileRevalidate({
|
||||
cacheName: CACHE
|
||||
})
|
||||
);
|
||||
|
||||
self.addEventListener('fetch', (event) => {
|
||||
if (event.request.mode === 'navigate') {
|
||||
event.respondWith((async () => {
|
||||
try {
|
||||
const preloadResp = await event.preloadResponse;
|
||||
|
||||
if (preloadResp) {
|
||||
return preloadResp;
|
||||
}
|
||||
|
||||
const networkResp = await fetch(event.request);
|
||||
return networkResp;
|
||||
} catch (error) {
|
||||
|
||||
const cache = await caches.open(CACHE);
|
||||
const cachedResp = await cache.match(offlineFallbackPage);
|
||||
return cachedResp;
|
||||
}
|
||||
})());
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user