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 {bindReporter} from './lib/bindReporter.js';
|
18 | import {initMetric} from './lib/initMetric.js';
|
19 | import {onBFCacheRestore} from './lib/bfcache.js';
|
20 | import {getNavigationEntry} from './lib/getNavigationEntry.js';
|
21 | import {MetricRatingThresholds, ReportOpts, TTFBMetric} from './types.js';
|
22 | import {getActivationStart} from './lib/getActivationStart.js';
|
23 | import {whenActivated} from './lib/whenActivated.js';
|
24 |
|
25 | /** Thresholds for TTFB. See https://web.dev/articles/ttfb#what_is_a_good_ttfb_score */
|
26 | export const TTFBThresholds: MetricRatingThresholds = [800, 1800];
|
27 |
|
28 | /**
|
29 | * Runs in the next task after the page is done loading and/or prerendering.
|
30 | * @param callback
|
31 | */
|
32 | const whenReady = (callback: () => void) => {
|
33 | if (document.prerendering) {
|
34 | whenActivated(() => whenReady(callback));
|
35 | } else if (document.readyState !== 'complete') {
|
36 | addEventListener('load', () => whenReady(callback), true);
|
37 | } else {
|
38 | // Queue a task so the callback runs after `loadEventEnd`.
|
39 | setTimeout(callback, 0);
|
40 | }
|
41 | };
|
42 |
|
43 | /**
|
44 | * Calculates the [TTFB](https://web.dev/articles/ttfb) value for the
|
45 | * current page and calls the `callback` function once the page has loaded,
|
46 | * along with the relevant `navigation` performance entry used to determine the
|
47 | * value. The reported value is a `DOMHighResTimeStamp`.
|
48 | *
|
49 | * Note, this function waits until after the page is loaded to call `callback`
|
50 | * in order to ensure all properties of the `navigation` entry are populated.
|
51 | * This is useful if you want to report on other metrics exposed by the
|
52 | * [Navigation Timing API](https://w3c.github.io/navigation-timing/). For
|
53 | * example, the TTFB metric starts from the page's [time
|
54 | * origin](https://www.w3.org/TR/hr-time-2/#sec-time-origin), which means it
|
55 | * includes time spent on DNS lookup, connection negotiation, network latency,
|
56 | * and server processing time.
|
57 | */
|
58 | export const onTTFB = (
|
59 | onReport: (metric: TTFBMetric) => void,
|
60 | opts?: ReportOpts,
|
61 | ) => {
|
62 | // Set defaults
|
63 | opts = opts || {};
|
64 |
|
65 | let metric = initMetric('TTFB');
|
66 | let report = bindReporter(
|
67 | onReport,
|
68 | metric,
|
69 | TTFBThresholds,
|
70 | opts.reportAllChanges,
|
71 | );
|
72 |
|
73 | whenReady(() => {
|
74 | const navigationEntry = getNavigationEntry();
|
75 |
|
76 | if (navigationEntry) {
|
77 | // The activationStart reference is used because TTFB should be
|
78 | // relative to page activation rather than navigation start if the
|
79 | // page was prerendered. But in cases where `activationStart` occurs
|
80 | // after the first byte is received, this time should be clamped at 0.
|
81 | metric.value = Math.max(
|
82 | navigationEntry.responseStart - getActivationStart(),
|
83 | 0,
|
84 | );
|
85 |
|
86 | metric.entries = [navigationEntry];
|
87 | report(true);
|
88 |
|
89 | // Only report TTFB after bfcache restores if a `navigation` entry
|
90 | // was reported for the initial load.
|
91 | onBFCacheRestore(() => {
|
92 | metric = initMetric('TTFB', 0);
|
93 | report = bindReporter(
|
94 | onReport,
|
95 | metric,
|
96 | TTFBThresholds,
|
97 | opts!.reportAllChanges,
|
98 | );
|
99 |
|
100 | report(true);
|
101 | });
|
102 | }
|
103 | });
|
104 | };
|