Repository navigation
Only finalize LCP on user events (isTrusted=true) - #635
Conversation
philipwalton
left a comment
There was a problem hiding this comment.
Overall, LGTM. I just had a few minor comments.
| // Wait until all images are loaded and fully rendered. | ||
| await imagesPainted(); | ||
|
|
||
| // Pause to ensure web-vitals has a chance to collect performance metrics |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
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.
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
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
Fixes #632
Note this meant the stubVisibilityChange no longer finalized LCP in the test suite so I switched most of those to a new
switchToNewTabAndBackfunction.