fix(pwa): 保护离线外壳并限制缓存清理范围
This commit is contained in:
1 parent
ff55c95ea0
commit
3d7e551838
2 files changed
+138
-5
No files matched your search
@@ -6,7 +6,8 @@
|
||||
* - 构建产物 /assets/*(带内容哈希):cache-first(文件名变化即自动失效)
|
||||
* - /api 与 /socket.io:完全透传,绝不缓存(避免脏数据与鉴权问题)
|
||||
*/
|
||||
const CACHE_NAME = 'classhelper-admin-v1';
|
||||
const CACHE_PREFIX = 'classhelper-admin-';
|
||||
const CACHE_NAME = `${CACHE_PREFIX}v2`;
|
||||
const APP_SHELL = ['/', '/index.html', '/manifest.webmanifest', '/icon-192.png', '/icon-512.png'];
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
@@ -23,7 +24,13 @@ self.addEventListener('activate', (event) => {
|
||||
event.waitUntil(
|
||||
caches
|
||||
.keys()
|
||||
.then((keys) => Promise.all(keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key))))
|
||||
.then((keys) =>
|
||||
Promise.all(
|
||||
keys
|
||||
.filter((key) => key.startsWith(CACHE_PREFIX) && key !== CACHE_NAME)
|
||||
.map((key) => caches.delete(key)),
|
||||
),
|
||||
)
|
||||
.then(() => self.clients.claim()),
|
||||
);
|
||||
});
|
||||
@@ -36,15 +43,25 @@ self.addEventListener('fetch', (event) => {
|
||||
// 只接管同源请求
|
||||
if (url.origin !== self.location.origin) return;
|
||||
// 接口与实时通道透传
|
||||
if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/socket.io/')) return;
|
||||
if (
|
||||
['/api', '/socket.io'].some((prefix) => url.pathname === prefix || url.pathname.startsWith(`${prefix}/`))
|
||||
)
|
||||
return;
|
||||
|
||||
// 页面导航:网络优先,离线时用缓存外壳
|
||||
if (request.mode === 'navigate') {
|
||||
event.respondWith(
|
||||
fetch(request)
|
||||
.then((response) => {
|
||||
if (response.ok && response.headers.get('content-type')?.includes('text/html')) {
|
||||
const clone = response.clone();
|
||||
caches.open(CACHE_NAME).then((cache) => cache.put('/index.html', clone));
|
||||
event.waitUntil(
|
||||
caches
|
||||
.open(CACHE_NAME)
|
||||
.then((cache) => cache.put('/index.html', clone))
|
||||
.catch(() => undefined),
|
||||
);
|
||||
}
|
||||
return response;
|
||||
})
|
||||
.catch(() => caches.match('/index.html').then((cached) => cached ?? Response.error())),
|
||||
@@ -60,7 +77,12 @@ self.addEventListener('fetch', (event) => {
|
||||
.then((response) => {
|
||||
if (response.ok) {
|
||||
const clone = response.clone();
|
||||
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone));
|
||||
event.waitUntil(
|
||||
caches
|
||||
.open(CACHE_NAME)
|
||||
.then((cache) => cache.put(request, clone))
|
||||
.catch(() => undefined),
|
||||
);
|
||||
}
|
||||
return response;
|
||||
})
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import vm from 'node:vm';
|
||||
|
||||
const source = fs.readFileSync(new URL('../packages/web-admin/public/sw.js', import.meta.url), 'utf8');
|
||||
const registry = fs.readFileSync(
|
||||
new URL('../packages/web-admin/src/element-plus.ts', import.meta.url),
|
||||
'utf8',
|
||||
);
|
||||
function verifyComponentRegistry(dir) {
|
||||
for (const item of fs.readdirSync(dir, { withFileTypes: true })) {
|
||||
const file = new URL(item.name + (item.isDirectory() ? '/' : ''), dir);
|
||||
if (item.isDirectory()) verifyComponentRegistry(file);
|
||||
else if (item.name.endsWith('.vue')) {
|
||||
for (const [, tag] of fs.readFileSync(file, 'utf8').matchAll(/<(el-[a-z0-9-]+)\b/g)) {
|
||||
const name = tag
|
||||
.split('-')
|
||||
.map((word) => word[0].toUpperCase() + word.slice(1))
|
||||
.join('');
|
||||
assert(registry.includes(name), `${file.pathname} 的 ${tag} 未注册`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
verifyComponentRegistry(new URL('../packages/web-admin/src/', import.meta.url));
|
||||
console.log('组件注册表覆盖全部模板标签');
|
||||
let passed = 0;
|
||||
let failed = 0;
|
||||
async function check(name, test) {
|
||||
const handlers = new Map();
|
||||
const writes = [];
|
||||
const deleted = [];
|
||||
const context = {
|
||||
URL,
|
||||
Response,
|
||||
self: {
|
||||
location: { origin: 'https://audit.invalid' },
|
||||
addEventListener: (name, fn) => handlers.set(name, fn),
|
||||
clients: { claim: async () => {} },
|
||||
skipWaiting: async () => {},
|
||||
},
|
||||
caches: {
|
||||
open: async () => ({
|
||||
addAll: async () => {},
|
||||
put: async (key) => {
|
||||
writes.push(key);
|
||||
},
|
||||
}),
|
||||
keys: async () => ['classhelper-admin-v1', 'unrelated-app-cache'],
|
||||
delete: async (key) => {
|
||||
deleted.push(key);
|
||||
},
|
||||
match: async () => undefined,
|
||||
},
|
||||
fetch: async () => new Response('应用外壳', { headers: { 'content-type': 'text/html' } }),
|
||||
};
|
||||
vm.runInNewContext(source, context);
|
||||
async function fire(name, request) {
|
||||
const tasks = [];
|
||||
let response;
|
||||
handlers.get(name)({
|
||||
request,
|
||||
waitUntil: (task) => tasks.push(task),
|
||||
respondWith: (task) => {
|
||||
response = task;
|
||||
},
|
||||
});
|
||||
const result = response ? await response : undefined;
|
||||
await Promise.all(tasks);
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
return result;
|
||||
}
|
||||
try {
|
||||
await test({ context, writes, deleted, fire });
|
||||
console.log(`通过 ${++passed}:${name}`);
|
||||
} catch (error) {
|
||||
failed++;
|
||||
console.error(`失败:${name}:${error.message}`);
|
||||
}
|
||||
}
|
||||
const navigate = (pathname) => ({ method: 'GET', mode: 'navigate', url: 'https://audit.invalid' + pathname });
|
||||
await check('错误导航响应不覆盖离线外壳', async ({ context, writes, fire }) => {
|
||||
context.fetch = async () =>
|
||||
new Response('失败页面', { status: 500, headers: { 'content-type': 'text/html' } });
|
||||
assert.equal((await fire('fetch', navigate('/dashboard'))).status, 500);
|
||||
assert.deepEqual(writes, []);
|
||||
});
|
||||
await check('非HTML导航不覆盖离线外壳', async ({ context, writes, fire }) => {
|
||||
context.fetch = async () => new Response('{}', { headers: { 'content-type': 'application/json' } });
|
||||
await fire('fetch', navigate('/download'));
|
||||
assert.deepEqual(writes, []);
|
||||
});
|
||||
await check('API与实时根路径及子路径全部透传', async ({ fire }) => {
|
||||
for (const pathname of ['/api', '/api/health', '/socket.io', '/socket.io/'])
|
||||
assert.equal(await fire('fetch', navigate(pathname)), undefined);
|
||||
});
|
||||
await check('只清理本应用的旧缓存', async ({ deleted, fire }) => {
|
||||
await fire('activate');
|
||||
assert.deepEqual(deleted, ['classhelper-admin-v1']);
|
||||
});
|
||||
await check('有效HTML仍更新外壳并支持离线回退', async ({ context, writes, fire }) => {
|
||||
assert.equal((await fire('fetch', navigate('/dashboard'))).status, 200);
|
||||
assert.deepEqual(writes, ['/index.html']);
|
||||
context.fetch = async () => {
|
||||
throw new Error('离线夹具');
|
||||
};
|
||||
context.caches.match = async () => new Response('已缓存外壳');
|
||||
assert.equal(await (await fire('fetch', navigate('/dashboard'))).text(), '已缓存外壳');
|
||||
});
|
||||
console.log(`Web Worker 专项:${passed} 通过,${failed} 失败`);
|
||||
if (failed) process.exitCode = 1;
|
||||
Reference in new issue
Block a user