From abed85a1e33461247304bb2efeb6cb835bee99a4 Mon Sep 17 00:00:00 2001
From: rimu <3310831+rimu@users.noreply.github.com>
Date: Wed, 28 Feb 2024 17:42:49 +1300
Subject: [PATCH] piefed is a pwa
---
app/static/js/scripts.js | 15 ++++++++-
.../{site.webmanifest => manifest.json} | 2 +-
app/static/service_worker.js | 31 +++++++++++++++++++
app/templates/base.html | 2 +-
4 files changed, 47 insertions(+), 3 deletions(-)
rename app/static/{site.webmanifest => manifest.json} (94%)
create mode 100644 app/static/service_worker.js
diff --git a/app/static/js/scripts.js b/app/static/js/scripts.js
index a00aea38..6fe448a1 100644
--- a/app/static/js/scripts.js
+++ b/app/static/js/scripts.js
@@ -549,4 +549,17 @@ function displayTimeTracked() {
if(timeSpentElement && timeSpent) {
timeSpentElement.textContent = formatTime(timeSpent)
}
-}
\ No newline at end of file
+}
+
+/*register a service worker */
+if ('serviceWorker' in navigator) {
+ window.addEventListener('load', function() {
+ navigator.serviceWorker.register('/static/service_worker.js', {scope: '/static/'}).then(function(registration) {
+ // Registration was successful
+ // console.log('ServiceWorker2 registration successful with scope: ', registration.scope);
+ }, function(err) {
+ // registration failed :(
+ console.log('ServiceWorker2 registration failed: ', err);
+ });
+ });
+}
diff --git a/app/static/site.webmanifest b/app/static/manifest.json
similarity index 94%
rename from app/static/site.webmanifest
rename to app/static/manifest.json
index aaa029d9..a8538cf1 100644
--- a/app/static/site.webmanifest
+++ b/app/static/manifest.json
@@ -15,7 +15,7 @@
],
"theme_color": "#007BBF",
"background_color": "#ffffff",
- "display": "browser",
+ "display": "minimal-ui",
"start_url": "/",
"scope": "/"
}
diff --git a/app/static/service_worker.js b/app/static/service_worker.js
new file mode 100644
index 00000000..625d42fc
--- /dev/null
+++ b/app/static/service_worker.js
@@ -0,0 +1,31 @@
+//https://developers.google.com/web/fundamentals/primers/service-workers
+
+// Font files
+var fontFiles = [
+ '/static/fonts/feather/feather.ttf',
+];
+
+//this is just an empty service worker so that the 'Install CB as an app' prompt appears in web browsers
+self.addEventListener('install', function(event) {
+ event.waitUntil(caches.open('core').then(function (cache) {
+ fontFiles.forEach(function (file) {
+ cache.add(new Request(file));
+ });
+ return;
+ }));
+});
+
+self.addEventListener('fetch', function(event) {
+ // Fonts
+ // Offline-first
+ if (request.url.includes('feather.ttf')) {
+ event.respondWith(
+ caches.match(request).then(function (response) {
+ return response || fetch(request).then(function (response) {
+ // Return the requested file
+ return response;
+ });
+ })
+ );
+ }
+});
diff --git a/app/templates/base.html b/app/templates/base.html
index 1c96c0f7..f1c3b285 100644
--- a/app/templates/base.html
+++ b/app/templates/base.html
@@ -52,7 +52,7 @@
-
+