Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(frontend): reload once when a lazy chunk fails to parse (#109863)

posthog[bot]f3413d007097
diff --git a/frontend/src/lib/utils/isChunkLoadError.ts b/frontend/src/lib/utils/isChunkLoadError.ts
index 821eff9a42e4..9c1f39e75a1d 100644
--- a/frontend/src/lib/utils/isChunkLoadError.ts
+++ b/frontend/src/lib/utils/isChunkLoadError.ts
@@ -7,7 +7,8 @@
*
* Safari's/Firefox's generic network TypeErrors (`isGenericNetworkTypeError`) are also a failed
* `import()`'s shape in those browsers, but the message can't tell that apart from an unrelated
- * failed `fetch()` — so they only count here once `retryImport` has marked them.
+ * failed `fetch()` — so they only count here once `retryImport` has marked them. The same holds
+ * for a `SyntaxError` from a chunk that fails to parse.
*/
const markedChunkLoadErrors = new WeakSet<object>()
diff --git a/frontend/src/lib/utils/retryImport.test.ts b/frontend/src/lib/utils/retryImport.test.ts
index 771ef05cf024..38c568984b41 100644
--- a/frontend/src/lib/utils/retryImport.test.ts
+++ b/frontend/src/lib/utils/retryImport.test.ts
@@ -56,6 +56,15 @@ describe('retryImport', () => {
expect(isChunkLoadError(error)).toBe(true)
})
+ it('marks a chunk parse SyntaxError without retrying', async () => {
+ const error = new SyntaxError('Invalid or unexpected token')
+ const factory = jest.fn().mockRejectedValue(error)
+
+ await expect(retryImport(factory)).rejects.toBe(error)
+ expect(factory).toHaveBeenCalledTimes(1)
+ expect(isChunkLoadError(error)).toBe(true)
+ })
+
it('rethrows a non-chunk error immediately without retrying', async () => {
const factory = jest.fn().mockRejectedValue(new TypeError('undefined is not a function'))
diff --git a/frontend/src/lib/utils/retryImport.ts b/frontend/src/lib/utils/retryImport.ts
index 9007b70b0c76..bf0897c42ad4 100644
--- a/frontend/src/lib/utils/retryImport.ts
+++ b/frontend/src/lib/utils/retryImport.ts
@@ -10,6 +10,10 @@ function isMinifiedBootModuleEvaluationError(error: unknown): boolean {
return name === 'TypeError' && typeof message === 'string' && /^[A-Za-z_$] is not a function$/.test(message)
}
+function isSyntaxError(error: unknown): boolean {
+ return !!error && typeof error === 'object' && (error as { name?: string }).name === 'SyntaxError'
+}
+
/**
* Re-attempts a dynamic `import()` on a transient chunk-load failure before giving up.
*
@@ -27,6 +31,12 @@ export async function retryImport<T>(factory: () => T, retries = 2, baseDelayMs
try {
return await factory()
} catch (error) {
+ // A truncated or corrupted chunk fails to parse. The browser caches the failed module per URL,
+ // so a retry gets the same error. Only a page reload fetches the chunk again.
+ if (isSyntaxError(error)) {
+ markAsChunkLoadError(error)
+ throw error
+ }
if (!isChunkLoadError(error) && !isGenericNetworkTypeError(error)) {
throw error
}