jekyll-theme-conference 5.4.0 → 5.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/_layouts/sw.js CHANGED
@@ -14,9 +14,10 @@ const SITE_UPDATED = "site-updated";
14
14
  // main.css?t=1793258390): ignored, so a rebuild alone is no update
15
15
  const BUILD_TIME = /\?t=\d+/g;
16
16
 
17
- // Whether this build changed the site: set on install, told the pages on
18
- // activation
19
- let siteUpdated = false;
17
+ // Whether this build changed the site: noted in its cache on install (the
18
+ // browser may stop the worker before activation, losing variables), told the
19
+ // pages on activation
20
+ const SITE_UPDATED_KEY = `${BASE_URL}/__site-updated__`;
20
21
 
21
22
  // Whether the latest version differs from the stored one, ignoring the build time
22
23
  const isChanged = async (stored, latest) => {
@@ -49,16 +50,25 @@ const precache = async (cache, previous, url) => {
49
50
  headers["If-Modified-Since"] = cached.headers.get("Last-Modified");
50
51
  }
51
52
 
52
- // Low priority: the visitor's own requests go first
53
- const response = await fetch(url, { cache: "no-store", headers, priority: "low" });
54
- if (response.status === HTTP_NOT_MODIFIED && cached) {
55
- await cache.put(url, cached);
56
- return false;
57
- }
58
- if (!response.ok) {
59
- throw new Error(`${response.status} ${url}`);
53
+ // Low priority: the visitor's own requests go first. On failure (e.g. a
54
+ // flaky network) the previous copy stays, as the old cache is deleted.
55
+ let response;
56
+ try {
57
+ response = await fetch(url, { cache: "no-store", headers, priority: "low" });
58
+ if (response.status === HTTP_NOT_MODIFIED && cached) {
59
+ await cache.put(url, cached);
60
+ return false;
61
+ }
62
+ if (!response.ok) {
63
+ throw new Error(`${response.status} ${url}`);
64
+ }
65
+ await cache.put(url, response.clone());
66
+ } catch (error) {
67
+ if (cached) {
68
+ await cache.put(url, cached);
69
+ }
70
+ throw error;
60
71
  }
61
- await cache.put(url, response.clone());
62
72
 
63
73
  // Example: main.css?t=1793258390 of the previous build. New resources
64
74
  // change the pages linking them.
@@ -70,25 +80,25 @@ const precache = async (cache, previous, url) => {
70
80
  self.addEventListener("install", (event) => {
71
81
  event.waitUntil(
72
82
  (async () => {
73
- try {
74
- const response = await fetch(`${BASE_URL}/assets/js/site.json`, { cache: "no-store" });
75
- if (!response.ok) {
76
- throw new Error(`Failed to load site.json: ${response.status}`);
77
- }
78
- const { pages = [], assets = [] } = await response.json();
79
-
80
- // Cache each resource on its own, so one missing file does not
81
- // leave the whole cache empty
82
- const cache = await caches.open(CACHE_NAME);
83
- const previous = await previousCache();
84
- const results = await Promise.allSettled([...pages, ...assets].map((url) => precache(cache, previous, url)));
85
- const failed = results.filter((result) => result.status === "rejected");
86
- if (failed.length) {
87
- console.warn(`Service worker could not cache ${failed.length} resource(s)`);
88
- }
89
- siteUpdated = results.some((result) => result.value === true);
90
- } catch (error) {
91
- console.error("Service worker install failed:", error);
83
+ // Without the list (e.g. offline during the update) the install fails:
84
+ // the previous worker and its cache stay, the browser retries later
85
+ const response = await fetch(`${BASE_URL}/assets/js/site.json`, { cache: "no-store" });
86
+ if (!response.ok) {
87
+ throw new Error(`Failed to load site.json: ${response.status}`);
88
+ }
89
+ const { pages = [], assets = [] } = await response.json();
90
+
91
+ // Cache each resource on its own, so one missing file does not
92
+ // leave the whole cache empty
93
+ const cache = await caches.open(CACHE_NAME);
94
+ const previous = await previousCache();
95
+ const results = await Promise.allSettled([...pages, ...assets].map((url) => precache(cache, previous, url)));
96
+ const failed = results.filter((result) => result.status === "rejected");
97
+ if (failed.length) {
98
+ console.warn(`Service worker could not cache ${failed.length} resource(s)`);
99
+ }
100
+ if (results.some((result) => result.value === true)) {
101
+ await cache.put(SITE_UPDATED_KEY, new Response(""));
92
102
  }
93
103
 
94
104
  // Activate new versions right away (schedule updates)
@@ -113,7 +123,8 @@ self.addEventListener("activate", (event) => {
113
123
  await self.clients.claim();
114
124
 
115
125
  // Once for all pages of the build, e.g. not again on the next page
116
- if (siteUpdated) {
126
+ const cache = await caches.open(CACHE_NAME);
127
+ if (await cache.delete(SITE_UPDATED_KEY)) {
117
128
  const clients = await self.clients.matchAll({ type: "window" });
118
129
  clients.forEach((client) => client.postMessage({ type: SITE_UPDATED }));
119
130
  }
@@ -125,19 +136,34 @@ self.addEventListener("activate", (event) => {
125
136
  const fetchLatest = (event) =>
126
137
  (event.preloadResponse ?? Promise.resolve()).then((preloaded) => preloaded || fetch(event.request));
127
138
 
128
- // Store a response for later
129
- const store = async (request, response) => {
130
- if (response.ok) {
131
- const cache = await caches.open(CACHE_NAME);
132
- await cache.put(request, response.clone());
139
+ // Pages and data are looked up ignoring the query (staleWhileRevalidate):
140
+ // stored without it, so variants (e.g. /live/?room=A) do not pile up
141
+ const storeKey = (request) => {
142
+ const url = new URL(request.url);
143
+ if (request.mode !== "navigate" && !url.pathname.endsWith(".json")) {
144
+ return request;
145
+ }
146
+ url.search = "";
147
+ return url.href;
148
+ };
149
+
150
+ // Store a complete response for later (not e.g. partial content, 206, which
151
+ // the cache refuses), copied before the page reads it; resolves once stored
152
+ const store = (request, response) => {
153
+ if (response.status !== 200 || response.headers.get("Vary") === "*") {
154
+ return Promise.resolve();
133
155
  }
134
- return response;
156
+ const copy = response.clone();
157
+ return caches
158
+ .open(CACHE_NAME)
159
+ .then((cache) => cache.put(storeKey(request), copy))
160
+ .catch(() => {});
135
161
  };
136
162
 
137
- // Latest version from the network, stored for later
163
+ // Latest version from the network, stored for later without delaying it
138
164
  const fromNetwork = (event) => {
139
- const network = fetchLatest(event).then((response) => store(event.request, response));
140
- event.waitUntil(network.catch(() => {}));
165
+ const network = fetchLatest(event);
166
+ event.waitUntil(network.then((response) => store(event.request, response)).catch(() => {}));
141
167
  return network;
142
168
  };
143
169
 
@@ -166,13 +192,18 @@ const revalidatePage = async (event, cached) => {
166
192
  const NOT_FOUND_URL = {{ not_found_url | jsonify }};
167
193
 
168
194
  // The site's page not found in place of the server's (e.g. plain text), and
169
- // offline for pages not stored
195
+ // offline for pages not stored: then marked (data-offline), so it says the
196
+ // page is not available offline instead of missing (_layouts/not-found.html)
170
197
  const notFound = async (response) => {
171
198
  const page = NOT_FOUND_URL && (await caches.match(NOT_FOUND_URL));
172
199
  if (!page) {
173
200
  return response ?? Response.error();
174
201
  }
175
- return new Response(page.body, { status: HTTP_NOT_FOUND, headers: page.headers });
202
+ if (response) {
203
+ return new Response(page.body, { status: HTTP_NOT_FOUND, headers: page.headers });
204
+ }
205
+ const html = (await page.text()).replace("<html", "<html data-offline");
206
+ return new Response(html, { status: HTTP_NOT_FOUND, headers: page.headers });
176
207
  };
177
208
 
178
209
  // Page not stored (e.g. a missing one): the network, else the page not found
@@ -243,6 +274,11 @@ self.addEventListener("fetch", (event) => {
243
274
  if (request.method !== "GET" || url.origin !== self.location.origin || !url.pathname.startsWith(`${BASE_URL}/`)) {
244
275
  return;
245
276
  }
277
+ // Parts of a file (e.g. seeking in audio or video) stay the browser's: the
278
+ // cache holds whole files only
279
+ if (request.headers.has("Range")) {
280
+ return;
281
+ }
246
282
  if (url.pathname === `${BASE_URL}/sw.js` || DOCUMENT_PATHS.some((path) => url.pathname.startsWith(`${BASE_URL}${path}`))) {
247
283
  return;
248
284
  }