|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { initMetric } from './lib/initMetric'; |
| 18 | +import { ReportHandler } from './types'; |
| 19 | + |
| 20 | +interface NavigationEntryShim { |
| 21 | + // From `PerformanceNavigationTimingEntry`. |
| 22 | + entryType: string; |
| 23 | + startTime: number; |
| 24 | + |
| 25 | + // From `performance.timing`. |
| 26 | + connectEnd?: number; |
| 27 | + connectStart?: number; |
| 28 | + domComplete?: number; |
| 29 | + domContentLoadedEventEnd?: number; |
| 30 | + domContentLoadedEventStart?: number; |
| 31 | + domInteractive?: number; |
| 32 | + domainLookupEnd?: number; |
| 33 | + domainLookupStart?: number; |
| 34 | + fetchStart?: number; |
| 35 | + loadEventEnd?: number; |
| 36 | + loadEventStart?: number; |
| 37 | + redirectEnd?: number; |
| 38 | + redirectStart?: number; |
| 39 | + requestStart?: number; |
| 40 | + responseEnd?: number; |
| 41 | + responseStart?: number; |
| 42 | + secureConnectionStart?: number; |
| 43 | + unloadEventEnd?: number; |
| 44 | + unloadEventStart?: number; |
| 45 | +} |
| 46 | + |
| 47 | +type PerformanceTimingKeys = |
| 48 | + | 'connectEnd' |
| 49 | + | 'connectStart' |
| 50 | + | 'domComplete' |
| 51 | + | 'domContentLoadedEventEnd' |
| 52 | + | 'domContentLoadedEventStart' |
| 53 | + | 'domInteractive' |
| 54 | + | 'domainLookupEnd' |
| 55 | + | 'domainLookupStart' |
| 56 | + | 'fetchStart' |
| 57 | + | 'loadEventEnd' |
| 58 | + | 'loadEventStart' |
| 59 | + | 'redirectEnd' |
| 60 | + | 'redirectStart' |
| 61 | + | 'requestStart' |
| 62 | + | 'responseEnd' |
| 63 | + | 'responseStart' |
| 64 | + | 'secureConnectionStart' |
| 65 | + | 'unloadEventEnd' |
| 66 | + | 'unloadEventStart'; |
| 67 | + |
| 68 | +const afterLoad = (callback: () => void): void => { |
| 69 | + if (document.readyState === 'complete') { |
| 70 | + // Queue a task so the callback runs after `loadEventEnd`. |
| 71 | + setTimeout(callback, 0); |
| 72 | + } else { |
| 73 | + // Use `pageshow` so the callback runs after `loadEventEnd`. |
| 74 | + addEventListener('pageshow', callback); |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +const getNavigationEntryFromPerformanceTiming = (): PerformanceNavigationTiming => { |
| 79 | + // Really annoying that TypeScript errors when using `PerformanceTiming`. |
| 80 | + // Note: browsers that do not support navigation entries will fall back to using performance.timing |
| 81 | + // (with the timestamps converted from epoch time to DOMHighResTimeStamp). |
| 82 | + // eslint-disable-next-line deprecation/deprecation |
| 83 | + const timing = performance.timing; |
| 84 | + |
| 85 | + const navigationEntry: NavigationEntryShim = { |
| 86 | + entryType: 'navigation', |
| 87 | + startTime: 0, |
| 88 | + }; |
| 89 | + |
| 90 | + for (const key in timing) { |
| 91 | + if (key !== 'navigationStart' && key !== 'toJSON') { |
| 92 | + navigationEntry[key as PerformanceTimingKeys] = Math.max( |
| 93 | + timing[key as PerformanceTimingKeys] - timing.navigationStart, |
| 94 | + 0, |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | + return navigationEntry as PerformanceNavigationTiming; |
| 99 | +}; |
| 100 | + |
| 101 | +export const getTTFB = (onReport: ReportHandler): void => { |
| 102 | + const metric = initMetric('TTFB'); |
| 103 | + |
| 104 | + afterLoad(() => { |
| 105 | + try { |
| 106 | + // Use the NavigationTiming L2 entry if available. |
| 107 | + const navigationEntry = |
| 108 | + performance.getEntriesByType('navigation')[0] || getNavigationEntryFromPerformanceTiming(); |
| 109 | + |
| 110 | + metric.value = metric.delta = (navigationEntry as PerformanceNavigationTiming).responseStart; |
| 111 | + |
| 112 | + metric.entries = [navigationEntry]; |
| 113 | + metric.isFinal = true; |
| 114 | + |
| 115 | + onReport(metric); |
| 116 | + } catch (error) { |
| 117 | + // Do nothing. |
| 118 | + } |
| 119 | + }); |
| 120 | +}; |
0 commit comments