fix(router): add ErrorBoundary and lazyWithRetry to prevent dynamic import chunk failures
Deploy Canina / deploy (push) Successful in 31s
Deploy Canina / deploy (push) Successful in 31s
This commit is contained in:
1 parent
f430c4931f
commit
f306b95f62
14 files changed
+48329
-47545
No files matched your search
@@ -0,0 +1,35 @@
|
||||
import { ComponentType, lazy } from 'react';
|
||||
|
||||
/**
|
||||
* Wraps dynamic React.lazy imports with retry logic and auto cache-busting
|
||||
* to prevent "Failed to fetch dynamically imported module" errors after deployments.
|
||||
*/
|
||||
export function lazyWithRetry<T extends ComponentType<unknown>>(
|
||||
componentImport: () => Promise<{ default: T }>,
|
||||
retriesLeft = 2,
|
||||
interval = 1000
|
||||
): React.LazyExoticComponent<T> {
|
||||
return lazy(() =>
|
||||
new Promise<{ default: T }>((resolve, reject) => {
|
||||
componentImport()
|
||||
.then(resolve)
|
||||
.catch((error: unknown) => {
|
||||
if (retriesLeft <= 0) {
|
||||
// Check if page was already reloaded for chunk error
|
||||
const hasReloaded = sessionStorage.getItem('chunk_retry_reloaded');
|
||||
if (!hasReloaded) {
|
||||
sessionStorage.setItem('chunk_retry_reloaded', 'true');
|
||||
window.location.reload();
|
||||
return;
|
||||
}
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
lazyWithRetry(componentImport, retriesLeft - 1, interval);
|
||||
}, interval);
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user