Skip to content

Commit 2c876e0

Browse files
Adam RaineDevtools-frontend LUCI CQ
authored andcommitted
[RPP Observations] Add warning if page was hidden during load
Bug: 364902432 Change-Id: Iaffc9eedecd802664381160dbce0e1395984e3c4 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6239030 Reviewed-by: Connor Clark <[email protected]> Commit-Queue: Adam Raine <[email protected]>
1 parent c7db943 commit 2c876e0

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

front_end/models/live-metrics/LiveMetrics.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ const UIStrings = {
1818
*/
1919
lcpEmulationWarning:
2020
'Simulating a new device after the page loads can affect LCP. Reload the page after simulating a new device for accurate LCP data.',
21+
/**
22+
* @description Warning text indicating that the Largest Contentful Paint (LCP) performance metric was affected by the page loading in the background.
23+
*/
24+
lcpVisibilityWarning: 'LCP value may be inflated because the page started loading in the background.',
2125
} as const;
2226

2327
const str_ = i18n.i18n.registerUIStrings('models/live-metrics/LiveMetrics.ts', UIStrings);
@@ -277,6 +281,10 @@ export class LiveMetrics extends Common.ObjectWrapper.ObjectWrapper<EventTypes>
277281
warnings.push(i18nString(UIStrings.lcpEmulationWarning));
278282
}
279283

284+
if (webVitalsEvent.startedHidden) {
285+
warnings.push(i18nString(UIStrings.lcpVisibilityWarning));
286+
}
287+
280288
this.#lcpValue = lcpEvent;
281289
break;
282290
}

front_end/models/live-metrics/web-vitals-injected/spec/spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface InpPhases {
3535
export interface LcpChangeEvent extends MetricChangeEvent {
3636
name: 'LCP';
3737
phases: LcpPhases;
38+
startedHidden: boolean;
3839
nodeIndex?: number;
3940
}
4041

front_end/models/live-metrics/web-vitals-injected/web-vitals-injected.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,44 @@ function limitScripts(loafs: Spec.PerformanceLongAnimationFrameTimingJSON[]):
121121
});
122122
}
123123

124+
function isPrerendered(): boolean {
125+
if (document.prerendering) {
126+
return true;
127+
}
128+
129+
const firstNavStart = self.performance.getEntriesByType?.('navigation')[0]?.activationStart;
130+
return firstNavStart !== undefined && firstNavStart > 0;
131+
}
132+
133+
let startedHidden: boolean|null = null;
134+
124135
function initialize(): void {
125136
sendEventToDevTools({name: 'reset'});
126137

138+
new PerformanceObserver(list => {
139+
for (const entry of list.getEntries()) {
140+
if (startedHidden === null && !isPrerendered()) {
141+
startedHidden = entry.name === 'hidden';
142+
}
143+
}
144+
}).observe({type: 'visibility-state', buffered: true});
145+
127146
// We want to treat bfcache navigations like a standard navigations, so emit
128147
// a reset event when bfcache is restored.
129148
//
130149
// Metric functions will also re-emit their values using this listener's callback.
131150
// To ensure this event is fired before those values are emitted, register this
132151
// callback before any others.
133152
WebVitals.onBFCacheRestore(() => {
153+
startedHidden = false;
134154
sendEventToDevTools({name: 'reset'});
135155
});
136156

137157
onLCP(metric => {
138158
const event: Spec.LcpChangeEvent = {
139159
name: 'LCP',
140160
value: metric.value,
161+
startedHidden: Boolean(startedHidden),
141162
phases: {
142163
timeToFirstByte: metric.attribution.timeToFirstByte,
143164
resourceLoadDelay: metric.attribution.resourceLoadDelay,

0 commit comments

Comments
 (0)