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

Cap TTFB in attribution #440

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/attribution/onFCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,21 @@ const attributeFCP = (metric: FCPMetric): void => {
const navigationEntry = getNavigationEntry();
const fcpEntry = metric.entries[metric.entries.length - 1];

// Should always have an fcpEntry to be able to attribute
if (!fcpEntry) return;
tunetheweb marked this conversation as resolved.
Show resolved Hide resolved

if (navigationEntry) {
const activationStart = navigationEntry.activationStart || 0;
const ttfb = Math.max(0, navigationEntry.responseStart - activationStart);
// Cap at 0 and ignore negative responseStart, future responseStart,
// or responseStart after LCP
const ttfb = Math.max(
0,
navigationEntry.responseStart <= 0 ||
navigationEntry.responseStart > performance.now() ||
navigationEntry.responseStart - activationStart > fcpEntry.startTime
? 0
: navigationEntry.responseStart - activationStart,
);

(metric as FCPMetricWithAttribution).attribution = {
timeToFirstByte: ttfb,
Expand Down
17 changes: 15 additions & 2 deletions src/attribution/onLCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,26 @@ const attributeLCP = (metric: LCPMetric) => {
if (navigationEntry) {
const activationStart = navigationEntry.activationStart || 0;
const lcpEntry = metric.entries[metric.entries.length - 1];

// Should always have an lcpEntry to be able to attribute
if (!lcpEntry) return;
tunetheweb marked this conversation as resolved.
Show resolved Hide resolved

const lcpResourceEntry =
lcpEntry.url &&
performance
.getEntriesByType('resource')
.filter((e) => e.name === lcpEntry.url)[0];

const ttfb = Math.max(0, navigationEntry.responseStart - activationStart);
// Cap at 0 and ignore negative responseStart, future responseStart,
// or responseStart after LCP
const ttfb = Math.max(
0,
navigationEntry.responseStart <= 0 ||
navigationEntry.responseStart > performance.now() ||
navigationEntry.responseStart - activationStart > lcpEntry.startTime
? 0
: navigationEntry.responseStart - activationStart,
);

const lcpRequestStart = Math.max(
ttfb,
Expand All @@ -55,7 +68,7 @@ const attributeLCP = (metric: LCPMetric) => {
);
const lcpRenderTime = Math.max(
lcpResponseEnd,
lcpEntry ? lcpEntry.startTime - activationStart : 0,
lcpEntry.startTime - activationStart,
);

const attribution: LCPAttribution = {
Expand Down