42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
const RECOVERY_KEY_PREFIX = "vignette:chunk-recovery:";
|
|
|
|
type ChunkRecoveryWindow = Window & {
|
|
__vignetteChunkRecoveryInstalled?: boolean;
|
|
};
|
|
|
|
function recoveryKey(pathname: string) {
|
|
return `${RECOVERY_KEY_PREFIX}${pathname}`;
|
|
}
|
|
|
|
/**
|
|
* Cloudflare Pages 배포 전환 뒤 열린 탭이 삭제된 Vite 청크를 요청하면 한 번만
|
|
* 최신 문서를 다시 받는다. 두 번째 실패는 ErrorBoundary로 보내 무한 새로고침을 막는다.
|
|
*/
|
|
export function installChunkRecovery() {
|
|
const recoveryWindow = window as ChunkRecoveryWindow;
|
|
if (recoveryWindow.__vignetteChunkRecoveryInstalled) return;
|
|
recoveryWindow.__vignetteChunkRecoveryInstalled = true;
|
|
|
|
window.addEventListener("vite:preloadError", (event) => {
|
|
const key = recoveryKey(window.location.pathname);
|
|
try {
|
|
if (window.sessionStorage.getItem(key)) return;
|
|
window.sessionStorage.setItem(key, new Date().toISOString());
|
|
} catch {
|
|
// 재시도 상태를 보존할 수 없으면 무한 reload 대신 원래 오류 경계로 보낸다.
|
|
return;
|
|
}
|
|
|
|
event.preventDefault();
|
|
window.location.reload();
|
|
});
|
|
}
|
|
|
|
/** 라우트 청크가 정상 렌더된 뒤에만 같은 경로의 재시도 잠금을 해제한다. */
|
|
export function clearChunkRecoveryMarker(pathname: string) {
|
|
try {
|
|
window.sessionStorage.removeItem(recoveryKey(pathname));
|
|
} catch {
|
|
// storage 접근 불가 환경에서도 정상 렌더는 유지한다.
|
|
}
|
|
}
|