: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
chore(frontend): turn zod's JIT parser compiler off for everyone (#95824)
Tom Piccirello4b955247e0f5
diff --git a/frontend/src/exporter/index.tsx b/frontend/src/exporter/index.tsxindex 2e5759c44577..5d1eee87d7c2 100644--- a/frontend/src/exporter/index.tsx+++ b/frontend/src/exporter/index.tsx@@ -1,6 +1,10 @@import '~/styles'import './Exporter.scss'+// The relative path keeps this with the side-effect imports when imports are sorted, so it evaluates+// before any module that builds a zod schema. See lib/configureZod.+import '../lib/configureZod'+import { polyfillCountryFlagEmojis } from 'country-flag-emoji-polyfill'import { BeforeSendFn, CapturedNetworkRequest } from 'posthog-js'import { createRoot } from 'react-dom/client'diff --git a/frontend/src/index.test.tsx b/frontend/src/index.test.tsxindex 087ce6d12d1b..ef23f6833a0d 100644--- a/frontend/src/index.test.tsx+++ b/frontend/src/index.test.tsx@@ -35,11 +35,10 @@ describe('app entry boot', () => {documentListeners = jest.spyOn(document, 'addEventListener')jest.doMock('react', () => React)jest.doMock('react-dom/client', () => ({ createRoot }))- jest.doMock('lib/configureZod', () => ({- configureZod: () => {- configured = true- },- }))+ jest.doMock('lib/configureZod', () => {+ configured = true+ return {}+ })jest.doMock('scenes/App', () => {if (!configured) {throw new Error('App evaluated before Zod configuration')@@ -149,11 +148,9 @@ describe('app entry boot', () => {jest.spyOn(console, 'error').mockImplementation(() => {})const error = new Error('Boot dependency failed')if (failureStage === 'configuration') {- jest.doMock('lib/configureZod', () => ({- configureZod: () => {- throw error- },- }))+ jest.doMock('lib/configureZod', () => {+ throw error+ })} else {jest.doMock('scenes/App', () => {throw errordiff --git a/frontend/src/index.tsx b/frontend/src/index.tsxindex a08dd42dc954..dcfc0ba5f6c2 100644--- a/frontend/src/index.tsx+++ b/frontend/src/index.tsx@@ -15,20 +15,16 @@ type AppModules = [typeof import('scenes/App'), typeof import('scenes/bootApp')]let appModulesPromise: Promise<AppModules> | undefinedfunction loadAppModules(): Promise<AppModules> {- return (appModulesPromise ??= retryBootImport(() => import('lib/configureZod')).then(({ configureZod }) => {- configureZod()- return Promise.all([- retryBootImport(() => import('scenes/App')),- retryBootImport(() => import('scenes/bootApp')),- ])- }))+ return (appModulesPromise ??= retryBootImport(() => import('lib/configureZod')).then(() =>+ 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-// binds its jitless setting when it constructs each object schema and the App graph-// constructs some at module scope. bootApp() runs the remaining one-time boot side+// lib/configureZod is imported on its own before the App chunk, because zod binds its+// jitless setting when it constructs each object schema and the App graph constructs+// some at module scope. bootApp() runs the remaining one-time boot side// 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.diff --git a/frontend/src/lib/configureZod.ts b/frontend/src/lib/configureZod.tsindex 98bd83c57672..f97c4d6badd3 100644--- a/frontend/src/lib/configureZod.ts+++ b/frontend/src/lib/configureZod.ts@@ -1,25 +1,20 @@import { z } from 'zod'-import { FEATURE_FLAGS } from 'lib/constants'-/**- * Turn zod's JIT parser compiler off for a share of users so we can measure what it costs us.- *- * zod compiles a parser per schema with `new Function`, which needs `'unsafe-eval'` in `script-src`- * and is about half of all CSP violations the app reports. The compiler pays for itself only when- * one schema is parsed many times, and most of ours are parsed a handful of times per page load, so- * turning it off may well be free or better. Measure rather than assume.+ * Turn zod's JIT parser compiler off.*- * The flag is read from the bootstrap Django renders into the page, not from `featureFlagLogic`.- * zod binds `jitless` when it constructs each object schema, so this must run before any module- * that builds a schema at module scope evaluates. src/index.tsx imports and calls it before it- * imports the App chunk. A flag arriving later would leave a mix of compiled and interpreted- * schemas and a measurement that means nothing.+ * zod compiles a parser per schema with `new Function`, which needs `'unsafe-eval'` in `script-src`.+ * That was about half of every CSP violation the app reported, and `script-src` cannot be enforced+ * while we need it. A 48-hour measurement at 50% of traffic found jitless performance-neutral: INP+ * p75 was identical at the three largest samples, LCP was equal or better, and eval violations fell+ * from 33 to 14-20 per 1000 pageviews.*- * Django renders bootstrap flags only for authenticated users, so anonymous pages such as login- * and signup keep the compiler whatever the flag says.+ * zod binds `jitless` when it constructs each object schema, so this module must evaluate before any+ * module that builds a schema at module scope. Each esbuild entry point bundles its own copy of zod+ * with its own config, so every browser entry point imports this module itself:+ * - src/index.tsx imports it on its own before it imports the App chunk.+ * - src/exporter/index.tsx and src/render-query/index.tsx import it for its side effect ahead of their+ * other imports. ESM evaluates imports in declaration order, so it runs before the modules that build+ * schemas.*/-export function configureZod(): void {- const bootstrapFlags = window.POSTHOG_USER_IDENTITY_WITH_FLAGS?.featureFlags- z.config({ jitless: !!bootstrapFlags?.[FEATURE_FLAGS.ZOD_JITLESS] })-}+z.config({ jitless: true })diff --git a/frontend/src/lib/constants.tsx b/frontend/src/lib/constants.tsxindex 48b2d77e1626..fc40a5dc5019 100644--- a/frontend/src/lib/constants.tsx+++ b/frontend/src/lib/constants.tsx@@ -247,7 +247,6 @@ export const FEATURE_FLAGS = {WEB_ANALYTICS_FOR_MOBILE: 'web-analytics-for-mobile', // owner: #team-web-analyticsWEB_ANALYTICS_PATH_CLEANING_SUGGESTIONS: 'web-analytics-path-cleaning-suggestions', // owner: @lricoy #team-web-analyticsWEB_ANALYTICS_REFERRER_URL_DRILLDOWN: 'web-analytics-referrer-url-drilldown', // owner: @jabahamondes #team-web-analytics- ZOD_JITLESS: 'zod-jitless', // owner: @Piccirello measuring the cost of zod's JIT parser compiler// Temporary feature flags, still WIP, should be removed eventuallyACTION_REFERENCE_COUNT: 'action-reference-count', // owner: @andyzzhao #team-product-analytics, gates bulk action reference counting on actions listdiff --git a/frontend/src/render-query/index.tsx b/frontend/src/render-query/index.tsxindex 64e9b9b95e6d..ac3dc48c5e85 100644--- a/frontend/src/render-query/index.tsx+++ b/frontend/src/render-query/index.tsx@@ -1,6 +1,10 @@import '~/styles'import './RenderQuery.scss'+// The relative path keeps this with the side-effect imports when imports are sorted, so it evaluates+// before any module that builds a zod schema. See lib/configureZod.+import '../lib/configureZod'+import { createRoot } from 'react-dom/client'import { initKea } from '~/initKea'