@rmdes/indiekit-frontend 1.0.0-beta.39 → 1.0.0-beta.40
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.
- package/lib/serviceworker.js +37 -1
- package/package.json +1 -1
package/lib/serviceworker.js
CHANGED
|
@@ -87,6 +87,31 @@ async function notifyClients() {
|
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Remove any cached auth/session pages from the pages cache.
|
|
92
|
+
* Called on activate to purge stale entries from before the fetch bypass was added.
|
|
93
|
+
*/
|
|
94
|
+
async function clearAuthSessionEntries() {
|
|
95
|
+
try {
|
|
96
|
+
const pagesCache = await caches.open(pagesCacheName);
|
|
97
|
+
const keys = await pagesCache.keys();
|
|
98
|
+
|
|
99
|
+
await Promise.all(
|
|
100
|
+
keys
|
|
101
|
+
.filter((request) => {
|
|
102
|
+
const url = new URL(request.url);
|
|
103
|
+
return (
|
|
104
|
+
url.origin === self.location.origin &&
|
|
105
|
+
/^\/(auth|session)(?:\/|$)/.test(url.pathname)
|
|
106
|
+
);
|
|
107
|
+
})
|
|
108
|
+
.map((request) => pagesCache.delete(request)),
|
|
109
|
+
);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
console.error("Error clearing auth/session cache entries", error);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
90
115
|
/**
|
|
91
116
|
* Trim cache
|
|
92
117
|
* @param {string} cacheName - Name of cache
|
|
@@ -120,6 +145,7 @@ self.addEventListener("activate", async (event) => {
|
|
|
120
145
|
event.waitUntil(
|
|
121
146
|
(async () => {
|
|
122
147
|
await clearOldCaches();
|
|
148
|
+
await clearAuthSessionEntries();
|
|
123
149
|
// Don't clear pages cache on activate — stale cached pages provide a
|
|
124
150
|
// valuable fallback when the network is slow (e.g. right after a deploy).
|
|
125
151
|
// The network-first fetch strategy naturally updates cached pages on
|
|
@@ -150,13 +176,23 @@ self.addEventListener("fetch", (event) => {
|
|
|
150
176
|
// Cross-origin images (avatars, album covers, etc.) must be handled
|
|
151
177
|
// by the browser natively — opaque responses from SW fetch are unreliable
|
|
152
178
|
// and caching them wastes ~7MB each against storage quota.
|
|
179
|
+
const requestUrl = new URL(request.url);
|
|
153
180
|
if (
|
|
154
|
-
|
|
181
|
+
requestUrl.origin !== self.location.origin ||
|
|
155
182
|
request.method !== "GET"
|
|
156
183
|
) {
|
|
157
184
|
return;
|
|
158
185
|
}
|
|
159
186
|
|
|
187
|
+
// Never cache auth/session pages — always go to network.
|
|
188
|
+
// Stale cached auth responses break login detection (admin.js probes
|
|
189
|
+
// /session/login to check if owner is logged in) and can serve
|
|
190
|
+
// authenticated pages after session expiry.
|
|
191
|
+
if (/^\/(auth|session)(?:\/|$)/.test(requestUrl.pathname)) {
|
|
192
|
+
event.respondWith(fetch(request));
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
160
196
|
// For HTML requests: network-first with conditional timeout
|
|
161
197
|
// - If a cached version exists: race network against timeout, serve cache on timeout
|
|
162
198
|
// - If no cached version: wait for network without timeout (avoid premature "Offline")
|