155 lines
4.3 KiB
JavaScript
155 lines
4.3 KiB
JavaScript
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
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,
|
|
);
|
|
}
|