vomebook commited on
Commit
8e991c3
·
verified ·
1 Parent(s): 712b637

Prefer fresh Reader runtime assets

Browse files
Files changed (1) hide show
  1. static/sw.js +14 -8
static/sw.js CHANGED
@@ -18,6 +18,12 @@ const PRECACHE_URLS = [
18
  "/icons/icon-192.png",
19
  "/icons/icon-512.png"
20
  ];
 
 
 
 
 
 
21
  self.addEventListener("install", (event) => {
22
  event.waitUntil(
23
  caches.open(CACHE_NAME).then((cache) => {
@@ -60,15 +66,15 @@ self.addEventListener("fetch", (event) => {
60
  if (url.pathname.startsWith("/txt/")) {
61
  return;
62
  }
63
- if (event.request.mode === "navigate" && url.pathname === "/static/reader.html") {
 
64
  event.respondWith(
65
- caches.open(CACHE_NAME).then((cache) => cache.match("/static/reader.html").then((cached) => {
66
- const fetchPromise = fetch(event.request).then((response) => {
67
- if (response.ok) cache.put("/static/reader.html", response.clone());
68
- return response;
69
- }).catch(() => cached);
70
- return cached || fetchPromise;
71
- }))
72
  );
73
  return;
74
  }
 
18
  "/icons/icon-192.png",
19
  "/icons/icon-512.png"
20
  ];
21
+ const READER_RUNTIME_PATHS = new Set([
22
+ "/static/reader-contract.js",
23
+ "/static/reader-store.js",
24
+ "/static/reader.css",
25
+ "/static/reader.js"
26
+ ]);
27
  self.addEventListener("install", (event) => {
28
  event.waitUntil(
29
  caches.open(CACHE_NAME).then((cache) => {
 
66
  if (url.pathname.startsWith("/txt/")) {
67
  return;
68
  }
69
+ if ((event.request.mode === "navigate" && url.pathname === "/static/reader.html") || READER_RUNTIME_PATHS.has(url.pathname)) {
70
+ const cacheKey = event.request.mode === "navigate" ? "/static/reader.html" : event.request;
71
  event.respondWith(
72
+ caches.open(CACHE_NAME).then((cache) => cache.match(cacheKey).then((cached) =>
73
+ fetch(event.request).then((response) => {
74
+ if (response.ok) cache.put(cacheKey, response.clone());
75
+ return response.ok || !cached ? response : cached;
76
+ }).catch(() => cached)
77
+ ))
 
78
  );
79
  return;
80
  }