Skip to content

Commit

Permalink
Fix FCP and LCP attributions
Browse files Browse the repository at this point in the history
  • Loading branch information
tunetheweb committed Dec 13, 2023
1 parent 7c00038 commit 2dea9a2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/attribution/onFCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ const attributeFCP = (metric: FCPMetric): void => {
let navigationEntry;
const fcpEntry = metric.entries[metric.entries.length - 1];

let activationStart = 0;
let ttfb = 0;
let softNavStart = 0;
if (!metric.navigationId || metric.navigationId === hardNavId) {
navigationEntry = getNavigationEntry();
if (navigationEntry) {
activationStart = navigationEntry.activationStart || 0;
const activationStart = navigationEntry.activationStart || 0;
ttfb = Math.max(0, navigationEntry.responseStart - activationStart);
}
} else {
navigationEntry = getSoftNavigationEntry(metric.navigationId);
// No need to set activationStart or ttfb as can use default of 0
// Set ttfb to the SoftNav start time
softNavStart = navigationEntry ? navigationEntry.startTime : 0;
ttfb = softNavStart;
}

if (navigationEntry) {
Expand Down
9 changes: 6 additions & 3 deletions src/attribution/onLCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const attributeLCP = (metric: LCPMetric) => {
let navigationEntry;
let activationStart = 0;
let responseStart = 0;
let softNavStart = 0;

if (!metric.navigationId || metric.navigationId === hardNavId) {
navigationEntry = getNavigationEntry();
Expand All @@ -45,7 +46,9 @@ const attributeLCP = (metric: LCPMetric) => {
: 0;
} else {
navigationEntry = getSoftNavigationEntry(metric.navigationId);
// No need to set activationStart or responseStart as can use default of 0
// Set activationStart to the SoftNav start time
softNavStart = navigationEntry ? navigationEntry.startTime : 0;
activationStart = softNavStart;
}

if (navigationEntry) {
Expand All @@ -67,11 +70,11 @@ const attributeLCP = (metric: LCPMetric) => {
: 0
);
const lcpResponseEnd = Math.max(
lcpRequestStart,
lcpRequestStart - softNavStart,
lcpResourceEntry ? lcpResourceEntry.responseEnd - activationStart : 0
);
const lcpRenderTime = Math.max(
lcpResponseEnd,
lcpResponseEnd - softNavStart,
lcpEntry ? lcpEntry.startTime - activationStart : 0
);

Expand Down

0 comments on commit 2dea9a2

Please sign in to comment.