fix(browser): default external_scripts_inject_target to 'head'#3465
Draft
posthog[bot] wants to merge 1 commit intomainfrom
Draft
fix(browser): default external_scripts_inject_target to 'head'#3465posthog[bot] wants to merge 1 commit intomainfrom
posthog[bot] wants to merge 1 commit intomainfrom
Conversation
Inject lazy-loaded scripts (recorder, surveys, exception-autocapture, etc.) into document.head by default instead of document.body. Body injection mutates body DOM before React finishes hydrating, shifting SSR-rendered body scripts (e.g. JSON-LD) out of position and triggering hydration mismatches. The opt-in previously gated behind defaults >= 2026-01-30 now applies for everyone; users who need the legacy behavior can still set external_scripts_inject_target: body explicitly. Generated-By: PostHog Code Task-Id: 4738b68a-f485-4795-bd88-be9ae23e8d02
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Contributor
|
Size Change: -338 B (0%) Total Size: 6.94 MB
ℹ️ View Unchanged
|
Contributor
|
This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
external_scripts_inject_targetto'head'unconditionally so SSR apps (e.g. Next.js pages rendering body-level<script type="application/ld+json">) do not see React hydration mismatches caused by PostHog injecting lazy-loaded scripts into<body>before hydration finishes.defaults: '2026-01-30'(packages/browser/src/posthog-core.ts:193). Everyone on olderdefaultsremained exposed.packages/browser/src/entrypoints/external-scripts-loader.tsis kept behind explicit opt-in viaexternal_scripts_inject_target: 'body'for users who want the old behavior.ConfigDefaultsdoc comment so'2026-01-30'reflects only what it still gates (internal_or_test_user_hostname).Context
A new error-tracking issue (fingerprint
07e8b7fc…, first seen 2026-04-23) surfacedError: Hydration failed because the server rendered text didn't match the clientwith the React diff showing the server-rendered<script type="application/ld+json">shifted out of position by an injectedstatic/exception-autocapture.jstag. Root cause is thebodyinjection path:scripts[0].parentNode?.insertBefore(scriptTag, scripts[0])moves body DOM before React finishes hydrating.Commit
f8c17bc(PR #2802) introduced the safer'head'target but gated the default ondefaults: '2026-01-30', so the fix never reached users on olderdefaults.Test plan
pnpm test:unitinpackages/browser— all 3910 relevant unit tests pass (one unrelated failure requiresdist/array.js, not produced by these tests).pnpm test:unitinpackages/types— config snapshot + interface tests pass.pnpm typecheckandpnpm lintinpackages/browser— both clean.default inject target leaves body JSON-LD untouched without explicit configinpackages/browser/playwright/mocked/hydration-error.spec.ts, proving the default now preserves SSR-rendered body scripts (framework bundle + JSON-LD) in their original positions with zero hydration errors.packages/browser/playground/hydration/index.htmlto include a realistic<script type="application/ld+json">and removed the explicitexternal_scripts_inject_target: 'head'override so the playground exercises the default.Created with PostHog Code