Skip to content

Commit 8155ccd

Browse files
committed
update getTTFB() implement to latest web-vital revision
1 parent 294a55f commit 8155ccd

File tree

2 files changed

+16
-59
lines changed

2 files changed

+16
-59
lines changed

packages/tracing/src/browser/web-vitals/getTTFB.ts

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,58 +17,10 @@
1717
import { getGlobalObject } from '@sentry/utils';
1818

1919
import { initMetric } from './lib/initMetric';
20-
import { ReportHandler } from './types';
20+
import { NavigationTimingPolyfillEntry, ReportHandler } from './types';
2121

2222
const global = getGlobalObject<Window>();
2323

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-
7224
const afterLoad = (callback: () => void): void => {
7325
if (document.readyState === 'complete') {
7426
// Queue a task so the callback runs after `loadEventEnd`.
@@ -79,27 +31,22 @@ const afterLoad = (callback: () => void): void => {
7931
}
8032
};
8133

82-
const getNavigationEntryFromPerformanceTiming = (): PerformanceNavigationTiming => {
34+
const getNavigationEntryFromPerformanceTiming = (): NavigationTimingPolyfillEntry => {
8335
// 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).
8636
// eslint-disable-next-line deprecation/deprecation
8737
const timing = global.performance.timing;
8838

89-
const navigationEntry: NavigationEntryShim = {
39+
const navigationEntry: { [key: string]: number | string } = {
9040
entryType: 'navigation',
9141
startTime: 0,
9242
};
9343

9444
for (const key in timing) {
9545
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);
10047
}
10148
}
102-
return navigationEntry as PerformanceNavigationTiming;
49+
return navigationEntry as NavigationTimingPolyfillEntry;
10350
};
10451

10552
export const getTTFB = (onReport: ReportHandler): void => {
@@ -114,7 +61,6 @@ export const getTTFB = (onReport: ReportHandler): void => {
11461
metric.value = metric.delta = (navigationEntry as PerformanceNavigationTiming).responseStart;
11562

11663
metric.entries = [navigationEntry];
117-
metric.isFinal = true;
11864

11965
onReport(metric);
12066
} catch (error) {

packages/tracing/src/browser/web-vitals/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,14 @@ interface NetworkInformation extends EventTarget {
8282
export interface NavigatorDeviceMemory {
8383
readonly deviceMemory?: number;
8484
}
85+
86+
export type NavigationTimingPolyfillEntry = Omit<
87+
PerformanceNavigationTiming,
88+
| 'initiatorType'
89+
| 'nextHopProtocol'
90+
| 'redirectCount'
91+
| 'transferSize'
92+
| 'encodedBodySize'
93+
| 'decodedBodySize'
94+
| 'toJSON'
95+
>;

0 commit comments

Comments
 (0)