fix(page): 修复 index.html 主文件引用失配(新主文件没被加载,自动刷新拦截等全部失效)
- 根因:index.html 写死 main.9f4e7a89.js,重拆后主文件改名 main.7cfa4058.js → 请求旧名 404,新主文件里的 HORNET 注入 / 自动刷新拦截 / F5 兜底压根没加载 - 3DTank/web/index.html:引用改为 main.7cfa4058.js - tools/serve.js:Resolve 增加兜底 —— 请求 main.<hash>.js 但文件不存在时,回退到目录里最新的 main.<hash>.js(以后换 bundle 无需再改 HTML) - tools/split.js:拆分完成后自动改写 index.html 的主文件引用(防复发)
This commit is contained in:
1 parent
fd0dffe31b
commit
469f913619
2 files changed
+35
-1
No files matched your search
+15
-1
@@ -94,7 +94,21 @@ function Resolve(UrlPath) {
|
||||
const Clean = decodeURIComponent(UrlPath.split('?')[0]);
|
||||
// 切割版 JS 走 TankIC 目录
|
||||
if (Clean.startsWith('/play/static/js/')) {
|
||||
return path.join(JS_DIR, Clean.slice('/play/static/js/'.length));
|
||||
const P = path.join(JS_DIR, Clean.slice('/play/static/js/'.length));
|
||||
// 主文件名的 hash 随 bundle 版本变化,而 index.html 里写的是固定名(重新拆分后会失配 → 404)。
|
||||
// 这里兜底:请求 main.<hash>.js 但该文件不存在时,回退到 JS 目录里最新的 main.<hash>.js。
|
||||
if (/[/\\]main\.[0-9a-f]{8}\.js$/.test(P) && !fs.existsSync(P)) {
|
||||
const Newest = fs
|
||||
.readdirSync(JS_DIR)
|
||||
.filter((F) => /^main\.[0-9a-f]{8}\.js$/.test(F))
|
||||
.map((F) => ({ F, T: fs.statSync(path.join(JS_DIR, F)).mtimeMs }))
|
||||
.sort((A, B) => B.T - A.T)[0];
|
||||
if (Newest) {
|
||||
console.log('[serve] 主文件失配,回退到最新: ' + path.basename(P) + ' → ' + Newest.F);
|
||||
return path.join(JS_DIR, Newest.F);
|
||||
}
|
||||
}
|
||||
return P;
|
||||
}
|
||||
const Rel = Clean.replace(/^\/+/, '');
|
||||
return Rel === '' ? path.join(WEB_DIR, 'index.html') : path.join(WEB_DIR, Rel);
|
||||
|
||||
Reference in new issue
Block a user