From a10ae84d4474cbc58670d5eb82845134d3e996da Mon Sep 17 00:00:00 2001
From: Hangzhi Yu <hangzhi@protonmail.com>
Date: Thu, 31 Dec 2020 01:24:41 +0100
Subject: [PATCH] Add simple message (Materialize toast) that pops up whenever
 a page is served from the PWA cache

---
 aleksis/core/static/js/serviceworker.js |  2 ++
 aleksis/core/templates/core/base.html   | 10 ++++++++++
 2 files changed, 12 insertions(+)

diff --git a/aleksis/core/static/js/serviceworker.js b/aleksis/core/static/js/serviceworker.js
index 818e27cb7..8d6d37152 100644
--- a/aleksis/core/static/js/serviceworker.js
+++ b/aleksis/core/static/js/serviceworker.js
@@ -52,6 +52,8 @@ function fromCache(event) {
     // Return response
     // If not in the cache, then return offline fallback page
     return caches.open(CACHE).then(function (cache) {
+        const channel = new BroadcastChannel('cache-or-not');
+        channel.postMessage(true);
         return cache.match(event.request)
             .then(function (matching) {
                 if (!matching || matching.status === 404) {
diff --git a/aleksis/core/templates/core/base.html b/aleksis/core/templates/core/base.html
index 2087829da..802f27b81 100644
--- a/aleksis/core/templates/core/base.html
+++ b/aleksis/core/templates/core/base.html
@@ -174,5 +174,15 @@
 {% include_js "jquery-sortablejs" %}
 <script type="text/javascript" src="{% static 'js/search.js' %}"></script>
 <script type="text/javascript" src="{% static 'js/main.js' %}"></script>
+
+<script>
+  const channel = new BroadcastChannel('cache-or-not');
+  channel.addEventListener('message', event => {
+    if (event.data) {
+      M.toast({html: "{% trans "This page may contain outdated information since there is no internet connetion." %}"}, 3600)
+    }
+  });
+</script>
+
 </body>
 </html>
-- 
GitLab