fix(browser): preserve initial pageview on fast redirect or hidden-page close - #4709
Draft
posthog[bot] wants to merge 2 commits into
Draft
fix(browser): preserve initial pageview on fast redirect or hidden-page close#4709posthog[bot] wants to merge 2 commits into
posthog[bot] wants to merge 2 commits into
Conversation
…ge close The initial $pageview rides the immediate fetch path, so a fast client-side or authentication redirect can cancel it. A page that loads while hidden defers its pageview until visibilitychange, which never fires if the page closes first. In both cases the $pageleave still rides sendBeacon on unload, so Web Analytics records a pageleave with no matching pageview. Flush a pending initial $pageview over sendBeacon in _handle_unload, before the $pageleave, so it survives the same unload and keeps $pageview_id continuity. A prerendering guard prevents discarded prerenders from minting a phantom pageview. Generated-By: PostHog Desktop Task-Id: 32df7479-1f11-4431-bbce-ca472677ca3b
Contributor
Contributor
posthog-js Compliance ReportDate: 2026-08-31 19:25:01 UTC ✅ All Tests Passed!26/26 tests passed Capture Tests✅ 26/26 tests passed View Details
|
Contributor
|
Size Change: +3.42 kB (+0.02%) Total Size: 20.8 MB 📦 View Changed
ℹ️ View Unchanged
|
…pers Register `_flushInitialPageviewOnUnload` and `_sendInitialPageview` in the terser mangled-names list so the "Write mangled property names" CI check passes. Generated-By: PostHog Desktop Task-Id: 32df7479-1f11-4431-bbce-ca472677ca3b
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.
Problem
$pageleavewith no matching$pageview, and no client error is raised.$pageviewrides the immediate fetch path, while$pageleaveridessendBeaconon unload. The two survive page teardown differently:visibilitychange; that event never fires if the page closes first.sendBeaconpageleave still lands, so the pageview is lost but the pageleave is kept.Changes
$pageviewoversendBeaconon unload, so it survives the same teardown that the pageleave already survives._handle_unloadnow calls_flushInitialPageviewOnUnload()before capturing$pageleave, which preserves event order and$pageview_idcontinuity.capture_pageviewis enabled, consent is granted (or cookieless mode is on), and the initial pageview was not already sent.prerenderingguard keeps a discarded prerender from minting a phantom pageview, matching the visibility gate in_captureInitialPageview._captureInitialPageviewinto a shared_sendInitialPageview(transport?)helper so the load path (defaultfetch, unchanged) and the unload path (sendBeacon) reuse one code path and the single-fire guard.Release info Sub-libraries affected
Libraries affected
Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
Autonomy: Fully autonomous
posthog-core-also.test.tscovering the unload flush: fires oversendBeacon, orders$pageviewbefore$pageleave, and stays quiet when the pageview was already sent, whencapture_pageviewis off, or when the user is opted out.sendBeacononpagehide.pnpm typecheckand lint pass on the changed files.Created with PostHog Desktop from this inbox report.