Skip to content

Commit bff8c3d

Browse files
author
Evie Gauthier
committed
Add automatic retry for chunk loading failures
- Global error handler catches chunk load errors - Auto-reload on first 2 failures without user intervention - On 3rd failure, let error bubble to Sentry error boundary - Uses sessionStorage to track retry count across reloads - Clears counter on successful page load - Prevents stale HTML from breaking app after deployments
1 parent 77046ab commit bff8c3d

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

src/index.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,41 @@ const injectIOSMetaTags = () => {
143143

144144
injectIOSMetaTags();
145145

146+
// Handle chunk loading failures with automatic retry
147+
const CHUNK_RETRY_KEY = 'cinny_chunk_retry_count';
148+
const MAX_CHUNK_RETRIES = 2;
149+
150+
window.addEventListener('error', (event) => {
151+
// Check if this is a chunk loading error
152+
const isChunkLoadError =
153+
event.message?.includes('dynamically imported module') ||
154+
event.message?.includes('Failed to fetch') ||
155+
(event.error?.name === 'ChunkLoadError');
156+
157+
if (isChunkLoadError) {
158+
const retryCount = parseInt(sessionStorage.getItem(CHUNK_RETRY_KEY) ?? '0', 10);
159+
160+
if (retryCount < MAX_CHUNK_RETRIES) {
161+
// Increment retry count and reload
162+
sessionStorage.setItem(CHUNK_RETRY_KEY, String(retryCount + 1));
163+
log.warn(`Chunk load failed, reloading (attempt ${retryCount + 1}/${MAX_CHUNK_RETRIES})`);
164+
window.location.reload();
165+
166+
// Prevent default error handling since we're reloading
167+
event.preventDefault();
168+
} else {
169+
// Max retries exceeded, clear counter and let error bubble up
170+
sessionStorage.removeItem(CHUNK_RETRY_KEY);
171+
log.error('Chunk load failed after max retries, showing error');
172+
}
173+
}
174+
});
175+
176+
// Clear chunk retry counter on successful page load
177+
window.addEventListener('load', () => {
178+
sessionStorage.removeItem(CHUNK_RETRY_KEY);
179+
});
180+
146181
const mountApp = () => {
147182
const rootContainer = document.getElementById('root');
148183

0 commit comments

Comments
 (0)