Commit f2573ed3 authored by Jimmy Lai's avatar Jimmy Lai

Improve chunk error detection and remove barrel file

- Improve isNetworkError detection with browser-specific messages
- Make detection more conservative (don't retry AbortError, CORS errors)
- Remove index.ts barrel file - import directly from source files
- Update error-boundary.tsx imports to use direct paths

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: 's avatarClaude Opus 4.5 <noreply@anthropic.com>
parent 8190c21d
export {
isChunkLoadError,
isNetworkError,
isChunkOrNetworkError,
} from './is-chunk-load-error'
export { type ChunkErrorAction } from './chunk-load-error-handler'
export {
ChunkLoadErrorBanner,
type ChunkLoadErrorBannerProps,
} from './chunk-load-error-banner'
......@@ -4,10 +4,15 @@
* ChunkLoadError can come from:
* - Turbopack: error.name === 'ChunkLoadError'
* - Webpack: error.name === 'ChunkLoadError'
*
* We intentionally keep detection narrow to avoid false positives:
* - Server 4xx/5xx responses don't throw (they return Response objects)
* - We should NOT retry CORS errors, AbortErrors, or parsing failures
*/
/**
* Detects if an error is a ChunkLoadError from Turbopack or webpack.
* This is the most reliable check - both bundlers explicitly set this error name.
*/
export function isChunkLoadError(error: unknown): error is Error {
if (!error || typeof error !== 'object') return false
......@@ -15,22 +20,47 @@ export function isChunkLoadError(error: unknown): error is Error {
}
/**
* Detects network-related errors (fetch failures, offline, etc.)
* Detects network-level fetch failures that are worth retrying.
*
* We're intentionally conservative here to avoid retrying errors that won't benefit:
* - AbortError: Intentional cancellation, should not retry
* - CORS errors: Configuration issue, retry won't help
* - Server errors: These don't throw (return Response), but if they did, retry might not help
*
* We only catch errors that indicate the network request itself failed,
* not errors from processing the response.
*/
export function isNetworkError(error: unknown): error is Error {
if (!error || typeof error !== 'object') return false
const err = error as Error
// Check for typical network error indicators
if (err.name === 'TypeError' && err.message?.includes('fetch')) return true
if (err.message?.includes('NetworkError')) return true
if (err.message?.includes('Failed to fetch')) return true
// Never retry intentional aborts
if (err.name === 'AbortError') return false
// TypeError with specific fetch failure messages
// These occur when the network request itself fails (not CORS, not server error)
if (err.name === 'TypeError') {
const message = err.message || ''
// Browser-specific network failure messages
// Chrome/Edge: "Failed to fetch"
// Firefox: "NetworkError when attempting to fetch resource"
// Safari: "Load failed" or "The Internet connection appears to be offline"
if (
message === 'Failed to fetch' ||
message.startsWith('NetworkError when attempting to fetch') ||
message === 'Load failed' ||
message.includes('Internet connection appears to be offline') ||
message.includes('network connection was lost')
) {
return true
}
}
return false
}
/**
* Returns true if the error is a chunk load or network error.
* Returns true if the error is a chunk load or network error worth retrying.
*/
export function isChunkOrNetworkError(error: unknown): boolean {
return isChunkLoadError(error) || isNetworkError(error)
......
......@@ -6,11 +6,9 @@ import { isNextRouterError } from './is-next-router-error'
import { handleHardNavError } from './nav-failure-handler'
import { HandleISRError } from './handle-isr-error'
import { isBot } from '../../shared/lib/router/utils/is-bot'
import {
isChunkOrNetworkError,
ChunkLoadErrorBanner,
type ChunkErrorAction,
} from './chunk-load-error'
import { isChunkOrNetworkError } from './chunk-load-error/is-chunk-load-error'
import { ChunkLoadErrorBanner } from './chunk-load-error/chunk-load-error-banner'
import type { ChunkErrorAction } from './chunk-load-error/chunk-load-error-handler'
const isBotUserAgent =
typeof window !== 'undefined' && isBot(window.navigator.userAgent)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment