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): fall back to the default build when a stable chunk fails (#108957)

posthog[bot]233686435f9b
diff --git a/frontend/src/layout/AppLoadError.tsx b/frontend/src/layout/AppLoadError.tsx
index 4e856ae7a5ba..0d98219a2429 100644
--- a/frontend/src/layout/AppLoadError.tsx
+++ b/frontend/src/layout/AppLoadError.tsx
@@ -31,8 +31,9 @@ export function AppLoadError({ error }: AppLoadErrorProps): JSX.Element {
<div className="p-4">
<h1 className="mb-1 text-2xl font-bold">PostHog could not load</h1>
<p>
- Your browser could not download part of the app. This is usually a network problem. Reload the page to
- try again. If it happens again, try a different network, or turn off browser extensions and proxies.
+ Your browser could not download part of the app. This can happen for a few minutes after we release an
+ update, or because of a network problem. Wait a minute, then reload the page. If it keeps happening, try
+ a different network, turn off browser extensions and proxies, or email an engineer.
</p>
<div className="flex gap-2 flex-wrap">
<LemonButton
diff --git a/frontend/src/lib/utils/stableChunks.test.ts b/frontend/src/lib/utils/stableChunks.test.ts
new file mode 100644
index 000000000000..77f471ad047c
--- /dev/null
+++ b/frontend/src/lib/utils/stableChunks.test.ts
@@ -0,0 +1,41 @@
+import { reloadAfterChunkLoadError } from './stableChunks'
+
+describe('reloadAfterChunkLoadError', () => {
+ const originalLocation = window.location
+ let reload: jest.Mock
+ let replace: jest.Mock
+
+ beforeEach(() => {
+ document.head.innerHTML = ''
+ reload = jest.fn()
+ replace = jest.fn()
+ Object.defineProperty(window, 'location', {
+ configurable: true,
+ value: { href: 'https://us.posthog.com/project/1/dashboard?tab=a#panel=x', reload, replace },
+ })
+ })
+
+ afterEach(() => {
+ Object.defineProperty(window, 'location', { configurable: true, value: originalLocation })
+ })
+
+ it('reloads the same page on the default build', () => {
+ reloadAfterChunkLoadError()
+
+ expect(reload).toHaveBeenCalledTimes(1)
+ expect(replace).not.toHaveBeenCalled()
+ })
+
+ it('loads the page on the default build when the stable build fails, keeping the rest of the URL', () => {
+ const importMap = document.createElement('script')
+ importMap.type = 'importmap'
+ document.head.appendChild(importMap)
+
+ reloadAfterChunkLoadError()
+
+ expect(reload).not.toHaveBeenCalled()
+ expect(replace).toHaveBeenCalledWith(
+ 'https://us.posthog.com/project/1/dashboard?tab=a&stable_chunks=fallback#panel=x'
+ )
+ })
+})
diff --git a/frontend/src/lib/utils/stableChunks.ts b/frontend/src/lib/utils/stableChunks.ts
new file mode 100644
index 000000000000..f13388308c5d
--- /dev/null
+++ b/frontend/src/lib/utils/stableChunks.ts
@@ -0,0 +1,20 @@
+/** Only the stable chunk build renders an import map (see posthog/stable_chunks.py). */
+export function isStableChunkBuild(): boolean {
+ return document.querySelector('script[type="importmap"]') !== null
+}
+
+/**
+ * Reloads the page after a chunk failed to load. On the stable build, a plain reload gets the same
+ * import map and fails again while a new stable chunk is not reachable, so the reload asks for the
+ * default build. The server always serves the default build for ?stable_chunks=fallback and stores
+ * no choice for it, so this cannot loop, and the next page load tries the stable build again.
+ */
+export function reloadAfterChunkLoadError(): void {
+ if (!isStableChunkBuild()) {
+ window.location.reload()
+ return
+ }
+ const url = new URL(window.location.href)
+ url.searchParams.set('stable_chunks', 'fallback')
+ window.location.replace(url.toString())
+}
diff --git a/frontend/src/scenes/ChunkLoadErrorBoundary.test.tsx b/frontend/src/scenes/ChunkLoadErrorBoundary.test.tsx
index 8af50d32e245..5a4af36db57d 100644
--- a/frontend/src/scenes/ChunkLoadErrorBoundary.test.tsx
+++ b/frontend/src/scenes/ChunkLoadErrorBoundary.test.tsx
@@ -7,6 +7,13 @@ import { ChunkLoadErrorBoundary } from './ChunkLoadErrorBoundary'
const RELOAD_GUARD_KEY = 'posthog-chunk-reload-at'
+function renderStableBuildImportMap(): void {
+ const importMap = document.createElement('script')
+ importMap.type = 'importmap'
+ importMap.textContent = '{"imports":{}}'
+ document.head.appendChild(importMap)
+}
+
class TestErrorBoundary extends Component<{ children: ReactNode }, { error: Error | null }> {
override state: { error: Error | null } = { error: null }
@@ -42,6 +49,7 @@ describe('ChunkLoadErrorBoundary', () => {
beforeEach(() => {
window.localStorage.clear()
+ document.head.innerHTML = ''
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {})
})
@@ -86,6 +94,23 @@ describe('ChunkLoadErrorBoundary', () => {
).toBeInTheDocument()
})
+ it('reloads on the stable build even after a recent reload, because that reload leaves the stable build', () => {
+ const reload = jest.fn()
+ window.localStorage.setItem(RELOAD_GUARD_KEY, String(Date.now()))
+ renderStableBuildImportMap()
+
+ render(
+ <TestErrorBoundary>
+ <ChunkLoadErrorBoundary reload={reload} fallback={() => <div>fallback</div>}>
+ <ThrowChunkError />
+ </ChunkLoadErrorBoundary>
+ </TestErrorBoundary>
+ )
+
+ expect(reload).toHaveBeenCalledTimes(1)
+ expect(screen.queryByText('fallback')).not.toBeInTheDocument()
+ })
+
it('renders the fallback for repeated chunk errors when one is provided', () => {
const reload = jest.fn()
window.localStorage.setItem(RELOAD_GUARD_KEY, String(Date.now()))
diff --git a/frontend/src/scenes/ChunkLoadErrorBoundary.tsx b/frontend/src/scenes/ChunkLoadErrorBoundary.tsx
index f614f1e74188..565ff8c41324 100644
--- a/frontend/src/scenes/ChunkLoadErrorBoundary.tsx
+++ b/frontend/src/scenes/ChunkLoadErrorBoundary.tsx
@@ -1,6 +1,7 @@
import { Component, type ReactNode } from 'react'
import { isChunkLoadError } from 'lib/utils/isChunkLoadError'
+import { isStableChunkBuild, reloadAfterChunkLoadError } from 'lib/utils/stableChunks'
const RELOAD_GUARD_KEY = 'posthog-chunk-reload-at'
const RELOAD_GUARD_WINDOW_MS = 20_000
@@ -14,7 +15,8 @@ interface State {
* Catches chunk-load failures from `React.lazy(() => import(...))` boundaries.
* On a stale-deploy chunk-hash mismatch we reload once; if a reload was already
* attempted in the last 20s we render `fallback` when given, or else let the error
- * bubble to the outer ErrorBoundary rather than spinning forever. Non-chunk errors
+ * bubble to the outer ErrorBoundary rather than spinning forever. On the stable chunk
+ * build we always reload on the default build, which cannot loop. Non-chunk errors
* are re-thrown so the regular error UI still renders.
*/
interface ChunkLoadErrorBoundaryProps {
@@ -40,7 +42,7 @@ export class ChunkLoadErrorBoundary extends Component<ChunkLoadErrorBoundaryProp
} catch {
// localStorage may be unavailable (e.g. Safari private mode) - treat as no prior reload
}
- if (lastReload && Date.now() - lastReload < RELOAD_GUARD_WINDOW_MS) {
+ if (lastReload && Date.now() - lastReload < RELOAD_GUARD_WINDOW_MS && !isStableChunkBuild()) {
console.error('[ChunkLoadErrorBoundary] Recently reloaded; surfacing error instead of looping.')
this.setState({ surface: true })
return
@@ -56,7 +58,7 @@ export class ChunkLoadErrorBoundary extends Component<ChunkLoadErrorBoundaryProp
if (this.props.reload) {
this.props.reload()
} else {
- window.location.reload()
+ reloadAfterChunkLoadError()
}
}
diff --git a/frontend/src/scenes/sceneLogic.tsx b/frontend/src/scenes/sceneLogic.tsx
index f11448ab5292..9e89783d22bd 100644
--- a/frontend/src/scenes/sceneLogic.tsx
+++ b/frontend/src/scenes/sceneLogic.tsx
@@ -25,6 +25,7 @@ import { getAppContext } from 'lib/utils/getAppContext'
import { isChunkLoadError } from 'lib/utils/isChunkLoadError'
import { addProjectIdIfMissing, getProjectIdentifierInPath, removeProjectIdIfPresent } from 'lib/utils/kea-router'
import { retryImport } from 'lib/utils/retryImport'
+import { isStableChunkBuild, reloadAfterChunkLoadError } from 'lib/utils/stableChunks'
import { identifierToHuman } from 'lib/utils/strings'
import { getRelativeNextPath } from 'lib/utils/url'
import {
@@ -1017,6 +1018,7 @@ export const sceneLogic = kea<sceneLogicType>([
if (isChunkLoadError(error)) {
// Reloaded once in the last 20 seconds and now reloading again? Show network error
if (
+ !isStableChunkBuild() &&
values.lastReloadAt &&
parseInt(String(values.lastReloadAt)) > new Date().valueOf() - 20000
) {
@@ -1063,7 +1065,7 @@ export const sceneLogic = kea<sceneLogicType>([
actions.setScene(sceneId, sceneKey, params, clickedLink || wasNotLoaded, exportedScene)
},
reloadBrowserDueToImportError: () => {
- window.location.reload()
+ reloadAfterChunkLoadError()
},
})),