17
17
import { getGlobalObject } from '@sentry/utils' ;
18
18
19
19
import { initMetric } from './lib/initMetric' ;
20
- import { ReportHandler } from './types' ;
20
+ import { NavigationTimingPolyfillEntry , ReportHandler } from './types' ;
21
21
22
22
const global = getGlobalObject < Window > ( ) ;
23
23
24
- interface NavigationEntryShim {
25
- // From `PerformanceNavigationTimingEntry`.
26
- entryType : string ;
27
- startTime : number ;
28
-
29
- // From `performance.timing`.
30
- connectEnd ?: number ;
31
- connectStart ?: number ;
32
- domComplete ?: number ;
33
- domContentLoadedEventEnd ?: number ;
34
- domContentLoadedEventStart ?: number ;
35
- domInteractive ?: number ;
36
- domainLookupEnd ?: number ;
37
- domainLookupStart ?: number ;
38
- fetchStart ?: number ;
39
- loadEventEnd ?: number ;
40
- loadEventStart ?: number ;
41
- redirectEnd ?: number ;
42
- redirectStart ?: number ;
43
- requestStart ?: number ;
44
- responseEnd ?: number ;
45
- responseStart ?: number ;
46
- secureConnectionStart ?: number ;
47
- unloadEventEnd ?: number ;
48
- unloadEventStart ?: number ;
49
- }
50
-
51
- type PerformanceTimingKeys =
52
- | 'connectEnd'
53
- | 'connectStart'
54
- | 'domComplete'
55
- | 'domContentLoadedEventEnd'
56
- | 'domContentLoadedEventStart'
57
- | 'domInteractive'
58
- | 'domainLookupEnd'
59
- | 'domainLookupStart'
60
- | 'fetchStart'
61
- | 'loadEventEnd'
62
- | 'loadEventStart'
63
- | 'redirectEnd'
64
- | 'redirectStart'
65
- | 'requestStart'
66
- | 'responseEnd'
67
- | 'responseStart'
68
- | 'secureConnectionStart'
69
- | 'unloadEventEnd'
70
- | 'unloadEventStart' ;
71
-
72
24
const afterLoad = ( callback : ( ) => void ) : void => {
73
25
if ( document . readyState === 'complete' ) {
74
26
// Queue a task so the callback runs after `loadEventEnd`.
@@ -79,27 +31,22 @@ const afterLoad = (callback: () => void): void => {
79
31
}
80
32
} ;
81
33
82
- const getNavigationEntryFromPerformanceTiming = ( ) : PerformanceNavigationTiming => {
34
+ const getNavigationEntryFromPerformanceTiming = ( ) : NavigationTimingPolyfillEntry => {
83
35
// Really annoying that TypeScript errors when using `PerformanceTiming`.
84
- // Note: browsers that do not support navigation entries will fall back to using performance.timing
85
- // (with the timestamps converted from epoch time to DOMHighResTimeStamp).
86
36
// eslint-disable-next-line deprecation/deprecation
87
37
const timing = global . performance . timing ;
88
38
89
- const navigationEntry : NavigationEntryShim = {
39
+ const navigationEntry : { [ key : string ] : number | string } = {
90
40
entryType : 'navigation' ,
91
41
startTime : 0 ,
92
42
} ;
93
43
94
44
for ( const key in timing ) {
95
45
if ( key !== 'navigationStart' && key !== 'toJSON' ) {
96
- navigationEntry [ key as PerformanceTimingKeys ] = Math . max (
97
- timing [ key as PerformanceTimingKeys ] - timing . navigationStart ,
98
- 0 ,
99
- ) ;
46
+ navigationEntry [ key ] = Math . max ( ( timing [ key as keyof PerformanceTiming ] as number ) - timing . navigationStart , 0 ) ;
100
47
}
101
48
}
102
- return navigationEntry as PerformanceNavigationTiming ;
49
+ return navigationEntry as NavigationTimingPolyfillEntry ;
103
50
} ;
104
51
105
52
export const getTTFB = ( onReport : ReportHandler ) : void => {
@@ -114,7 +61,6 @@ export const getTTFB = (onReport: ReportHandler): void => {
114
61
metric . value = metric . delta = ( navigationEntry as PerformanceNavigationTiming ) . responseStart ;
115
62
116
63
metric . entries = [ navigationEntry ] ;
117
- metric . isFinal = true ;
118
64
119
65
onReport ( metric ) ;
120
66
} catch ( error ) {
0 commit comments