: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): preload app modules while the stylesheet loads (#96200)
Paul D'Ambrad0d87201f657
diff --git a/frontend/src/index.test.tsx b/frontend/src/index.test.tsxnew file mode 100644index 000000000000..087ce6d12d1b--- /dev/null+++ b/frontend/src/index.test.tsx@@ -0,0 +1,176 @@+import '@testing-library/jest-dom'++import { act } from '@testing-library/react'+import * as React from 'react'+import { createRoot } from 'react-dom/client'++jest.mock('~/styles', () => ({}))++function deferred<T>(): { promise: Promise<T>; resolve: (value: T) => void } {+ let resolve!: (value: T) => void+ const promise = new Promise<T>((resolvePromise) => {+ resolve = resolvePromise+ })+ return { promise, resolve }+}++describe('app entry boot', () => {+ let configured: boolean+ let modulesLoaded: string[]+ let bootApp: jest.Mock+ let stylesheet: ReturnType<typeof deferred<boolean>>+ let documentListeners: jest.SpyInstance<void, Parameters<typeof document.addEventListener>>++ beforeEach(() => {+ jest.resetModules()+ jest.useFakeTimers()+ configured = false+ modulesLoaded = []+ bootApp = jest.fn()+ stylesheet = deferred<boolean>()+ window.ESBUILD_CSS_READY = stylesheet.promise+ document.body.innerHTML = '<div id="root"></div>'+ window.__posthogAppRoot = createRoot(document.getElementById('root')!)+ jest.spyOn(document, 'readyState', 'get').mockReturnValue('complete')+ documentListeners = jest.spyOn(document, 'addEventListener')+ jest.doMock('react', () => React)+ jest.doMock('react-dom/client', () => ({ createRoot }))+ jest.doMock('lib/configureZod', () => ({+ configureZod: () => {+ configured = true+ },+ }))+ jest.doMock('scenes/App', () => {+ if (!configured) {+ throw new Error('App evaluated before Zod configuration')+ }+ modulesLoaded.push('App')+ return {+ App: (): JSX.Element => {+ if (bootApp.mock.calls.length !== 1) {+ throw new Error('App rendered without one-time boot')+ }+ return <div data-attr="boot-test-app" />+ },+ }+ })+ jest.doMock('scenes/bootApp', () => {+ if (!configured) {+ throw new Error('bootApp evaluated before Zod configuration')+ }+ modulesLoaded.push('bootApp')+ return { bootApp }+ })+ })++ afterEach(async () => {+ for (const [type, listener, options] of documentListeners.mock.calls) {+ if (type === 'DOMContentLoaded') {+ document.removeEventListener(type, listener, options)+ }+ }+ await act(async () => window.__posthogAppRoot?.unmount())+ delete window.__posthogAppRoot+ delete window.ESBUILD_CSS_READY+ document.body.innerHTML = ''+ jest.clearAllTimers()+ jest.useRealTimers()+ jest.restoreAllMocks()+ })++ async function loadEntry(): Promise<void> {+ await act(async () => {+ await jest.isolateModulesAsync(async () => {+ await import('./index')+ })+ })+ }++ it.each(['complete', 'loading'] as const)(+ 'loads modules while CSS is pending after the document is %s',+ async (readyState) => {+ const documentState = jest.spyOn(document, 'readyState', 'get').mockReturnValue(readyState)+ await loadEntry()++ expect(configured).toBe(readyState === 'complete')+ expect(modulesLoaded).toEqual(readyState === 'complete' ? ['App', 'bootApp'] : [])+ if (readyState === 'loading') {+ documentState.mockReturnValue('complete')+ await act(async () => {+ document.dispatchEvent(new Event('DOMContentLoaded'))+ })+ }++ expect(modulesLoaded).toEqual(['App', 'bootApp'])+ expect(bootApp).not.toHaveBeenCalled()+ expect(document.getElementById('root')).toBeEmptyDOMElement()++ await act(async () => stylesheet.resolve(true))++ expect(document.querySelector('[data-attr="boot-test-app"]')).toBeInTheDocument()+ expect(bootApp).toHaveBeenCalledTimes(1)+ }+ )++ it.each(['loaded', 'failed', 'absent'] as const)('boots when the stylesheet is %s', async (stylesheetState) => {+ if (stylesheetState === 'absent') {+ delete window.ESBUILD_CSS_READY+ }+ await loadEntry()++ await act(async () => {+ if (stylesheetState === 'loaded' || stylesheetState === 'failed') {+ stylesheet.resolve(stylesheetState === 'loaded')+ }+ })++ expect(document.querySelector('[data-attr="boot-test-app"]')).toBeInTheDocument()+ expect(bootApp).toHaveBeenCalledTimes(1)+ })++ it('boots after five seconds if the stylesheet stays pending', async () => {+ await loadEntry()+ await act(async () => {+ await jest.advanceTimersByTimeAsync(4999)+ })+ expect(bootApp).not.toHaveBeenCalled()+ expect(document.getElementById('root')).toBeEmptyDOMElement()++ await act(async () => {+ await jest.advanceTimersByTimeAsync(1)+ })+ expect(document.querySelector('[data-attr="boot-test-app"]')).toBeInTheDocument()+ expect(bootApp).toHaveBeenCalledTimes(1)+ })++ it.each(['configuration', 'module'] as const)(+ 'shows an early %s failure through the boot error boundary after CSS is ready',+ async (failureStage) => {+ jest.spyOn(console, 'error').mockImplementation(() => {})+ const error = new Error('Boot dependency failed')+ if (failureStage === 'configuration') {+ jest.doMock('lib/configureZod', () => ({+ configureZod: () => {+ throw error+ },+ }))+ } else {+ jest.doMock('scenes/App', () => {+ throw error+ })+ }++ await loadEntry()+ await act(async () => {+ await jest.advanceTimersByTimeAsync(0)+ })+ expect(document.getElementById('root')).toBeEmptyDOMElement()+ expect(bootApp).not.toHaveBeenCalled()++ await act(async () => stylesheet.resolve(true))++ expect(document.querySelector('[role="alert"]')).toHaveTextContent('PostHog crashed while starting.')+ expect(bootApp).not.toHaveBeenCalled()+ }+ )+})diff --git a/frontend/src/index.tsx b/frontend/src/index.tsxindex effd98638213..a08dd42dc954 100644--- a/frontend/src/index.tsx+++ b/frontend/src/index.tsx@@ -10,6 +10,20 @@ import { retryBootImport } from 'lib/utils/retryImport'import { RootErrorBoundary } from './RootErrorBoundary'import { ChunkLoadErrorBoundary } from './scenes/ChunkLoadErrorBoundary'+type AppModules = [typeof import('scenes/App'), typeof import('scenes/bootApp')]++let appModulesPromise: Promise<AppModules> | undefined++function loadAppModules(): Promise<AppModules> {+ return (appModulesPromise ??= retryBootImport(() => import('lib/configureZod')).then(({ configureZod }) => {+ configureZod()+ return Promise.all([+ retryBootImport(() => import('scenes/App')),+ retryBootImport(() => import('scenes/bootApp')),+ ])+ }))+}+// Lazy-load App so the entry chunk stays minimal: the entire transitive dependency// graph (kea, posthog-js, scene logic, UI components) is only fetched when it renders.// configureZod() is imported and called on its own before the App chunk, because zod@@ -18,19 +32,12 @@ import { ChunkLoadErrorBoundary } from './scenes/ChunkLoadErrorBoundary'// effects (posthog-js, kea) after the chunks 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.+// boot() also starts the module imports while CSS loads; runtime initialization stays behind the render gate.const App = lazy(() =>- retryBootImport(() => import('lib/configureZod'))- .then(({ configureZod }) => {- configureZod()- return Promise.all([- retryBootImport(() => import('scenes/App')),- retryBootImport(() => import('scenes/bootApp')),- ])- })- .then(([appModule, bootModule]) => {- bootModule.bootApp()- return { default: appModule.App }- })+ loadAppModules().then(([appModule, bootModule]) => {+ bootModule.bootApp()+ return { default: appModule.App }+ }))declare global {@@ -82,6 +89,8 @@ function whenBootStylesheetReady(): Promise<unknown> {}function boot(): void {+ // Observe early failures until React mounts its boundaries, without replacing the rejected promise.+ void loadAppModules().catch(() => {})void whenBootStylesheetReady().then(renderApp)}