feat: Add service worker
All checks were successful
Build Docker / BuildImage (push) Successful in 49s

This commit is contained in:
Nathan Woodburn 2025-01-29 18:51:28 +11:00
parent d4136a396a
commit 8ccbe2ebf1
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
4 changed files with 65 additions and 5 deletions

53
pwa/sw.js Normal file
View 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;
}
})());
}
});

View File

@ -237,18 +237,20 @@ def manifest():
host = request.host host = request.host
# Read as json # Read as json
with open("manifest.json") as file: with open("pwa/manifest.json") as file:
manifest = json.load(file) manifest = json.load(file)
url = f"https://{host}" url = f"https://{host}"
if host == "localhost:5000" or host == "127.0.0.1:5000": if host == "localhost:5000" or host == "127.0.0.1:5000":
url = "http://127.0.0.1:5000" url = "http://127.0.0.1:5000"
manifest["start_url"] = url manifest["start_url"] = url
manifest["scope"] = url manifest["scope"] = url
return jsonify(manifest) return jsonify(manifest)
@app.route("/sw.js")
def pw():
return send_from_directory("pwa", "sw.js")
# region Sol Links # region Sol Links
@app.route("/actions.json") @app.route("/actions.json")

View File

@ -274,7 +274,12 @@ Check them out here!</blockquote><img class="img-fluid" src="/assets/img/pfront.
ethdomain.addEventListener('click', () => { ethdomain.addEventListener('click', () => {
copyToClipboard(ethdomain); copyToClipboard(ethdomain);
}); });
</script>{{custom | safe}} </script>{{custom | safe}}<script>
if (typeof navigator.serviceWorker !== 'undefined') {
navigator.serviceWorker.register('sw.js')
}
</script>
<div class="d-none d-print-none d-sm-none d-md-block d-lg-block d-xl-block d-xxl-block clock" style="padding: 1em;background: #10101039;border-top-right-radius: 10px;"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 20 20" fill="none" class="fs-2"> <div class="d-none d-print-none d-sm-none d-md-block d-lg-block d-xl-block d-xxl-block clock" style="padding: 1em;background: #10101039;border-top-right-radius: 10px;"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 20 20" fill="none" class="fs-2">
<path fill-rule="evenodd" clip-rule="evenodd" d="M10 18C14.4183 18 18 14.4183 18 10C18 5.58172 14.4183 2 10 2C5.58172 2 2 5.58172 2 10C2 14.4183 5.58172 18 10 18ZM11 6C11 5.44772 10.5523 5 10 5C9.44771 5 9 5.44772 9 6V10C9 10.2652 9.10536 10.5196 9.29289 10.7071L12.1213 13.5355C12.5118 13.9261 13.145 13.9261 13.5355 13.5355C13.9261 13.145 13.9261 12.5118 13.5355 12.1213L11 9.58579V6Z" fill="currentColor"></path> <path fill-rule="evenodd" clip-rule="evenodd" d="M10 18C14.4183 18 18 14.4183 18 10C18 5.58172 14.4183 2 10 2C5.58172 2 2 5.58172 2 10C2 14.4183 5.58172 18 10 18ZM11 6C11 5.44772 10.5523 5 10 5C9.44771 5 9 5.44772 9 6V10C9 10.2652 9.10536 10.5196 9.29289 10.7071L12.1213 13.5355C12.5118 13.9261 13.145 13.9261 13.5355 13.5355C13.9261 13.145 13.9261 12.5118 13.5355 12.1213L11 9.58579V6Z" fill="currentColor"></path>