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

[soft-navigation] expose reset for cls and inp on navigation #587

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
2 changes: 1 addition & 1 deletion src/attribution/onCLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const onCLS = (
onReport: (metric: CLSMetricWithAttribution) => void,
opts?: ReportOpts,
) => {
unattributedOnCLS((metric: CLSMetric) => {
return unattributedOnCLS((metric: CLSMetric) => {
const metricWithAttribution = attributeCLS(metric);
onReport(metricWithAttribution);
}, opts);
Expand Down
2 changes: 1 addition & 1 deletion src/attribution/onINP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export const onINP = (
if (!loafObserver) {
loafObserver = observe('long-animation-frame', handleLoAFEntries);
}
unattributedOnINP((metric: INPMetric) => {
return unattributedOnINP((metric: INPMetric) => {
const metricWithAttribution = attributeINP(metric);
onReport(metricWithAttribution);
}, opts);
Expand Down
36 changes: 22 additions & 14 deletions src/onCLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,29 @@ export const onCLS = (
// Set defaults
opts = opts || {};

let metric = initMetric('CLS', 0);
let report: ReturnType<typeof bindReporter>;

let sessionValue = 0;
let sessionEntries: LayoutShift[] = [];

const reset = () => {
sessionValue = 0;
sessionEntries.length = 0;
metric = initMetric('CLS', 0);

report = bindReporter(
onReport,
metric,
CLSThresholds,
opts!.reportAllChanges,
);
};

// Start monitoring FCP so we can only report CLS if FCP is also reported.
// Note: this is done to match the current behavior of CrUX.
onFCP(
runOnce(() => {
let metric = initMetric('CLS', 0);
let report: ReturnType<typeof bindReporter>;

let sessionValue = 0;
let sessionEntries: LayoutShift[] = [];

const handleEntries = (entries: LayoutShift[]) => {
entries.forEach((entry) => {
// Only count layout shifts without recent user input.
Expand Down Expand Up @@ -116,14 +129,7 @@ export const onCLS = (
// Only report after a bfcache restore if the `PerformanceObserver`
// successfully registered.
onBFCacheRestore(() => {
sessionValue = 0;
metric = initMetric('CLS', 0);
report = bindReporter(
onReport,
metric,
CLSThresholds,
opts!.reportAllChanges,
);
reset();

doubleRAF(() => report());
});
Expand All @@ -135,4 +141,6 @@ export const onCLS = (
}
}),
);

return {reset};
};
20 changes: 17 additions & 3 deletions src/onINP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,25 @@ export const onINP = (
// Set defaults
opts = opts || {};

let metric = initMetric('INP');
let report: ReturnType<typeof bindReporter>;

const reset = () => {
resetInteractions();

metric = initMetric('INP');
report = bindReporter(
onReport,
metric,
INPThresholds,
opts!.reportAllChanges,
);
};

whenActivated(() => {
// TODO(philipwalton): remove once the polyfill is no longer needed.
initInteractionCountPolyfill();

let metric = initMetric('INP');
let report: ReturnType<typeof bindReporter>;

const handleEntries = (entries: INPMetric['entries']) => {
// Queue the `handleEntries()` callback in the next idle task.
// This is needed to increase the chances that all event entries that
Expand Down Expand Up @@ -147,4 +159,6 @@ export const onINP = (
});
}
});

return {reset};
};