Skip to content

Commit 08280a9

Browse files
committed
feat: enhance global error handling to auto-reload transient network errors in the admin panel.
1 parent eee8fe4 commit 08280a9

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/app/global-error.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,27 @@ const GlobalError: React.FC<{
2828
const [isRecovering, setIsRecovering] = useState(false);
2929

3030
useEffect(() => {
31+
// Check if the error is likely due to being offline (e.g., failed to load a JS chunk)
32+
const isOfflineError =
33+
!navigator.onLine ||
34+
error.name === 'ChunkLoadError' ||
35+
error.message.toLowerCase().includes('failed to fetch') ||
36+
error.message.toLowerCase().includes('network error');
37+
38+
// Completely disable global offline redirection for the Payload CMS admin panel.
39+
// Payload has its own offline handling and error boundaries.
40+
const isAdminPanel =
41+
typeof globalThis !== 'undefined' && globalThis.location.pathname.startsWith('/admin');
42+
3143
// In draft/preview mode, auto-reload instead of showing the error page.
3244
// This prevents the Payload CMS Live Preview iframe from getting stuck.
33-
if (isDraftOrPreviewMode()) {
45+
// We also do this for transient network errors in the admin panel to prevent the
46+
// root error boundary from permanently replacing the admin UI with a red error screen.
47+
const isTransientAdminError = isAdminPanel && isOfflineError;
48+
49+
if (isDraftOrPreviewMode() || isTransientAdminError) {
3450
console.warn(
35-
'[GlobalError] Transient error in draft/preview mode. Auto-reloading in 2s:',
51+
'[GlobalError] Transient error in admin/preview mode. Auto-reloading in 2s:',
3652
error.message,
3753
);
3854
setIsRecovering(true);
@@ -44,18 +60,6 @@ const GlobalError: React.FC<{
4460
};
4561
}
4662

47-
// Check if the error is likely due to being offline (e.g., failed to load a JS chunk)
48-
const isOfflineError =
49-
!navigator.onLine ||
50-
error.name === 'ChunkLoadError' ||
51-
error.message.toLowerCase().includes('failed to fetch') ||
52-
error.message.toLowerCase().includes('network error');
53-
54-
// Completely disable global offline redirection for the Payload CMS admin panel.
55-
// Payload has its own offline handling and error boundaries.
56-
const isAdminPanel =
57-
typeof globalThis !== 'undefined' && globalThis.location.pathname.startsWith('/admin');
58-
5963
if (isOfflineError && !isAdminPanel) {
6064
console.error('[GlobalError] Network/Offline error detected:', {
6165
message: error.message,

0 commit comments

Comments
 (0)