cloudflare-next-intl 0.9.51 → 0.9.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/src/error_handling/install_global_error_override.js +16 -0
- package/dist/src/error_handling/use_stale_deploy_recovery.d.ts +1 -1
- package/dist/src/error_handling/use_stale_deploy_recovery.js +26 -2
- package/dist/src/server/components/helper_script.js +46 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -690,7 +690,9 @@ through `@intl-config`.
|
|
|
690
690
|
|
|
691
691
|
When a new version of your application is deployed to Cloudflare Workers, users on older client sessions may encounter `ChunkLoadError` or failed dynamic imports when requesting outdated chunks.
|
|
692
692
|
|
|
693
|
-
`IntlHelperScript` renders an early-catch `<script>` (production only, id `stale-deploy-early-catch`) that runs before hydration and listens for `window.error`/`unhandledrejection` events matching the same patterns as `isStaleDeployError` (inlined as JSON, so it stays in sync with `staleDeployPatterns` config), then force-reloads once per build id. This covers the case a React-level recovery (`useStaleDeployRecovery` below) cannot: when the chunk that failed to load is part of your own error boundary/global-error bundle, React never gets a chance to render the recovery UI. Both layers share the same `sessionStorage['stale-deploy-recovery-reloaded']` marker keyed by build id, so they can't double-reload each other. No setup beyond rendering `<IntlHelperScript />` is required.
|
|
693
|
+
`IntlHelperScript` renders an early-catch `<script>` (production only, id `stale-deploy-early-catch`) that runs before hydration and listens for `window.error`/`unhandledrejection` events matching the same patterns as `isStaleDeployError` (inlined as JSON, so it stays in sync with `staleDeployPatterns` config), then force-reloads (throttled to once per 15s, capped at 2 attempts per build id — a 3rd stale-deploy error for the same build id falls through instead of reloading again; the count resets once a new build id is seen). It also listens for `error` events during the capture phase to catch resource-load failures (a same-origin chunk `<script>`/`<link>` 404ing or served with a disallowed MIME type), which fire a non-bubbling, message-less `error` event on the element itself rather than surfacing as a catchable message; a failed third-party resource (analytics, reCAPTCHA, etc.) is ignored, since only the app's own build output can break its module graph. This covers the case a React-level recovery (`useStaleDeployRecovery` below) cannot: when the chunk that failed to load is part of your own error boundary/global-error bundle, React never gets a chance to render the recovery UI. Both layers share the same `sessionStorage['stale-deploy-recovery-reloaded']` marker keyed by build id, so they can't double-reload each other. No setup beyond rendering `<IntlHelperScript />` is required.
|
|
694
|
+
|
|
695
|
+
`installGlobalErrorOverride` (used internally by error reporting setup) also reports these same-tree resource-load failures — a failed `script`/`link` load is reported via `reportError` instead of being silently dropped, since the browser gives it no bubbling event or message to catch any other way.
|
|
694
696
|
|
|
695
697
|
For errors that don't crash the module graph itself (a normal thrown error reaching an error boundary), use `isStaleDeployError` and `clearClientCache` in error boundaries or global error handlers to automatically recover:
|
|
696
698
|
|
|
@@ -16,6 +16,22 @@ export default function installGlobalErrorOverride(config) {
|
|
|
16
16
|
isClient: true,
|
|
17
17
|
});
|
|
18
18
|
});
|
|
19
|
+
window.addEventListener('error', (event) => {
|
|
20
|
+
const el = event.target;
|
|
21
|
+
if (!el || el === window)
|
|
22
|
+
return;
|
|
23
|
+
const tag = el.tagName?.toLowerCase();
|
|
24
|
+
if (tag !== 'script' && tag !== 'link')
|
|
25
|
+
return;
|
|
26
|
+
const src = el.src || el.href;
|
|
27
|
+
if (!src)
|
|
28
|
+
return;
|
|
29
|
+
void reportError(config, {
|
|
30
|
+
error: `Failed to load ${tag} resource: ${src}`,
|
|
31
|
+
classOrMethodName: 'Global Resource Error Handler',
|
|
32
|
+
isClient: true,
|
|
33
|
+
});
|
|
34
|
+
}, true);
|
|
19
35
|
window.addEventListener('unhandledrejection', (event) => {
|
|
20
36
|
void reportError(config, {
|
|
21
37
|
error: event.reason,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export declare function isRecentBuild(setAt: number | null, now: number, windowMs?: number): boolean;
|
|
2
|
-
export declare function shouldRecoverFromStaleDeploy(error: unknown, buildId: string, marker: string | null, recentBuild?: boolean, reloadTime?: number | null, now?: number, throttleMs?: number): boolean;
|
|
2
|
+
export declare function shouldRecoverFromStaleDeploy(error: unknown, buildId: string, marker: string | null, recentBuild?: boolean, reloadTime?: number | null, now?: number, throttleMs?: number, attempts?: number, maxAttempts?: number): boolean;
|
|
3
3
|
export declare function performCacheBustReload(): void;
|
|
4
4
|
export default function useStaleDeployRecovery(error: unknown, onRecover?: () => Promise<unknown>, delayMs?: number): boolean;
|
|
@@ -4,6 +4,8 @@ import isStaleDeployError from './is_stale_deploy_error.js';
|
|
|
4
4
|
import clearClientCache from './clear_client_cache.js';
|
|
5
5
|
const RECOVERY_RELOAD_KEY = 'stale-deploy-recovery-reloaded';
|
|
6
6
|
const RECOVERY_TIME_KEY = 'stale-deploy-recovery-time';
|
|
7
|
+
const RECOVERY_COUNT_KEY = 'stale-deploy-recovery-count';
|
|
8
|
+
const MAX_RECOVERY_ATTEMPTS = 2;
|
|
7
9
|
const BUILD_ID_KEY = 'buildId';
|
|
8
10
|
const BUILD_ID_SET_AT_KEY = 'buildIdSetAt';
|
|
9
11
|
const RECENT_BUILD_WINDOW_MS = 60000;
|
|
@@ -28,16 +30,33 @@ function buildIdSetAt() {
|
|
|
28
30
|
export function isRecentBuild(setAt, now, windowMs = RECENT_BUILD_WINDOW_MS) {
|
|
29
31
|
return setAt !== null && now - setAt < windowMs;
|
|
30
32
|
}
|
|
31
|
-
export function shouldRecoverFromStaleDeploy(error, buildId, marker, recentBuild = false, reloadTime = null, now = Date.now(), throttleMs = RELOAD_THROTTLE_MS) {
|
|
33
|
+
export function shouldRecoverFromStaleDeploy(error, buildId, marker, recentBuild = false, reloadTime = null, now = Date.now(), throttleMs = RELOAD_THROTTLE_MS, attempts = 0, maxAttempts = MAX_RECOVERY_ATTEMPTS) {
|
|
32
34
|
if (!isStaleDeployError(error))
|
|
33
35
|
return false;
|
|
34
36
|
const isRecentlyReloaded = reloadTime !== null && now - reloadTime < throttleMs;
|
|
35
37
|
const isSameBuildMarker = marker !== null && marker !== '' && (buildId === 'unknown' || marker === buildId);
|
|
38
|
+
if (isSameBuildMarker && attempts >= maxAttempts) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
36
41
|
if (isSameBuildMarker && isRecentlyReloaded && !recentBuild) {
|
|
37
42
|
return false;
|
|
38
43
|
}
|
|
39
44
|
return true;
|
|
40
45
|
}
|
|
46
|
+
function currentAttempts(buildId, marker) {
|
|
47
|
+
if (marker === null || marker === '')
|
|
48
|
+
return 0;
|
|
49
|
+
if (buildId !== 'unknown' && marker !== buildId)
|
|
50
|
+
return 0;
|
|
51
|
+
try {
|
|
52
|
+
const raw = sessionStorage.getItem(RECOVERY_COUNT_KEY);
|
|
53
|
+
const parsed = raw ? Number(raw) : 0;
|
|
54
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : 0;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
41
60
|
function canRecover(error) {
|
|
42
61
|
if (typeof window === 'undefined')
|
|
43
62
|
return false;
|
|
@@ -48,7 +67,8 @@ function canRecover(error) {
|
|
|
48
67
|
const marker = sessionStorage.getItem(RECOVERY_RELOAD_KEY);
|
|
49
68
|
const isRecent = isRecentBuild(buildIdSetAt(), Date.now());
|
|
50
69
|
const isStale = isStaleDeployError(error);
|
|
51
|
-
const
|
|
70
|
+
const attempts = currentAttempts(bId, marker);
|
|
71
|
+
const result = shouldRecoverFromStaleDeploy(error, bId, marker, isRecent, reloadTime, Date.now(), RELOAD_THROTTLE_MS, attempts);
|
|
52
72
|
console.warn('[useStaleDeployRecovery]', {
|
|
53
73
|
error,
|
|
54
74
|
isStale,
|
|
@@ -56,6 +76,7 @@ function canRecover(error) {
|
|
|
56
76
|
marker,
|
|
57
77
|
isRecent,
|
|
58
78
|
reloadTime,
|
|
79
|
+
attempts,
|
|
59
80
|
result,
|
|
60
81
|
});
|
|
61
82
|
return result;
|
|
@@ -89,7 +110,10 @@ export default function useStaleDeployRecovery(error, onRecover, delayMs = 1000)
|
|
|
89
110
|
Promise.all([initialOnRecover?.().catch(() => undefined), clearClientCache().catch(() => undefined)])
|
|
90
111
|
.finally(() => {
|
|
91
112
|
try {
|
|
113
|
+
const marker = sessionStorage.getItem(RECOVERY_RELOAD_KEY);
|
|
114
|
+
const spent = currentAttempts(buildId, marker);
|
|
92
115
|
sessionStorage.setItem(RECOVERY_RELOAD_KEY, buildId);
|
|
116
|
+
sessionStorage.setItem(RECOVERY_COUNT_KEY, String(spent + 1));
|
|
93
117
|
sessionStorage.setItem(RECOVERY_TIME_KEY, String(Date.now()));
|
|
94
118
|
}
|
|
95
119
|
catch { }
|
|
@@ -17,6 +17,10 @@ export default function HelperScript() {
|
|
|
17
17
|
try {
|
|
18
18
|
var patterns = ${JSON.stringify(defaultStaleDeployPatterns)};
|
|
19
19
|
var key = 'stale-deploy-recovery-reloaded';
|
|
20
|
+
var timeKey = 'stale-deploy-recovery-time';
|
|
21
|
+
var countKey = 'stale-deploy-recovery-count';
|
|
22
|
+
var maxAttempts = 2;
|
|
23
|
+
var throttleMs = 15000;
|
|
20
24
|
var attemptedThisLoad = false;
|
|
21
25
|
function isStale(msg) {
|
|
22
26
|
if (msg === undefined || msg === null) return true;
|
|
@@ -34,12 +38,31 @@ export default function HelperScript() {
|
|
|
34
38
|
if (!stale) return;
|
|
35
39
|
var buildId = localStorage.getItem('buildId') || 'unknown';
|
|
36
40
|
var marker = sessionStorage.getItem(key);
|
|
37
|
-
|
|
38
|
-
|
|
41
|
+
var lastRaw = sessionStorage.getItem(timeKey);
|
|
42
|
+
var last = lastRaw ? Number(lastRaw) : null;
|
|
43
|
+
var throttled = last !== null && (Date.now() - last) < throttleMs;
|
|
44
|
+
// Attempts are counted per build id: the 1st and 2nd
|
|
45
|
+
// page load may each recover, the 3rd falls through to
|
|
46
|
+
// the error UI. A new deploy resets the count.
|
|
47
|
+
var sameBuild = marker === buildId;
|
|
48
|
+
var attempts = 0;
|
|
49
|
+
if (sameBuild) {
|
|
50
|
+
var rawCount = sessionStorage.getItem(countKey);
|
|
51
|
+
attempts = rawCount ? Number(rawCount) : 0;
|
|
52
|
+
if (!(attempts >= 0)) attempts = 0;
|
|
53
|
+
}
|
|
54
|
+
if (sameBuild && attempts >= maxAttempts) {
|
|
55
|
+
console.warn('[StaleDeploy early-catch] Skipping reload, attempts exhausted for buildId:', buildId, attempts);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (sameBuild && throttled) {
|
|
59
|
+
console.warn('[StaleDeploy early-catch] Skipping reload, throttled for buildId:', buildId);
|
|
39
60
|
return;
|
|
40
61
|
}
|
|
41
62
|
attemptedThisLoad = true;
|
|
42
63
|
sessionStorage.setItem(key, buildId);
|
|
64
|
+
sessionStorage.setItem(countKey, String(attempts + 1));
|
|
65
|
+
sessionStorage.setItem(timeKey, String(Date.now()));
|
|
43
66
|
try {
|
|
44
67
|
if (document.documentElement) {
|
|
45
68
|
document.documentElement.style.backgroundColor = '#ffffff';
|
|
@@ -61,6 +84,27 @@ export default function HelperScript() {
|
|
|
61
84
|
}
|
|
62
85
|
}
|
|
63
86
|
window.addEventListener('error', function(e) { recover(e.message, 'error-event'); });
|
|
87
|
+
// Resource-load failures (a chunk 404ing or served with a
|
|
88
|
+
// disallowed MIME type) fire a non-bubbling 'error' event on the
|
|
89
|
+
// element itself, so they only reach window during capture, and
|
|
90
|
+
// they carry no message. Treat a failed script/link as stale.
|
|
91
|
+
window.addEventListener('error', function(e) {
|
|
92
|
+
try {
|
|
93
|
+
var el = e.target;
|
|
94
|
+
if (!el || el === window) return;
|
|
95
|
+
var tag = (el.tagName || '').toLowerCase();
|
|
96
|
+
if (tag !== 'script' && tag !== 'link') return;
|
|
97
|
+
var src = el.src || el.href || '';
|
|
98
|
+
if (!src) return;
|
|
99
|
+
// Only our own build output can break the React module
|
|
100
|
+
// graph. A failed third-party script (analytics,
|
|
101
|
+
// reCAPTCHA) must never trigger a reload.
|
|
102
|
+
var sameOrigin = false;
|
|
103
|
+
try { sameOrigin = new URL(src, window.location.href).origin === window.location.origin; } catch (err2) { return; }
|
|
104
|
+
if (!sameOrigin) return;
|
|
105
|
+
recover('chunk resource failed to load: ' + src, 'resource-error');
|
|
106
|
+
} catch (err) {}
|
|
107
|
+
}, true);
|
|
64
108
|
window.addEventListener('unhandledrejection', function(e) {
|
|
65
109
|
recover(e.reason && (e.reason.message || e.reason), 'unhandledrejection');
|
|
66
110
|
});
|