Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eventListener to reset CLS & INP metrics #433

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/lib/manualSoftNavigation.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Copy link
Author

Choose a reason for hiding this comment

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

Naming probably isn't the best, I defer to your judgement ;)

document.addEventListener('reset-web-vital-metrics', cb);
};
Copy link
Author

Choose a reason for hiding this comment

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

I could also easily export something like

export const triggerManualSoftNavigation = () => {
  document.dispatchEvent(new Event('reset-web-vital-metrics'));
}

but I'm not sure how you feel about exposing API besides onXXX from this package.

15 changes: 12 additions & 3 deletions src/onCLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -115,9 +116,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(
Expand All @@ -126,10 +125,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());
});

onManualSoftNavigation(() => {
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.
Expand Down
20 changes: 14 additions & 6 deletions src/onINP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -240,21 +241,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();
});

onManualSoftNavigation(() => {
report(true);
resetINPMetric();
});
}
});
Expand Down