diff options
author | Alex Shpak <alex-shpak@users.noreply.github.com> | 2020-04-21 22:35:30 +0200 |
---|---|---|
committer | Alex Shpak <alex-shpak@users.noreply.github.com> | 2020-05-01 13:19:58 +0200 |
commit | c672d64f126178f0facb91204761b1b913743d71 (patch) | |
tree | d2de47eeb45c0bb4d76cf7de2c1635f2a7cb07df | |
parent | 9c5fc12dd8fba31d6b5ced553d9748ec87dd9474 (diff) |
Generate pages list in sw.js
-rw-r--r-- | assets/sw-register.js | 3 | ||||
-rw-r--r-- | assets/sw.js (renamed from static/sw.js) | 14 |
2 files changed, 14 insertions, 3 deletions
diff --git a/assets/sw-register.js b/assets/sw-register.js index 583532c..0b71f03 100644 --- a/assets/sw-register.js +++ b/assets/sw-register.js @@ -1,6 +1,7 @@ +{{ $swJS := resources.Get "sw.js" | resources.ExecuteAsTemplate "sw.js" . }} if (navigator.serviceWorker) { navigator.serviceWorker.register( - "{{ "/sw.js" | relURL }}", + "{{ $swJS.RelPermalink }}", { scope: "{{ "/" | relURL }}" } ); } diff --git a/static/sw.js b/assets/sw.js index d42b04c..d7d517a 100644 --- a/static/sw.js +++ b/assets/sw.js @@ -1,11 +1,21 @@ +const cacheName = self.location.pathname +const pages = [ + {{ range .Site.AllPages -}} + "{{ .RelPermalink }}", + {{ end }} +]; + self.addEventListener("install", function (event) { self.skipWaiting(); + + const precache = caches.open(cacheName).then((cache) => { + return cache.addAll(pages); + }); + event.waitUntil(precache); }); self.addEventListener("fetch", (event) => { - const cacheName = self.location.pathname const request = event.request; - if (request.method !== "GET") { return; } |