fix(vite): handle vite:preloadError and dynamic chunk mismatch seamlessly
Deploy Canina / deploy (push) Successful in 32s
Deploy Canina / deploy (push) Successful in 32s
This commit is contained in:
1 parent
f306b95f62
commit
c1d380c765
12 files changed
+5245
-4895
No files matched your search
@@ -1,7 +1,33 @@
|
||||
import { StrictMode } from 'react'
|
||||
import { createRoot } from 'react-dom/client'
|
||||
import './index.css'
|
||||
import App from './App.tsx'
|
||||
|
||||
// Handle Vite dynamic import chunk mismatch after fresh deployment
|
||||
window.addEventListener('vite:preloadError', (event) => {
|
||||
event.preventDefault();
|
||||
console.warn('[Vite] Preload error detected, reloading to fetch latest bundle...');
|
||||
window.location.reload();
|
||||
});
|
||||
|
||||
window.addEventListener('error', (e) => {
|
||||
const msg = e.message || '';
|
||||
if (
|
||||
msg.includes('Failed to fetch dynamically imported module') ||
|
||||
msg.includes('Importing a module script failed') ||
|
||||
msg.includes('error loading dynamically imported module')
|
||||
) {
|
||||
const lastReload = sessionStorage.getItem('vite_chunk_error_reload');
|
||||
const now = Date.now();
|
||||
if (!lastReload || now - parseInt(lastReload, 10) > 8000) {
|
||||
sessionStorage.setItem('vite_chunk_error_reload', String(now));
|
||||
window.location.reload();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
<App />
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>,
|
||||
)
|
||||
Reference in new issue
Block a user