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(toolbar): cut bundle under CloudFront gzip limit (#68603)

Paul D'Ambra162f362c5170
diff --git a/.github/workflows/ci-frontend.yml b/.github/workflows/ci-frontend.yml
index b16015c78099..cb20b126da86 100644
--- a/.github/workflows/ci-frontend.yml
+++ b/.github/workflows/ci-frontend.yml
@@ -420,6 +420,10 @@ jobs:
- name: Measure PR dist size
run: du -sb frontend/dist | cut -f1 > frontend/dist-size-pr.txt
+ # Runs against the PR's own build, before the base rebuild below overwrites dist/.
+ - name: Check toolbar bundle stays under the CloudFront gzip limit
+ run: pnpm --filter=@posthog/frontend check-toolbar-size
+
- name: Build base branch for size comparison
# A broken base build (install/esbuild — which --report-only does not cover) must
# only cost the vs-base delta, never redden every open PR's job. The comment steps
diff --git a/frontend/bin/check-toolbar-size.mjs b/frontend/bin/check-toolbar-size.mjs
new file mode 100644
index 000000000000..0ae61e44ef5f
--- /dev/null
+++ b/frontend/bin/check-toolbar-size.mjs
@@ -0,0 +1,46 @@
+import fs from 'fs'
+import path from 'path'
+
+// Fail the build if dist/toolbar.js grows past the size CloudFront will still gzip.
+//
+// CloudFront only compresses responses whose body is between 1,000 and 10,000,000 bytes
+// (https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html).
+// Once the toolbar bundle crosses 10,000,000 bytes it is served uncompressed, so a bundle that
+// tips just over the line jumps from ~1.7 MB on the wire to its full ~10 MB. That cliff is
+// invisible in a size-delta comment, so guard it with a hard failure instead.
+//
+// When this fails, shrink the bundle rather than raising the limit — see the deny-list in
+// frontend/toolbar-config.mjs for the mechanism (heavy libraries the toolbar never renders can
+// be cut at resolve time, since the IIFE bundle has no code-splitting to defer them).
+const MAX_BYTES = 10_000_000
+
+function humanBytes(bytes) {
+ return `${(bytes / 1_000_000).toFixed(2)} MB (${bytes.toLocaleString()} bytes)`
+}
+
+function main() {
+ const filePath = 'dist/toolbar.js'
+ const absPath = path.resolve(process.cwd(), filePath)
+
+ let bytes
+ try {
+ bytes = fs.statSync(absPath).size
+ } catch {
+ console.error(`✗ Could not read ${filePath} — build the toolbar before running this check.`)
+ process.exit(1)
+ }
+
+ if (bytes > MAX_BYTES) {
+ console.error(
+ `✗ Toolbar bundle is ${humanBytes(bytes)}, over the ${humanBytes(MAX_BYTES)} CloudFront gzip limit.`
+ )
+ console.error(
+ ' CloudFront serves files this large uncompressed. Shrink the bundle (see frontend/toolbar-config.mjs).'
+ )
+ process.exit(1)
+ }
+
+ console.info(`✓ Toolbar bundle is ${humanBytes(bytes)}, within the ${humanBytes(MAX_BYTES)} CloudFront gzip limit.`)
+}
+
+main()
diff --git a/frontend/package.json b/frontend/package.json
index b6330b69c00f..0410e3ef0a4a 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -43,6 +43,7 @@
"format": "cd .. && oxlint --fix --fix-suggestions --fix-dangerously --quiet && oxfmt \"./{products,frontend/src,docs}/**/*.{js,mjs,ts,tsx,json,yaml,yml,css,scss}\"",
"visualize-toolbar-bundle": "pnpm exec esbuild-visualizer --metadata ./toolbar-esbuild-meta.json --filename=toolbar-esbuild-bundle-visualization.html",
"check-toolbar-csp-eval": "node ./bin/check-toolbar-csp-eval.mjs",
+ "check-toolbar-size": "node ./bin/check-toolbar-size.mjs",
"start-vite": "pnpm build:products && concurrently -n VITE,TOOLBAR -c green,magenta \"vite --host 0.0.0.0\" \"pnpm watch:toolbar\"",
"watch:toolbar": "node ./bin/build-toolbar.mjs --dev",
"mobile-replay:web:schema:build:json": "ts-json-schema-generator -f ./tsconfig.json --path 'node_modules/posthog-js/dist/rrweb-types.d.ts' --type 'eventWithTime' --expose all --no-top-ref --no-type-check --out ./src/scenes/session-recordings/mobile-replay/schema/web/rr-web-schema.json && oxfmt ./src/scenes/session-recordings/mobile-replay/schema/web/rr-web-schema.json",
diff --git a/frontend/toolbar-config.mjs b/frontend/toolbar-config.mjs
index 44c033d7f022..a21b895e6757 100644
--- a/frontend/toolbar-config.mjs
+++ b/frontend/toolbar-config.mjs
@@ -38,6 +38,25 @@ const deniedPaths = [
'scenes/session-recordings/player/snapshot-processing/DecompressionWorkerManager.ts',
]
+// Heavy third-party libraries the toolbar never renders, but which leak in transitively
+// through the shared scene graph (mostly via scenes/urls.ts -> products.tsx). Because the
+// toolbar is a single IIFE with no code-splitting, even lazily `import()`-ed libraries get
+// inlined, so these must be cut at resolve time. CloudFront won't gzip files over 10 MB, so
+// keeping the bundle small is what keeps it compressible.
+const deniedThirdPartyPackages = [
+ // mermaid diagram rendering (via LemonMarkdownWithMermaid). Denying the entry cascades to
+ // its exclusive deps: katex, cytoscape, @mermaid-js/parser, dagre-d3-es, layout/cose-base.
+ /^mermaid(\/|$)/,
+ // @posthog/brand illustration components (e.g. hoggies), pulled in by AI product scenes.
+ /^@posthog\/brand(\/|$)/,
+ // chart.js + its annotation plugin (via Sparkline). Charts in the toolbar go through the
+ // already-denied LineGraph.
+ /^chart\.js(\/|$)/,
+ /^chartjs-plugin-annotation(\/|$)/,
+ // hls.js video streaming, dynamically imported by the shared replay rrweb plugins.
+ /^hls\.js(\/|$)/,
+]
+
const deniedPatterns = [
/monaco/,
/scenes\/insights\/filters\/ActionFilter/,
@@ -49,6 +68,7 @@ const deniedPatterns = [
/scenes\/billing/,
/scenes\/data-warehouse/,
/LineGraph/,
+ ...deniedThirdPartyPackages,
]
/**