Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tunetheweb committed Apr 12, 2024
1 parent ecd4dca commit 4c6f60f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1030,18 +1030,18 @@ interface TTFBAttribution {
*/
waitingDuration: number;
/**
* The total time spent checking the HTTP cache for a match. For sites using
* service workers this time represents the total service worker time.
* The total time spent checking the HTTP cache for a match. For navigations
* using service workers this time represents the total service worker time.
*/
cacheDuration: number;
/**
* The total time to resolve the DNS for the requested domain. This cannot
* always be accurately measured for requests using service workers.
* be measured for requests using service workers and will be 0.
*/
dnsDuration: number;
/**
* The total time to create the connection to the requested domain. This
* cannot always be accurately measured for requests using service workers.
* cannot be measured for requests using service workers and will be 0.
*/
connectionDuration: number;
/**
Expand Down
14 changes: 13 additions & 1 deletion src/attribution/onTTFB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ import {
} from '../types.js';

const attributeTTFB = (metric: TTFBMetric): void => {
if (metric.entries.length) {
// Only attribute if you have a non-zero value and a navigationEntry entry
// E.g. For cross-origin redirects, Safari has a workerStart time but no
// TTFB time, so do not provide attribute for that.
if (metric.value && metric.entries.length) {
const navigationEntry = metric.entries[0];
const activationStart = navigationEntry.activationStart || 0;

// Measure from workerStart or fetchStart so any service worker startup
// time is included in cacheDuration (which also includes other sw time
// anyway, that cannot be accurately split out cross-browser).
const waitEnd = Math.max(
(navigationEntry.workerStart || navigationEntry.fetchStart) -
activationStart,
Expand All @@ -49,8 +55,14 @@ const attributeTTFB = (metric: TTFBMetric): void => {
(metric as TTFBMetricWithAttribution).attribution = {
waitingDuration: waitEnd,
cacheDuration: dnsStart - waitEnd,
// dnsEnd usually equals connectStart but use connectStart over dnsEnd
// for dnsDuration in case there ever is a gap.
dnsDuration: connectStart - dnsStart,
connectionDuration: connectEnd - connectStart,
// There is often a gap between connectEnd and requestStart. Attribute
// that to requestDuration so connectionDuration remains 0 for
// service worker controlled requests were connectStart and connectEnd
// are the same.
requestDuration: metric.value - connectEnd,
navigationEntry: navigationEntry,
};
Expand Down
8 changes: 4 additions & 4 deletions src/types/ttfb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ export interface TTFBAttribution {
*/
waitingDuration: number;
/**
* The total time spent checking the HTTP cache for a match. For sites using
* service workers this time represents the total service worker time.
* The total time spent checking the HTTP cache for a match. For navigations
* using service workers this time represents the total service worker time.
*/
cacheDuration: number;
/**
* The total time to resolve the DNS for the requested domain. This cannot
* always be accurately measured for requests using service workers.
* be measured for requests using service workers and will be 0.
*/
dnsDuration: number;
/**
* The total time to create the connection to the requested domain. This
* cannot always be accurately measured for requests using service workers.
* cannot be measured for requests using service workers and will be 0.
*/
connectionDuration: number;
/**
Expand Down

0 comments on commit 4c6f60f

Please sign in to comment.