Files
monkey-ai-web/src/helpers/chunkReload.js
T
zhanjunhao cb9d836227 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

137 lines
3.6 KiB
JavaScript

const CHUNK_RELOAD_KEY = 'weelinking:chunk-reload';
const CHUNK_RELOAD_PARAM = '__wl_reload';
const RELOAD_COOLDOWN_MS = 60 * 1000;
const CHUNK_ERROR_PATTERNS = [
/Failed to fetch dynamically imported module/i,
/error loading dynamically imported module/i,
/Importing a module script failed/i,
/Failed to load module script/i,
/Expected a JavaScript module script/i,
/module script.*MIME type/i,
/disallowed MIME type/i,
/Loading chunk [\w-]+ failed/i,
/ChunkLoadError/i,
/Unable to preload CSS/i,
];
function getErrorMessage(error) {
if (!error) return '';
if (typeof error === 'string') return error;
if (typeof error.message === 'string') return error.message;
if (typeof error.reason === 'string') return error.reason;
if (error.reason) return getErrorMessage(error.reason);
try {
return String(error);
} catch {
return '';
}
}
function getStoredReload() {
try {
return JSON.parse(sessionStorage.getItem(CHUNK_RELOAD_KEY) || 'null');
} catch {
return null;
}
}
function getReloadFingerprint() {
const url = new URL(window.location.href);
url.searchParams.delete(CHUNK_RELOAD_PARAM);
return `${url.origin}${url.pathname}${url.search}`;
}
function getReloadUrl() {
const url = new URL(window.location.href);
url.searchParams.set(CHUNK_RELOAD_PARAM, String(Date.now()));
return url.toString();
}
function rememberReload(message) {
try {
sessionStorage.setItem(
CHUNK_RELOAD_KEY,
JSON.stringify({
fingerprint: getReloadFingerprint(),
href: window.location.href,
time: Date.now(),
message: message.slice(0, 200),
}),
);
} catch {
// Ignore storage failures; reloading is still safe.
}
}
export function isChunkLoadError(error) {
const message = getErrorMessage(error);
return CHUNK_ERROR_PATTERNS.some((pattern) => pattern.test(message));
}
export function reloadForChunkError(error, { force = false } = {}) {
if (typeof window === 'undefined') return false;
if (!force && !isChunkLoadError(error)) return false;
const message = getErrorMessage(error);
const previousReload = getStoredReload();
const fingerprint = getReloadFingerprint();
const now = Date.now();
if (
(previousReload?.fingerprint || previousReload?.href) === fingerprint &&
now - previousReload.time < RELOAD_COOLDOWN_MS
) {
return false;
}
rememberReload(message || 'chunk load failed');
window.location.replace(getReloadUrl());
return true;
}
export function installChunkReloadHandlers() {
if (typeof window === 'undefined') return;
if (window.__weelinkingChunkReloadHandlersInstalled) return;
window.__weelinkingChunkReloadHandlersInstalled = true;
window.addEventListener('vite:preloadError', (event) => {
if (reloadForChunkError(event.payload, { force: true })) {
event.preventDefault();
}
});
window.addEventListener('unhandledrejection', (event) => {
if (reloadForChunkError(event.reason)) {
event.preventDefault();
}
});
window.addEventListener(
'error',
(event) => {
const target = event.target;
const url = target?.src || target?.href || '';
const isStaticModule =
url.includes('/assets/') && /\.(js|css)(\?|$)/i.test(url);
if (isStaticModule) {
const error = new Error(
`Failed to fetch dynamically imported module: ${url}`,
);
if (reloadForChunkError(error, { force: true })) {
event.preventDefault();
}
return;
}
if (reloadForChunkError(event.error || event.message)) {
event.preventDefault();
}
},
true,
);
}