Repository navigation
fix(frontend): load monaco lazily via a CodeEditor facade - #62957
Conversation
monaco (~4 MB of JS) was statically reachable from the authed shell and from every scene that renders queries, via three chains: GlobalModals -> PreviewingCustomCssModal -> CodeEditor, Query -> HogDebug -> CodeEditor, and the activity describer registry -> hog function activityDescriptions -> monacoEnvironment. CodeEditor.tsx becomes a React.lazy facade over CodeEditorImpl, so all fourteen import sites defer monaco until an editor renders. The initModel/clearLogicReference helpers move to modelLogicReference.ts so the facade can re-export them without a static edge to the impl. The hog function activity Diff component gets the same treatment, and two scene logics switch to type-only monaco imports. After this, monaco core is statically unreachable from AuthenticatedShell, Query, dashboards, and saved insights; it ships as self-contained lazy chunks fetched on first editor render. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Reviews (1): Last reviewed commit: "fix(frontend): load monaco lazily via a ..." | Re-trigger Greptile |
|
Size Change: +54.3 kB (+0.08%) Total Size: 72.5 MB π¦ View Changed
βΉοΈ View Unchanged
|
CodeEditor lazy-loads monaco, so the hogql-query-editor container is visible before the editor mounts; clicking too early focuses nothing and the inserted query is silently lost. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pauldambra
left a comment
There was a problem hiding this comment.
Note
π€ Automated comment by QA Swarm β not written by a human
QA Swarm review complete. See inline comments.
|
Note π€ Automated comment by QA Swarm β not written by a human Multi-perspective review: qa-team (specialists + generalists), paul-reviewer, xp-reviewer, security-audit Verdict: π¬ APPROVE WITH NITSClean, faithful facade refactor β the 565-line implementation move is verbatim (two reviewers independently diffed it), the leak-hardening teardown logic is untouched, and the Key findingsπ‘ MEDIUM
π’ LOW
βͺ NIT
Convergence
Reviewer summaries
Automated by QA Swarm β not a human review |
Wrap the Suspense fallbacks in sized containers so the editor slot doesn't collapse to spinner size and reflow on chunk load. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
The PR is a substantial cross-team refactor touching @PostHog/team-data-stack and @PostHog/team-workflows code, with no human reviews from either owning team on the current head β only automated bot comments. Get a human review from one of the owning teams before re-requesting.
β¦contract no-restricted-imports now rejects value imports of monaco-editor and any direct import of CodeEditorImpl, so a future static import can't silently re-couple ~4 MB into a hot chunk; the modules that intentionally own monaco (lib/monaco, MonacoDiffEditor, the SQL editor logic, hog function test UI) keep the base restrictions via an override. CodeEditor's container now exposes data-editor-ready once monaco has mounted, and the playwright waits target that contract instead of monaco's internal .monaco-editor class name. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
This is a substantial cross-team refactor touching @PostHog/team-data-stack and @PostHog/team-workflows code, and no human review from either owning team exists on the current head β only automated bot comments. Get a human review from a member of one of the owning teams before re-requesting.
The diff-mode branch rendered MonacoDiffEditor bare, so consumers waiting on the editor-ready contract would time out whenever originalValue put the editor into diff mode (e.g. the SQL editor's draft/view comparison pane). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
This is a substantial cross-team refactor (1310 lines, two owning teams) with no human reviews from @PostHog/team-data-stack or @PostHog/team-workflows on the current head β only automated bot comments. Get a human review from a member of one of those teams before re-requesting.
|
Note π€ Automated comment by PR Shepherd β driven by @pauldambra Why the bundle size check shows no reduction (+0.08%): that's expected, for two reasons.
The intended effect is measured in the PR description instead: monaco core is statically unreachable from |
πΈοΈ Eager graphHow much code each root forces the browser to download and decode through static imports β the regression class total bundle size can't see.
β
Largest files eagerly reachable from
|
| Size | File |
|---|---|
| 878.2 KiB | src/styles/global.scss |
| 609.0 KiB | public/hedgehog/burning-money-hog.png |
| 541.9 KiB | public/hedgehog/waving-hog.png |
| 448.2 KiB | public/hedgehog/stop-sign-hog.png |
| 362.0 KiB | public/hedgehog/phone-pair-hogs.png |
| 354.8 KiB | ../node_modules/.pnpm/@posthog+icons@0.36.6_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@posthog/icons/dist/posthog-icons.es.js |
| 335.6 KiB | public/hedgehog/desk-hog.png |
| 323.2 KiB | public/hedgehog/3-bears-hogs.png |
| 297.4 KiB | src/taxonomy/core-filter-definitions-by-group.json |
| 286.4 KiB | src/lib/api.ts |
Largest files eagerly reachable from src/scenes/AuthenticatedShell.tsx
| Size | File |
|---|---|
| 878.2 KiB | src/styles/global.scss |
| 760.0 KiB | src/queries/validators.js |
| 609.0 KiB | public/hedgehog/burning-money-hog.png |
| 541.9 KiB | public/hedgehog/waving-hog.png |
| 448.2 KiB | public/hedgehog/stop-sign-hog.png |
| 398.7 KiB | ../node_modules/.pnpm/chart.js@4.5.1/node_modules/chart.js/dist/chart.js |
| 362.0 KiB | public/hedgehog/phone-pair-hogs.png |
| 354.8 KiB | ../node_modules/.pnpm/@posthog+icons@0.36.6_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@posthog/icons/dist/posthog-icons.es.js |
| 335.6 KiB | public/hedgehog/desk-hog.png |
| 323.2 KiB | public/hedgehog/3-bears-hogs.png |
Posted automatically by check-eager-graph Β· sizes are input-source bytes from the esbuild metafile Β· part of #32479
|
Note π€ Automated comment by PR Shepherd β driven by @pauldambra The eager graph check (merged in #63099) just posted its first measurement on this branch: the authenticated-shell eager closure is 45.79 MiB / 7,197 files, versus 55.51 MiB / 8,325 files on the master it was calibrated against β a 9.72 MiB (17.5%) reduction in the JavaScript every logged-in page must download and decode, from making monaco lazy. Once this merges, the shell budget gets ratcheted down and |
|
the one thing that I could think of as an improvement: page loads, no monaco... we then slyly load it in the background so that when you open any scene that needs this editor, it's already there |
|
there are a few other similar changes on the go... i'll point a robot at that when it has more examples π |
fable recorded a video for me of this running locally... i think it got bored in the middle cos there's a big gap with no action though
pr-62957-demo.mp4
Problem
monaco-editor (~4 MB of JS) was statically reachable from the authed shell and from every scene that renders queries β so it shipped in hot shared chunks and was decoded by every renderer, even though most sessions never open a code editor. Three chains anchored it (traced via the esbuild metafile):
AuthenticatedShell->GlobalModals->PreviewingCustomCssModal->CodeEditorQuery->HogDebug->CodeEditor(the universal query renderer, so dashboards, insights, and saved insights all carried it)activityLogLogic) -> hog functionactivityDescriptions->monacoEnvironmentPart of the decoded-script / renderer-memory reduction work in the bundle-split investigation.
References #32479
Changes
lib/monaco/CodeEditor.tsxis now a thinReact.lazyfacade over the implementation (moved unchanged toCodeEditorImpl.tsx). All fourteen import sites keep their imports and get laziness for free; monaco is fetched on first editor render, behind the same<Spinner />the editor already showed while monaco bootstraps.initModel/clearLogicReferencemove tolib/monaco/modelLogicReference.ts(type-only monaco imports), so the facade can re-export them without re-anchoring the impl.sqlEditorLogic's import path is unchanged.Diffcomponent moves to its own file behind the same lazy pattern β the describer registry is imported app-wide, and it was pullingmonacoEnvironment+ the hog language into every chunk containingActivityLog.dataWarehouseSettingsSceneLogicandhogFunctionTestLogicswitch toimport typefor monaco β they only use monaco types, but the value imports created real runtime edges in the metafile.Out of scope (left for a follow-up batch): three scene-local static imports of
MonacoDiffEditor(SQL editor history modal, audit logs, approvals) βActivityLogand the product scenes already lazy-load it.Measured on the production build's metafile after this change: monaco core is statically unreachable from
AuthenticatedShell,Query,Dashboard, andSavedInsights; monaco ships as self-contained lazy chunks (4.37 MB total, dominated by a 2.5 MB + 1.1 MB pair) fetched only when an editor actually renders.How did you test this code?
I'm an agent; automated verification only:
pnpm --filter=@posthog/frontend typescript:checkpasseshogli testonactivityLogLogic.test.tsx(10/10),hogFunctionConfigurationLogic.test.ts(3/3),DataWarehouseSourceScene.test.ts(4/4),timeSensitiveAuthenticationLogic.test.ts(9/9)Automatic notifications
Docs update
π€ Agent context
Autonomy: Human-driven (agent-assisted)
Claude Code session driven by @pauldambra β PR 2 of the bundle-split plan from the #32479 memory investigation (PR 1: #62923, removes the fake-lazy GlobalModals require; the two are independent and compose). Considered lazying each of the three chains at their call sites instead, but a facade at
CodeEditor.tsxcuts all component chains at once and protects future import sites; the only non-component consumers needed the small helper-module extraction.π€ Generated with Claude Code