Show the update banner only when the running build differs from the server and make Refresh UI land the new shell on flaky links
This commit is contained in:
@@ -27,8 +27,9 @@ const SW_SOURCE = fs.readFileSync(path.join(__dirname, 'sw.js'), 'utf8');
|
||||
|
||||
// Builds a fresh sandboxed SW environment with the given starting cache keys
|
||||
// and returns handles to drive/inspect it.
|
||||
function loadSw(initialCacheKeys) {
|
||||
function loadSw(initialCacheKeys, { fetchImpl } = {}) {
|
||||
const cacheStore = new Set(initialCacheKeys);
|
||||
const puts = [];
|
||||
const deleted = [];
|
||||
const clientMessages = [];
|
||||
|
||||
@@ -50,14 +51,15 @@ function loadSw(initialCacheKeys) {
|
||||
async delete(key) { deleted.push(key); return cacheStore.delete(key); },
|
||||
async open(key) {
|
||||
cacheStore.add(key);
|
||||
return { addAll: async () => {}, match: async () => undefined, put: async () => {} };
|
||||
return { addAll: async () => {}, match: async () => undefined, put: async (url) => { puts.push(url); } };
|
||||
},
|
||||
};
|
||||
|
||||
const sandbox = {
|
||||
self: self_,
|
||||
caches: caches_,
|
||||
fetch: async () => { throw new Error('fetch not mocked'); },
|
||||
fetch: fetchImpl || (async () => { throw new Error('fetch not mocked'); }),
|
||||
Request: class { constructor(url, init) { this.url = url; Object.assign(this, init); } },
|
||||
Response: class { constructor(body, init) { this.body = body; Object.assign(this, init); } },
|
||||
URL,
|
||||
console,
|
||||
@@ -72,7 +74,14 @@ function loadSw(initialCacheKeys) {
|
||||
await waitPromise;
|
||||
}
|
||||
|
||||
return { triggerActivate, deleted, clientMessages, cacheStoreRemaining: () => Array.from(cacheStore) };
|
||||
async function triggerInstall() {
|
||||
let waitPromise = Promise.resolve();
|
||||
const event = { waitUntil: (p) => { waitPromise = p; } };
|
||||
for (const fn of listeners.install || []) fn(event);
|
||||
await waitPromise;
|
||||
}
|
||||
|
||||
return { triggerActivate, triggerInstall, deleted, puts, clientMessages, cacheStoreRemaining: () => Array.from(cacheStore) };
|
||||
}
|
||||
|
||||
test('activate does NOT broadcast on a fresh install (only the current-version cache exists)', async () => {
|
||||
@@ -115,3 +124,29 @@ test('activate reports an update and still preserves utility caches together', a
|
||||
['ytplayer-fonts', 'ytplayer-thumbs', 'ytplayer-v1.0.4'].sort()
|
||||
);
|
||||
});
|
||||
|
||||
test('install fetches every shell file past the HTTP cache and retries dropped requests', async () => {
|
||||
const seen = {};
|
||||
const sw = loadSw([], {
|
||||
fetchImpl: async (req) => {
|
||||
seen[req.url] = (seen[req.url] || 0) + 1;
|
||||
assert.strictEqual(req.cache, 'reload', 'must bypass the browser HTTP cache');
|
||||
// The homelab link drops the first request for app.js.
|
||||
if (req.url === '/app.js' && seen[req.url] === 1) throw new TypeError('network error');
|
||||
return { ok: true, status: 200 };
|
||||
},
|
||||
});
|
||||
await sw.triggerInstall();
|
||||
assert.strictEqual(seen['/app.js'], 2, 'a dropped request is retried');
|
||||
assert.ok(sw.puts.includes('/app.js') && sw.puts.includes('/'), 'shell files cached');
|
||||
assert.deepStrictEqual(sw.deleted, []);
|
||||
});
|
||||
|
||||
test('a failed install deletes its partial cache so activate never mistakes it for an old deploy', async () => {
|
||||
const sw = loadSw([], {
|
||||
fetchImpl: async (req) => (req.url === '/styles.css' ? { ok: false, status: 504 } : { ok: true, status: 200 }),
|
||||
});
|
||||
await assert.rejects(sw.triggerInstall(), /styles\.css/);
|
||||
assert.deepStrictEqual(sw.deleted, ['ytplayer-v1.0.4'], 'partial cache removed');
|
||||
assert.deepStrictEqual(sw.cacheStoreRemaining(), []);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user