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): retry transient chunk-load failures before reload (#65732)

Eleftheria Trivyzakica7166e58034
diff --git a/frontend/src/lib/utils/retryImport.test.ts b/frontend/src/lib/utils/retryImport.test.ts
new file mode 100644
index 000000000000..0df8e5d07b3c
--- /dev/null
+++ b/frontend/src/lib/utils/retryImport.test.ts
@@ -0,0 +1,55 @@
+import { retryImport } from './retryImport'
+
+describe('retryImport', () => {
+ beforeEach(() => {
+ jest.useFakeTimers()
+ })
+
+ afterEach(() => {
+ jest.useRealTimers()
+ })
+
+ const chunkError = (): Error => new Error('Failed to fetch dynamically imported module: /x.js')
+
+ it('resolves on the first attempt without retrying', async () => {
+ const factory = jest.fn().mockResolvedValue('module')
+
+ await expect(retryImport(factory)).resolves.toBe('module')
+ expect(factory).toHaveBeenCalledTimes(1)
+ })
+
+ it('resolves a factory that returns a non-promise value', async () => {
+ const factory = jest.fn().mockReturnValue('module')
+
+ await expect(retryImport(factory)).resolves.toBe('module')
+ expect(factory).toHaveBeenCalledTimes(1)
+ })
+
+ it('retries a transient chunk error and resolves', async () => {
+ const factory = jest.fn().mockRejectedValueOnce(chunkError()).mockResolvedValue('module')
+
+ const promise = retryImport(factory)
+ await jest.runAllTimersAsync()
+
+ await expect(promise).resolves.toBe('module')
+ expect(factory).toHaveBeenCalledTimes(2)
+ })
+
+ it('exhausts retries on a persistent chunk error and rejects', async () => {
+ const factory = jest.fn().mockRejectedValue(chunkError())
+
+ const promise = retryImport(factory)
+ void promise.catch(() => {}) // avoid an unhandled rejection while the backoff timers drain
+ await jest.runAllTimersAsync()
+
+ await expect(promise).rejects.toThrow('Failed to fetch dynamically imported module')
+ expect(factory).toHaveBeenCalledTimes(3)
+ })
+
+ it('rethrows a non-chunk error immediately without retrying', async () => {
+ const factory = jest.fn().mockRejectedValue(new TypeError('undefined is not a function'))
+
+ await expect(retryImport(factory)).rejects.toThrow('undefined is not a function')
+ expect(factory).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/frontend/src/lib/utils/retryImport.ts b/frontend/src/lib/utils/retryImport.ts
new file mode 100644
index 000000000000..5e9f619335c4
--- /dev/null
+++ b/frontend/src/lib/utils/retryImport.ts
@@ -0,0 +1,26 @@
+import { isChunkLoadError } from 'lib/utils/isChunkLoadError'
+
+/**
+ * Re-attempts a dynamic `import()` on a transient chunk-load failure before giving up.
+ *
+ * Most "Failed to fetch dynamically imported module" errors are transient — a network blip,
+ * or a fetch racing an auth redirect — and succeed on a second attempt without a full-page
+ * reload, so page state is preserved. Non-chunk errors and exhausted retries rethrow, so the
+ * existing reload/error recovery (ChunkLoadErrorBoundary, sceneLogic) still runs.
+ *
+ * Bounded by a decrementing counter (1 + `retries` attempts), so it cannot loop.
+ *
+ * `await factory()` normalizes the factory: a synchronous return value or a synchronous throw is
+ * handled just like a resolved/rejected promise.
+ */
+export async function retryImport<T>(factory: () => T, retries = 2, baseDelayMs = 300): Promise<Awaited<T>> {
+ try {
+ return await factory()
+ } catch (error) {
+ if (retries <= 0 || !isChunkLoadError(error)) {
+ throw error
+ }
+ await new Promise<void>((resolve) => setTimeout(resolve, baseDelayMs))
+ return retryImport(factory, retries - 1, baseDelayMs * 2)
+ }
+}
diff --git a/frontend/src/scenes/App.tsx b/frontend/src/scenes/App.tsx
index b66dad678ea8..e4de27d94296 100644
--- a/frontend/src/scenes/App.tsx
+++ b/frontend/src/scenes/App.tsx
@@ -11,6 +11,7 @@ import { SpinnerOverlay } from 'lib/lemon-ui/Spinner/Spinner'
import { autofillReleaseLogic } from 'lib/memory/autofillReleaseLogic'
import { OAuthCallback } from 'lib/oauth/OAuthCallback'
import { oauthLogic } from 'lib/oauth/oauthLogic'
+import { retryImport } from 'lib/utils/retryImport'
import { appLogic } from 'scenes/appLogic'
import { appScenes } from 'scenes/appScenes'
import { sceneLogic } from 'scenes/sceneLogic'
@@ -21,7 +22,7 @@ import { themeLogic } from '~/layout/navigation-3000/themeLogic'
import { ChunkLoadErrorBoundary } from './ChunkLoadErrorBoundary'
-const AuthenticatedShell = React.lazy(() => import('./AuthenticatedShell'))
+const AuthenticatedShell = React.lazy(() => retryImport(() => import('./AuthenticatedShell')))
window.process = MOCK_NODE_PROCESS
diff --git a/frontend/src/scenes/sceneLogic.tsx b/frontend/src/scenes/sceneLogic.tsx
index 33a235cdef99..cc9f2a3de5d4 100644
--- a/frontend/src/scenes/sceneLogic.tsx
+++ b/frontend/src/scenes/sceneLogic.tsx
@@ -12,6 +12,7 @@ import { Spinner } from 'lib/lemon-ui/Spinner'
import { getAppContext } from 'lib/utils/getAppContext'
import { isChunkLoadError } from 'lib/utils/isChunkLoadError'
import { addProjectIdIfMissing, removeProjectIdIfPresent, stripTrailingSlash } from 'lib/utils/kea-router'
+import { retryImport } from 'lib/utils/retryImport'
import { identifierToHuman } from 'lib/utils/strings'
import { getRelativeNextPath } from 'lib/utils/url'
import {
@@ -677,7 +678,10 @@ export const sceneLogic = kea<sceneLogicType>([
let importedScene
try {
window.ESBUILD_LOAD_CHUNKS?.(sceneId)
- importedScene = await props.scenes[sceneId]()
+ // Capture the importer in the narrowed scope; the early guard above ensures it's
+ // defined, but that narrowing wouldn't flow into the retryImport closure.
+ const importScene = props.scenes[sceneId]
+ importedScene = await retryImport(() => importScene())
} catch (error: any) {
if (isChunkLoadError(error)) {
// Reloaded once in the last 20 seconds and now reloading again? Show network error