: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(frontend): recover the support router from a stale-deploy chunk failure (#104343)
posthog[bot]5467b845b75c
diff --git a/frontend/src/scenes/App.tsx b/frontend/src/scenes/App.tsxindex 7809ff890580..3d909674993d 100644--- a/frontend/src/scenes/App.tsx+++ b/frontend/src/scenes/App.tsx@@ -27,6 +27,7 @@ import { themeLogic } from '~/layout/navigation-3000/themeLogic'import { AuthenticatedShellFallback } from './AuthenticatedShellFallback'import { ChunkLoadErrorBoundary } from './ChunkLoadErrorBoundary'+import { mountSupportRouter } from './mountSupportRouter'const AuthenticatedShell = React.lazy(() => retryImport(() => import('./AuthenticatedShell')))@@ -90,13 +91,7 @@ export function App(): JSX.Element | null {// Mount the support-hash router (handles #panel=support) on every page, lazily so it stays out// of App's import graph — a static import drags supportLogic/sceneLogic/organizationLogic into// root init and triggers a circular-import TDZ. Its urlToAction fires on the current URL on mount.- useEffect(() => {- let unmount: (() => void) | undefined- void retryImport(() => import('lib/components/Support/supportRouterLogic')).then(({ supportRouterLogic }) => {- unmount = supportRouterLogic.mount()- })- return () => unmount?.()- }, [])+ useEffect(() => mountSupportRouter().unmount, [])useThemedHtml()diff --git a/frontend/src/scenes/mountSupportRouter.test.ts b/frontend/src/scenes/mountSupportRouter.test.tsnew file mode 100644index 000000000000..d6fd178fad7f--- /dev/null+++ b/frontend/src/scenes/mountSupportRouter.test.ts@@ -0,0 +1,66 @@+import { mountSupportRouter } from './mountSupportRouter'++const mount = jest.fn()+let importError: Error | null = null++jest.mock('lib/components/Support/supportRouterLogic', () => ({+ get supportRouterLogic() {+ if (importError) {+ throw importError+ }+ return { mount }+ },+}))++describe('mountSupportRouter', () => {+ beforeEach(() => {+ importError = null+ mount.mockReset().mockReturnValue(jest.fn())+ jest.spyOn(console, 'warn').mockImplementation(() => {})+ })++ afterEach(() => {+ jest.restoreAllMocks()+ })++ it('mounts the router and unmounts it again', async () => {+ const unmountRouter = jest.fn()+ mount.mockReturnValue(unmountRouter)++ const { unmount, mounted } = mountSupportRouter()+ await mounted++ expect(mount).toHaveBeenCalledTimes(1)+ unmount()+ expect(unmountRouter).toHaveBeenCalledTimes(1)+ })++ it('does not mount a router that resolves after the caller disposed', async () => {+ const { unmount, mounted } = mountSupportRouter()+ unmount()+ await mounted++ expect(mount).not.toHaveBeenCalled()+ })++ it.each([+ ['Firefox', 'error loading dynamically imported module: https://app/chunk.js'],+ ['esbuild', 'Failed to fetch dynamically imported module'],+ ['WebKit', 'Importing a module script failed.'],+ ])('swallows a %s chunk-load failure instead of rejecting', async (_browser, message) => {+ importError = new TypeError(message)++ const { unmount, mounted } = mountSupportRouter()+ await expect(mounted).resolves.toBeUndefined()++ expect(console.warn).toHaveBeenCalled()+ expect(() => unmount()).not.toThrow()+ })++ it('rethrows an error that is not a chunk-load failure', async () => {+ importError = new Error('supportRouterLogic is broken')++ const { mounted } = mountSupportRouter()+ await expect(mounted).rejects.toThrow('supportRouterLogic is broken')+ })+})diff --git a/frontend/src/scenes/mountSupportRouter.ts b/frontend/src/scenes/mountSupportRouter.tsnew file mode 100644index 000000000000..ea04b6bd7a39--- /dev/null+++ b/frontend/src/scenes/mountSupportRouter.ts@@ -0,0 +1,28 @@+import { isChunkLoadError } from 'lib/utils/isChunkLoadError'+import { retryImport } from 'lib/utils/retryImport'++export function mountSupportRouter(): { unmount: () => void; mounted: Promise<void> } {+ let disposed = false+ let unmountRouter: (() => void) | undefined+ const mounted = retryImport(() => import('lib/components/Support/supportRouterLogic'))+ .then(({ supportRouterLogic }) => {+ if (disposed) {+ return+ }+ unmountRouter = supportRouterLogic.mount()+ })+ .catch((error) => {+ if (!isChunkLoadError(error)) {+ throw error+ }+ console.warn('[App] Support router chunk failed to load; #panel=support is inactive', error)+ })+ return {+ unmount: () => {+ disposed = true+ unmountRouter?.()+ unmountRouter = undefined+ },+ mounted,+ }+}