From 9c251e1a582ca6f0f138e312ff02e3de35e1a8c5 Mon Sep 17 00:00:00 2001
From: Hangzhi Yu <hangzhi@protonmail.com>
Date: Sun, 6 Dec 2020 21:13:21 +0100
Subject: [PATCH] Add serviceworker template, settings and file replacement
 functionality

---
 aleksis/core/static/js/serviceworker.js | 26 +++++++------------------
 aleksis/core/urls.py                    |  1 +
 2 files changed, 8 insertions(+), 19 deletions(-)

diff --git a/aleksis/core/static/js/serviceworker.js b/aleksis/core/static/js/serviceworker.js
index 93d9797c7..c260c9ef2 100644
--- a/aleksis/core/static/js/serviceworker.js
+++ b/aleksis/core/static/js/serviceworker.js
@@ -1,18 +1,9 @@
-// This is the AlekSIS service worker
-
-const CACHE = "aleksis-cache";
 
-const precacheFiles = [
-    '',
-];
+// This is the AlekSIS service worker
 
-const offlineFallbackPage = '/offline';
+const CACHE = 'aleksis-cache';
 
-const avoidCachingPaths = [
-    '/admin',
-    '/settings',
-    '/accounts/login'
-]; // TODO: More paths are needed
+const offlineFallbackPage = 'offline/';
 
 function pathComparer(requestUrl, pathRegEx) {
     return requestUrl.match(new RegExp(pathRegEx));
@@ -40,10 +31,7 @@ self.addEventListener("install", function (event) {
     event.waitUntil(
         caches.open(CACHE).then(function (cache) {
             console.log("[AlekSIS PWA] Caching pages during install.");
-
-            return cache.addAll(precacheFiles).then(function () {
-                return cache.add(offlineFallbackPage);
-            });
+            return cache.addAll(precachePaths);
         })
     );
 });
@@ -95,11 +83,11 @@ function fromCache(event) {
 }
 
 function updateCache(request, response) {
-    if (!comparePaths(request.url, avoidCachingPaths)) {
+    if (response.headers.get('cache-control') && response.headers.get('cache-control').includes('no-cache')) {
+        return Promise.resolve();
+    } else {
         return caches.open(CACHE).then(function (cache) {
             return cache.put(request, response);
         });
     }
-
-    return Promise.resolve();
 }
diff --git a/aleksis/core/urls.py b/aleksis/core/urls.py
index 7b5b06632..6c989ddf7 100644
--- a/aleksis/core/urls.py
+++ b/aleksis/core/urls.py
@@ -18,6 +18,7 @@ from .util.core_helpers import is_celery_enabled
 urlpatterns = [
     path("", include("django_prometheus.urls")),
     path("", include("pwa.urls"), name="pwa"),
+    path("offline/", views.offline, name="offline"),
     path("about/", views.about, name="about_aleksis"),
     path("admin/", admin.site.urls),
     path("data_management/", views.data_management, name="data_management"),
-- 
GitLab