Repository navigation
build: download the PostHog bootstrap on each build - #21670
Conversation
Deploy preview
|
There was a problem hiding this comment.
PostHog Review · Standard · reviewed 60e0a25: 1 Must fix, 2 Should fix.
This is the only automatic review while this pull request is a draft. Comment @posthog review to review new changes sooner. Otherwise, a review of the new changes runs when you mark it ready for review.
See this review and its findings in PostHog Review. Comment @posthog review --deep for a Deep review. A Deep review takes longer and looks deeper than a Standard review.
Bundle reportTotal JS (gzip)10.53 MiB (+1.5 KiB / +0.0%) Largest changed named chunks
Eager graph (modules shipped in each entrypoint's initial chunks)
Largest modules in the
|
| Module | Size |
|---|---|
./src/data/mcp-tools.json |
1465.2 KiB |
css ./node_modules/.pnpm/css-loader@5.2.7_webpack@5.101.3/node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[8].oneOf[1].use[1]!./node_modules/.pnpm/postcss-loader@4.3.0_postcss@8.5.6_webpack@5.101.3/node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[8].oneOf[1].use[2]!./src/styles/global.css |
840.9 KiB |
./src/components/Stickers/Stickers.tsx |
696.4 KiB |
./node_modules/.pnpm/@posthog+brand@0.13.0_react@18.3.1/node_modules/@posthog/brand/dist/generated/hoggies/svg/x-ray.mjs |
481.2 KiB |
./node_modules/.pnpm/@posthog+brand@0.13.0_react@18.3.1/node_modules/@posthog/brand/dist/generated/hoggies/svg/trenchcoat.mjs |
450.8 KiB |
./node_modules/.pnpm/rehype-raw@7.0.0/node_modules/rehype-raw/lib/index.js + 29 modules |
395.1 KiB |
./node_modules/.pnpm/@posthog+brand@0.13.0_react@18.3.1/node_modules/@posthog/brand/dist/generated/hoggies/svg/im-the-driver.mjs |
385.8 KiB |
./src/hooks/useCustomers.tsx + 61 modules |
381.8 KiB |
./node_modules/.pnpm/@posthog+brand@0.13.0_react@18.3.1/node_modules/@posthog/brand/dist/generated/hoggies/svg/tourist.mjs |
364.6 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 |
354.8 KiB |
./node_modules/.pnpm/react-markdown@8.0.7_@types+react@16.14.66_react@18.3.1/node_modules/react-markdown/lib/react-markdown.js + 88 modules |
351.4 KiB |
./src/components/ProductComparisonTable/index.tsx + 130 modules |
314.3 KiB |
./node_modules/.pnpm/framer-motion@14.0.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/framer-motion/dist/es/render/components/motion/proxy.mjs + 126 modules |
287.2 KiB |
./node_modules/.pnpm/@posthog+brand@0.13.0_react@18.3.1/node_modules/@posthog/brand/dist/generated/hoggies/svg/doll-house.mjs |
284.1 KiB |
./node_modules/.pnpm/cloudinary-core@2.14.0_lodash@4.17.21/node_modules/cloudinary-core/cloudinary-core.js |
281.9 KiB |
Eager-graph budgets are report-only until a baseline is established. Sizes are gzip of public/**/*.js; eager size is webpack module source bytes for the modules actually shipped in the entrypoint's initial chunks (post-tree-shake).
dustinbyrne
left a comment
There was a problem hiding this comment.
The configuration and roadmap readiness changes check out. One availability concern remains: a stalled analytics loader now delays Gatsby startup and React-driven interactions, even though SSR content paints. Details are inline.
AI-assisted review.
ioannisj
left a comment
There was a problem hiding this comment.
🤖 An AI agent wrote this on behalf of @ioannisj.
Nothing blocking, three small maintainability items: the bootstrap URL is hard-coded in gatsby/onPreBootstrap.ts while the asset host is env-driven everywhere else, the two new test files duplicate the same TS loader helper, and one test still stubs an import the hook dropped. The download, retry, and script-ordering paths read correct to me.
| else Reflect.deleteProperty(globalThis, key) | ||
| } | ||
| }) | ||
| const load = (file, dependencies) => { |
There was a problem hiding this comment.
🤖 An AI agent wrote this on behalf of @ioannisj.
[suggestion] Small one: both new test files carry the same load helper (read the file, ts.transpileModule to CommonJS, runInNewContext with a stubbed require), and they already drift a bit, this one asserts on unexpected deps while the loader test falls through to the real require. Worth a shared scripts/test-utils/load-ts-module.cjs with the strict-deps check as an option, so the next hook test doesn't add a third copy? Or better as a follow-up?
…cript-loader # Conflicts: # .github/workflows/deploy-preview.yml

Executive summary
Download the published PostHog bootstrap during every Gatsby build, then serve it locally before Gatsby starts. Fetch the current major-1 release instead of committing a vendored copy. Keep the existing SDK initialization, callbacks, and asynchronous full-SDK download.
PostHog/posthog-js#5470 is merged and released. The required
loader-bootstrap.jsartifact is available from the release CDN. The previous release gate and temporary vendor provenance no longer apply.🗺️ PR tour
1. Fetch the bootstrap before publishing (build diff)
gatsby/onPreBootstrap.tsdownloadshttps://us-assets.i.posthog.com/static/1/loader-bootstrap.jswhenever analytics is configured.POSTHOG_BOOTSTRAP_URLnames the region-independent release artifact. Runtime regions and proxies remain separate, selected bydata-asset-host. The test keeps an independent expected URL. An uncachedRequestalso bypasses the existing URL-only development cache. Each attempt has a 30-second timeout. HTTP 408/429/5xx, network failures, and interrupted body reads get up to three attempts, with 500 ms and 1 s delays. Other HTTP errors and empty content fail immediately. Exhausted retries stop the build instead of publishing stale code.posthog.com/gatsby/onPreBootstrap.ts
Lines 86 to 113 in d5c7b4f
The hook removes only the trailing source-map comment and writes
static/scripts/posthog-bootstrap.jsbeside the existing generated init script..gitignoreexcludes this generated file. The vendored file and its provenance record are removed from the PR. The embedded handwritten bootstrap is also removed from the generated initialization code. All initialization settings remain intact.2. Keep application startup independent of the analytics host (diff)
src/html.tsxloads the local bootstrap and init script after server-rendered content, before Gatsby's async bundles. The local bootstrap creates the queue synchronously and downloads the full SDK asynchronously.data-asset-hostkeeps the runtime host configuration, whiledata-sdk-version="1"preserves same-major SDK updates.posthog.com/src/html.tsx
Lines 44 to 61 in d5c7b4f
3. Wait for the current initialized SDK (diff)
useEarlyAccessFeaturesreads the currentwindow.posthogon each attempt and checks__loaded. It does not retain a discarded queue for survey callbacks. Polling and the six-second fallback remain unchanged.posthog.com/src/hooks/useEarlyAccessFeatures.ts
Lines 132 to 139 in d5c7b4f
4. Test generation, startup, and fallback behavior (loader tests)
scripts/posthog-loader.test.cjshas 20 cases. It checks fresh downloads across repeated builds, transient recovery, bounded retry exhaustion, permanent failures, disabled analytics, HTML ordering, initialization settings, and application startup. The browser case now obtains the real published bootstrap through the build hook instead of reading a vendored file. It holds the remote SDK response and confirms that an async app probe remains usable and queues its capture.posthog.com/scripts/posthog-loader.test.cjs
Lines 102 to 188 in d5c7b4f
scripts/early-access-features.test.cjsretains four SDK-readiness and fallback cases. Its dependency map contains only the hook's actual imports, without the obsoleteusePostHogsetup.package.jsonaddspnpm test:posthog-loader. The preview workflow runs all 24 cases before building the site.The retained startup test uses an inert core response. It proves startup and queuing, not full SDK replay. Separate real-SDK checks are recorded below.
🔍 Reviewer's guide
Testing done
curl -fsSon US/static/1/loader-bootstrap.js, US/static/loader-bootstrap.js, and EU/static/1/loader-bootstrap.js.pnpm test:posthog-loaderbefore edits.pnpm exec node --test --test-name-pattern='a build retries|a build fails' scripts/posthog-loader.test.cjs7541920846; recoverable errors aborted immediately, and exhaustion made only one attempt. The 404 and empty-content controls passed. After: all ten pass.PUPPETEER_EXECUTABLE_PATH="/Applications/Brave Browser.app/Contents/MacOS/Brave Browser" pnpm test:posthog-loaderpnpm test:gatsby-functions,pnpm test:pocket-guides,pnpm test:twig-integration7541920846)pnpm start; compared its generated bootstrap with the released artifact after source-map removal.7541920846)/,/docs, and/roadmap, with SDK 1.443.0 held during hydration. Compared the downloaded bootstrap with the previous vendor through request interception.pnpm exec prettier --write/--check;pnpm exec node --check scripts/posthog-loader.test.cjs;git diff --check.autoreview --mode branch --base origin/master --prompt-file .cache/posthog-bootstrap-maintenance-review-notes.md.d5c7b4f0c33bb54c25512851bbb708348c1a49bd.masterwithout rewriting history. Queried GitHub commit verification.d5c7b4f0c33bb54c25512851bbb708348c1a49bd.d5c7b4f0c3)curlon the generated script, then Brave on/,/docs, and/roadmapat the preview. Compared the previous vendor and downloaded bootstrap in fresh isolated contexts.The full-application check used the actual generated file served by the restarted dev server. Only the previous-vendor control response was intercepted. Its ingestion endpoint used a dummy token and a local mock, not production ingestion. The retained browser test and the full-app checks use separate temporary Brave profiles, not working browser tabs.
Preview POST requests were fulfilled locally to avoid sending production telemetry. An initial preview check also saw hydration errors on the home page. The isolated comparison did not reproduce them there, and reproduced identical roadmap errors with both bootstrap versions. These warnings are not reported as fixed by this PR.
Not tested locally: a production build with production credentials and every shared-shell route. The CI production build and deployed preview for the static cleanup passed their checks above. No page layout, styling, or content changed in this PR. No pages moved.
What to look at
data-asset-host.Changes
Replace the embedded bootstrap with the SDK-maintained local bootstrap, fetched afresh during each build. Remove the temporary vendor and provenance record. Preserve initialization, application startup, and roadmap fallback behavior.
Checklist