From 3225eddf8b4be3069c4273dcdad31497bae3114c Mon Sep 17 00:00:00 2001 From: Sven Petroll Date: Wed, 21 Feb 2024 16:55:15 +0100 Subject: [PATCH 1/2] Add soft-navigation eventListener to reset CLS and INP --- src/onCLS.ts | 14 +++++++++++--- src/onINP.ts | 19 +++++++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/onCLS.ts b/src/onCLS.ts index ee369752..4e9dbf13 100644 --- a/src/onCLS.ts +++ b/src/onCLS.ts @@ -115,9 +115,7 @@ export const onCLS = (onReport: CLSReportCallback, opts?: ReportOpts) => { report(true); }); - // Only report after a bfcache restore if the `PerformanceObserver` - // successfully registered. - onBFCacheRestore(() => { + const resetCLSMetric = () => { sessionValue = 0; metric = initMetric('CLS', 0); report = bindReporter( @@ -126,10 +124,20 @@ export const onCLS = (onReport: CLSReportCallback, opts?: ReportOpts) => { CLSThresholds, opts!.reportAllChanges, ); + }; + // Only report after a bfcache restore if the `PerformanceObserver` + // successfully registered. + onBFCacheRestore(() => { + resetCLSMetric(); doubleRAF(() => report()); }); + document.addEventListener('reset-web-vital-metrics', () => { + report(true); + resetCLSMetric(); + }); + // Queue a task to report (if nothing else triggers a report first). // This allows CLS to be reported as soon as FCP fires when // `reportAllChanges` is true. diff --git a/src/onINP.ts b/src/onINP.ts index 73a46503..673a5653 100644 --- a/src/onINP.ts +++ b/src/onINP.ts @@ -240,21 +240,28 @@ export const onINP = (onReport: INPReportCallback, opts?: ReportOpts) => { report(true); }); - // Only report after a bfcache restore if the `PerformanceObserver` - // successfully registered. - onBFCacheRestore(() => { + const resetINPMetric = () => { longestInteractionList = []; - // Important, we want the count for the full page here, - // not just for the current navigation. prevInteractionCount = getInteractionCount(); - metric = initMetric('INP'); + report = bindReporter( onReport, metric, INPThresholds, opts!.reportAllChanges, ); + }; + + // Only report after a bfcache restore if the `PerformanceObserver` + // successfully registered. + onBFCacheRestore(() => { + resetINPMetric(); + }); + + document.addEventListener('reset-web-vital-metrics', () => { + report(true); + resetINPMetric(); }); } }); From 64850a324b4cad5e274c346266315665c815537c Mon Sep 17 00:00:00 2001 From: Sven Petroll Date: Fri, 23 Feb 2024 10:58:07 +0100 Subject: [PATCH 2/2] extract to lib --- src/lib/manualSoftNavigation.ts | 19 +++++++++++++++++++ src/onCLS.ts | 3 ++- src/onINP.ts | 3 ++- 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/lib/manualSoftNavigation.ts diff --git a/src/lib/manualSoftNavigation.ts b/src/lib/manualSoftNavigation.ts new file mode 100644 index 00000000..ce0b0fde --- /dev/null +++ b/src/lib/manualSoftNavigation.ts @@ -0,0 +1,19 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const onManualSoftNavigation = (cb: () => void) => { + document.addEventListener('reset-web-vital-metrics', cb); +}; diff --git a/src/onCLS.ts b/src/onCLS.ts index 4e9dbf13..bc636bb4 100644 --- a/src/onCLS.ts +++ b/src/onCLS.ts @@ -28,6 +28,7 @@ import { MetricRatingThresholds, ReportOpts, } from './types.js'; +import {onManualSoftNavigation} from './lib/manualSoftNavigation.js'; /** Thresholds for CLS. See https://web.dev/articles/cls#what_is_a_good_cls_score */ export const CLSThresholds: MetricRatingThresholds = [0.1, 0.25]; @@ -133,7 +134,7 @@ export const onCLS = (onReport: CLSReportCallback, opts?: ReportOpts) => { doubleRAF(() => report()); }); - document.addEventListener('reset-web-vital-metrics', () => { + onManualSoftNavigation(() => { report(true); resetCLSMetric(); }); diff --git a/src/onINP.ts b/src/onINP.ts index 673a5653..ad4a2c76 100644 --- a/src/onINP.ts +++ b/src/onINP.ts @@ -17,6 +17,7 @@ import {onBFCacheRestore} from './lib/bfcache.js'; import {bindReporter} from './lib/bindReporter.js'; import {initMetric} from './lib/initMetric.js'; +import {onManualSoftNavigation} from './lib/manualSoftNavigation.js'; import {observe} from './lib/observe.js'; import {onHidden} from './lib/onHidden.js'; import { @@ -259,7 +260,7 @@ export const onINP = (onReport: INPReportCallback, opts?: ReportOpts) => { resetINPMetric(); }); - document.addEventListener('reset-web-vital-metrics', () => { + onManualSoftNavigation(() => { report(true); resetINPMetric(); });