@@ -240,24 +240,7 @@ export function addPerformanceEntries(transaction: Transaction): void {
240240
241241 // Measurements are only available for pageload transactions
242242 if ( op === 'pageload' ) {
243- // Generate TTFB (Time to First Byte), which measured as the time between the beginning of the transaction and the
244- // start of the response in milliseconds
245- if ( typeof responseStartTimestamp === 'number' && transactionStartTime ) {
246- DEBUG_BUILD && logger . log ( '[Measurements] Adding TTFB' ) ;
247- _measurements [ 'ttfb' ] = {
248- value : ( responseStartTimestamp - transactionStartTime ) * 1000 ,
249- unit : 'millisecond' ,
250- } ;
251-
252- if ( typeof requestStartTimestamp === 'number' && requestStartTimestamp <= responseStartTimestamp ) {
253- // Capture the time spent making the request and receiving the first byte of the response.
254- // This is the time between the start of the request and the start of the response in milliseconds.
255- _measurements [ 'ttfb.requestTime' ] = {
256- value : ( responseStartTimestamp - requestStartTimestamp ) * 1000 ,
257- unit : 'millisecond' ,
258- } ;
259- }
260- }
243+ _addTtfbToMeasurements ( _measurements , responseStartTimestamp , requestStartTimestamp , transactionStartTime ) ;
261244
262245 [ 'fcp' , 'fp' , 'lcp' ] . forEach ( name => {
263246 if ( ! _measurements [ name ] || ! transactionStartTime || timeOrigin >= transactionStartTime ) {
@@ -547,3 +530,41 @@ function setResourceEntrySizeData(
547530 data [ dataKey ] = entryVal ;
548531 }
549532}
533+
534+ /**
535+ * Add ttfb information to measurements
536+ *
537+ * Exported for tests
538+ */
539+ export function _addTtfbToMeasurements (
540+ _measurements : Measurements ,
541+ responseStartTimestamp : number | undefined ,
542+ requestStartTimestamp : number | undefined ,
543+ transactionStartTime : number | undefined ,
544+ ) : void {
545+ // Generate TTFB (Time to First Byte), which measured as the time between the beginning of the transaction and the
546+ // start of the response in milliseconds
547+ if ( typeof responseStartTimestamp === 'number' && transactionStartTime ) {
548+ DEBUG_BUILD && logger . log ( '[Measurements] Adding TTFB' ) ;
549+ _measurements [ 'ttfb' ] = {
550+ // As per https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming/responseStart,
551+ // responseStart can be 0 if the request is coming straight from the cache.
552+ // This might lead us to calculate a negative ttfb if we don't use Math.max here.
553+ //
554+ // This logic is the same as what is in the web-vitals library to calculate ttfb
555+ // https://github.com/GoogleChrome/web-vitals/blob/2301de5015e82b09925238a228a0893635854587/src/onTTFB.ts#L92
556+ // TODO(abhi): We should use the web-vitals library instead of this custom calculation.
557+ value : Math . max ( responseStartTimestamp - transactionStartTime , 0 ) * 1000 ,
558+ unit : 'millisecond' ,
559+ } ;
560+
561+ if ( typeof requestStartTimestamp === 'number' && requestStartTimestamp <= responseStartTimestamp ) {
562+ // Capture the time spent making the request and receiving the first byte of the response.
563+ // This is the time between the start of the request and the start of the response in milliseconds.
564+ _measurements [ 'ttfb.requestTime' ] = {
565+ value : ( responseStartTimestamp - requestStartTimestamp ) * 1000 ,
566+ unit : 'millisecond' ,
567+ } ;
568+ }
569+ }
570+ }
0 commit comments