UNPKG

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