Skip to content

Commit

Permalink
Fix attribution for TTFB and LCP
Browse files Browse the repository at this point in the history
  • Loading branch information
tunetheweb committed Dec 13, 2023
1 parent 5de5e89 commit 7c00038
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web-vitals",
"version": "3.5.0-soft-navs-10",
"version": "3.5.0-soft-navs-11",
"description": "Easily measure performance metrics in JavaScript",
"type": "module",
"typings": "dist/modules/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/attribution/onLCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const attributeLCP = (metric: LCPMetric) => {
? navigationEntry.responseStart
: 0;
} else {
navigationEntry = getSoftNavigationEntry();
navigationEntry = getSoftNavigationEntry(metric.navigationId);
// No need to set activationStart or responseStart as can use default of 0
}

Expand Down
11 changes: 7 additions & 4 deletions src/attribution/onTTFB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ import {

const attributeTTFB = (metric: TTFBMetric): void => {
if (metric.entries.length) {
const navigationEntry = metric.entries[0];
// Is there a better way to check if this is a soft nav entry or not?
// Refuses to build without this as soft navs don't have activationStart
const navigationEntry = <PerformanceNavigationTiming>metric.entries[0];

const activationStart = navigationEntry.activationStart || 0;

const dnsStart = Math.max(
navigationEntry.domainLookupStart - activationStart,
navigationEntry.domainLookupStart - activationStart || 0,
0
);
const connectStart = Math.max(
navigationEntry.connectStart - activationStart,
navigationEntry.connectStart - activationStart || 0,
0
);
const requestStart = Math.max(
navigationEntry.requestStart - activationStart,
navigationEntry.requestStart - activationStart || 0,
0
);

Expand Down
1 change: 1 addition & 0 deletions src/onTTFB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export const onTTFB = (onReport: TTFBReportCallback, opts?: ReportOpts) => {
'soft-navigation',
entry.navigationId
);
metric.entries = [entry];
report = bindReporter(
onReport,
metric,
Expand Down
10 changes: 8 additions & 2 deletions src/types/ttfb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import {NavigationTimingPolyfillEntry} from './polyfills.js';
*/
export interface TTFBMetric extends Metric {
name: 'TTFB';
entries: PerformanceNavigationTiming[] | NavigationTimingPolyfillEntry[];
entries:
| PerformanceNavigationTiming[]
| NavigationTimingPolyfillEntry[]
| SoftNavigationEntry[];
}

/**
Expand Down Expand Up @@ -56,7 +59,10 @@ export interface TTFBAttribution {
* general page load issues. This can be used to access `serverTiming` for example:
* navigationEntry.serverTiming
*/
navigationEntry?: PerformanceNavigationTiming | NavigationTimingPolyfillEntry;
navigationEntry?:
| PerformanceNavigationTiming
| NavigationTimingPolyfillEntry
| SoftNavigationEntry;
}

/**
Expand Down

0 comments on commit 7c00038

Please sign in to comment.