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): recover from minified stale chunk errors (#70908)

posthog[bot]2141c61bbf67
diff --git a/frontend/src/index.tsx b/frontend/src/index.tsx
index c959bab4c0a3..d46bb9404e6a 100644
--- a/frontend/src/index.tsx
+++ b/frontend/src/index.tsx
@@ -5,7 +5,7 @@ import './buffer-polyfill'
import { Suspense, lazy } from 'react'
import { createRoot, type Root } from 'react-dom/client'
-import { retryImport } from 'lib/utils/retryImport'
+import { retryBootImport } from 'lib/utils/retryImport'
import { RootErrorBoundary } from './RootErrorBoundary'
import { ChunkLoadErrorBoundary } from './scenes/ChunkLoadErrorBoundary'
@@ -16,7 +16,7 @@ import { ChunkLoadErrorBoundary } from './scenes/ChunkLoadErrorBoundary'
// load and before <App /> first renders. It lives in its own module so scenes/App
// keeps component-only exports and stays a React Fast Refresh boundary.
const App = lazy(() =>
- Promise.all([retryImport(() => import('scenes/App')), retryImport(() => import('scenes/bootApp'))]).then(
+ Promise.all([retryBootImport(() => import('scenes/App')), retryBootImport(() => import('scenes/bootApp'))]).then(
([appModule, bootModule]) => {
bootModule.bootApp()
return { default: appModule.App }
diff --git a/frontend/src/lib/utils/isChunkLoadError.ts b/frontend/src/lib/utils/isChunkLoadError.ts
index 20dfc2fcebce..acb4f7033d79 100644
--- a/frontend/src/lib/utils/isChunkLoadError.ts
+++ b/frontend/src/lib/utils/isChunkLoadError.ts
@@ -7,6 +7,15 @@
* - Firefox: native `TypeError: error loading dynamically imported module: <url>` (deferred import of a now-deleted chunk after a deploy)
* - WebKit/Safari: `Importing a module script failed.` (module script fails to load, e.g. transient network failure)
*/
+const markedChunkLoadErrors = new WeakSet<object>()
+
+export function markAsChunkLoadError(error: unknown): void {
+ if (!error || typeof error !== 'object') {
+ return
+ }
+ markedChunkLoadErrors.add(error)
+}
+
export function isChunkLoadError(error: unknown): boolean {
if (!error || typeof error !== 'object') {
return false
@@ -15,6 +24,7 @@ export function isChunkLoadError(error: unknown): boolean {
const message = typeof err.message === 'string' ? err.message : ''
const isTypeError = err.name === 'TypeError'
return (
+ markedChunkLoadErrors.has(error) ||
err.name === 'ChunkLoadError' ||
message.includes('Failed to fetch dynamically imported module') ||
message.includes('Importing a module script failed') ||
diff --git a/frontend/src/lib/utils/retryImport.test.ts b/frontend/src/lib/utils/retryImport.test.ts
index 0df8e5d07b3c..ce5da7dac7cf 100644
--- a/frontend/src/lib/utils/retryImport.test.ts
+++ b/frontend/src/lib/utils/retryImport.test.ts
@@ -1,4 +1,5 @@
-import { retryImport } from './retryImport'
+import { isChunkLoadError } from './isChunkLoadError'
+import { retryBootImport, retryImport } from './retryImport'
describe('retryImport', () => {
beforeEach(() => {
@@ -46,6 +47,15 @@ describe('retryImport', () => {
expect(factory).toHaveBeenCalledTimes(3)
})
+ it('marks a minified boot module-evaluation TypeError without retrying', async () => {
+ const error = new TypeError('g is not a function')
+ const factory = jest.fn().mockRejectedValue(error)
+
+ await expect(retryBootImport(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 09474e1d3d33..3ca00d8f8079 100644
--- a/frontend/src/lib/utils/retryImport.ts
+++ b/frontend/src/lib/utils/retryImport.ts
@@ -1,6 +1,14 @@
import { ComponentType, LazyExoticComponent, lazy } from 'react'
-import { isChunkLoadError } from 'lib/utils/isChunkLoadError'
+import { isChunkLoadError, markAsChunkLoadError } from 'lib/utils/isChunkLoadError'
+
+function isMinifiedBootModuleEvaluationError(error: unknown): boolean {
+ if (!error || typeof error !== 'object') {
+ return false
+ }
+ const { name, message } = error as { name?: string; message?: string }
+ return name === 'TypeError' && typeof message === 'string' && /^[A-Za-z_$] is not a function$/.test(message)
+}
/**
* Re-attempts a dynamic `import()` on a transient chunk-load failure before giving up.
@@ -27,6 +35,17 @@ export async function retryImport<T>(factory: () => T, retries = 2, baseDelayMs
}
}
+export async function retryBootImport<T>(factory: () => T): Promise<Awaited<T>> {
+ try {
+ return await retryImport(factory)
+ } catch (error) {
+ if (isMinifiedBootModuleEvaluationError(error)) {
+ markAsChunkLoadError(error)
+ }
+ throw error
+ }
+}
+
/**
* Drop-in replacement for `React.lazy` that retries a transient chunk-load failure before giving up.
*