Skip to content

Only finalize LCP on user events (isTrusted=true) - #635

Merged
tunetheweb merged 7 commits into
mainfrom
only-finalize-lcp-on-user-events
Jul 15, 2025
Merged

tunetheweb merged 7 commits into
mainfrom
only-finalize-lcp-on-user-events

Conversation

@tunetheweb

Copy link
Copy Markdown
Member

Fixes #632

Note this meant the stubVisibilityChange no longer finalized LCP in the test suite so I switched most of those to a new switchToNewTabAndBack function.

@tunetheweb
tunetheweb requested a review from philipwalton July 14, 2025 21:25

@philipwalton philipwalton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, LGTM. I just had a few minor comments.

Comment thread test/e2e/onLCP-test.js Outdated
// Wait until all images are loaded and fully rendered.
await imagesPainted();

// Pause to ensure web-vitals has a chance to collect performance metrics

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 124 already has built-in waiting for collection, so that makes me wonder if this is needed for some other reason? Is there another signal we could wait for that brings the same reliability without the arbitrariness of 100ms?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the racy part is DCL waits for the library to be loaded, but not for the PO to have fired before we navigate away (and then also the callbacks for run once and idle until urgent).

it seems to mostly work on Chrome but is flakey on Firefox.

And we can’t await the beacon before the navigation since it’s the navigation that triggers the beacon. I can change this to a H1 click instead of a nav to trigger the beacon and that does work. I was in two minds whether to do that anyway, but wanted to confirm the reason it was racy (and why mostly in this test).

Comment thread test/e2e/onLCP-test.js Resolved
Comment thread src/onLCP.ts Resolved
@tunetheweb
tunetheweb merged commit 1852851 into main Jul 15, 2025
@tunetheweb
tunetheweb deleted the only-finalize-lcp-on-user-events branch July 15, 2025 11:32
logaretm added a commit to getsentry/sentry-javascript that referenced this pull request Nov 18, 2025
Bumps the vendored-in web vitals library to include the changes between
`5.0.2` <-> `5.1.0` from upstream

#### Changes from upstream

- Remove `visibilitychange` event listeners when no longer required
[#627](GoogleChrome/web-vitals#627)
- Register visibility-change early
[#637](GoogleChrome/web-vitals#637)
- Only finalize LCP on user events (isTrusted=true)
[#635](GoogleChrome/web-vitals#635)
- Fallback to default getSelector if custom function is null or
undefined [#634](GoogleChrome/web-vitals#634)

#### Our own Changes

- Added `addPageListener` and `removePageListener` utilities because the
upstream package changed the listeners to be added on `window` instead
of `document`, so I added those utilities to avoid having to check for
window every time we try to add a listener.
martinfrancois added a commit to martinfrancois/html-cheat-sheet that referenced this pull request Oct 30, 2026
A reader who clicks before PostHog starts doesn't lose LCP. The browser
stops recording LCP at that click but keeps the entry, and web-vitals
6.2.3 picks it up once PostHog starts. web-vitals then waits for the
reader's next key press or click, or for the page being hidden, and
counts only events from the reader (GoogleChrome/web-vitals#635). The
probe in issue #24 hid the page by dispatching its own visibilitychange,
which web-vitals ignores for LCP, so no LCP came. When Firefox leaves
the page for real after an early click, LCP arrives, and the first
visit in the live PostHog project, whose Copy click came before
PostHog's page view, has LCP too.

The new test holds PostHog's scripts until the reader has clicked, then
types a search and waits for LCP. ADR 0013 now says an early click
loses no LCP, and the web vitals test says why its clicks wait for
PostHog.

Closes #24
martinfrancois added a commit to martinfrancois/html-cheat-sheet that referenced this pull request Oct 30, 2026
A reader who clicks before PostHog starts doesn't lose LCP. The browser
stops recording LCP at that click but keeps the entry, and web-vitals
6.2.3 picks it up once PostHog starts. web-vitals then waits for the
reader's next key press or click, or for the page being hidden, and
counts only events from the reader (GoogleChrome/web-vitals#635). The
probe in issue #24 hid the page by dispatching its own visibilitychange,
which web-vitals ignores for LCP, so no LCP came. When Firefox leaves
the page for real after an early click, LCP arrives, and the first
visit in the live PostHog project, whose Copy click came before
PostHog's page view, has LCP too.

The new test holds PostHog's scripts until the reader has clicked, then
types a search and waits for LCP. ADR 0013 now says an early click
loses no LCP, and the web vitals test says why its clicks wait for
PostHog.

Closes #24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

onLCP() stops after synthetic click(), while PerformanceObservers don't

2 participants