fix(mirror): Service Worker 默认关闭(URL 加 #sw 才启用),排查 SW 是否导致断联

- main 加载器与 split.js 模板同步:默认注销已注册的 SW(单包缓存接管),#sw 显式启用,#nosw 仍可强制关闭

- 注意:注销在下次导航生效,需刷新两次
This commit is contained in:
TermiNexus committed 2026-10-11 20:11:02 +08:00
1 parent 418298e2ec
commit 562835c284
2 files changed
+16 -14

No files matched your search

+8 -7
View File
@@ -131,13 +131,7 @@ var __webpack_modules__ = window.__modules__;
// 作用域必须是 /:首页 / 发起的 /play/... 请求才会被接管(serve.js 已放行 Service-Worker-Allowed)
// 用途:把 unpacked 下的散装小文件用单包就地命中(hornet_sw.js 内置单包缓存)
if ('serviceWorker' in navigator && /^(localhost|127\.0\.0\.1|\[::1\])$/.test(location.hostname)) {
if (/#nosw/.test(location.hash)) {
// #nosw:注销已注册的 Service Worker(清除单包缓存接管),用于排查镜像页问题;生效需再刷新一次
navigator.serviceWorker.getRegistrations().then(function (Regs) {
Regs.forEach(function (Reg) { Reg.unregister(); });
console.warn('[Hornet] #nosw:已注销 Service Worker(再刷新一次生效)');
});
} else {
if (/#sw\b/.test(location.hash) && !/#nosw/.test(location.hash)) {
navigator.serviceWorker
.register('/hornet_sw.js', { scope: '/', updateViaCache: 'none' })
.then(function (Reg) {
@@ -147,6 +141,13 @@ var __webpack_modules__ = window.__modules__;
.catch(function (Err) {
console.warn('[Hornet] Service Worker 注册失败: ' + (Err && Err.message));
});
} else {
// 默认**不启用** Service Worker:注销已注册的实例(清除单包缓存接管),排查镜像页问题时用;生效需再刷新一次
navigator.serviceWorker.getRegistrations().then(function (Regs) {
if (!Regs.length) return;
Regs.forEach(function (Reg) { Reg.unregister(); });
console.warn('[Hornet] Service Worker 已注销(默认关闭;要启用请在 URL 加 #sw,需再刷新一次)');
});
}
}
+8 -7
View File
@@ -365,13 +365,7 @@ var __webpack_modules__ = window.__modules__;
// 作用域必须是 /:首页 / 发起的 /play/... 请求才会被接管(serve.js 放行 Service-Worker-Allowed)
// 用途:把 unpacked 下的散装小文件用单包就地命中(hornet_sw.js 内置单包缓存)
if ('serviceWorker' in navigator && /^(localhost|127\\.0\\.0\\.1|\\[::1\\])$/.test(location.hostname)) {
if (/#nosw/.test(location.hash)) {
// #nosw:注销已注册的 Service Worker(清除单包缓存接管),用于排查镜像页问题;生效需再刷新一次
navigator.serviceWorker.getRegistrations().then(function (Regs) {
Regs.forEach(function (Reg) { Reg.unregister(); });
console.warn('[Hornet] #nosw:已注销 Service Worker(再刷新一次生效)');
});
} else {
if (/#sw\\b/.test(location.hash) && !/#nosw/.test(location.hash)) {
navigator.serviceWorker
.register('/hornet_sw.js', { scope: '/', updateViaCache: 'none' })
.then(function (Reg) {
@@ -381,6 +375,13 @@ var __webpack_modules__ = window.__modules__;
.catch(function (Err) {
console.warn('[Hornet] Service Worker 注册失败: ' + (Err && Err.message));
});
} else {
// 默认**不启用** Service Worker:注销已注册的实例(清除单包缓存接管),排查镜像页问题时用;生效需再刷新一次
navigator.serviceWorker.getRegistrations().then(function (Regs) {
if (!Regs.length) return;
Regs.forEach(function (Reg) { Reg.unregister(); });
console.warn('[Hornet] Service Worker 已注销(默认关闭;要启用请在 URL 加 #sw,需再刷新一次)');
});
}
}