Comments (5)
Completed (as best as is possible) in #458
from web-vitals.
Decided to add as additional experimental breakdowns:
- redirectTime
- cacheTime (including SW + HTTP Cache time)
We will leave waitingTime
as is for now, but deprecate it, and drop it for v4.
So adding up the 6 metrics will not equal the total and need to add either redirectTime + cacheTime
or waitingTime
and the others to make the total.
from web-vitals.
Good morning Barry, here is the version I use.
It's more in line with the https://www.w3.org/TR/navigation-timing-2/#processing-model
I do the starttimes a bit different because I believe it makes more sense to at-least set start to the start of the entry before it then to 0
Feel free to use .... or not :-)
attribution/onTTFB.js
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {onTTFB as unattributedOnTTFB} from '../onTTFB.js';
import {
TTFBMetric,
TTFBMetricWithAttribution,
TTFBReportCallback,
TTFBReportCallbackWithAttribution,
ReportOpts,
} from '../types.js';
const attributeTTFB = (metric: TTFBMetric): void => {
if (metric.entries.length) {
const navigationEntry = metric.entries[0];
const activationStart = navigationEntry.activationStart || 0;
const workerStart = Math.max(navigationEntry.workerStart - activationStart, activationStart);
const dnsStart = Math.max(navigationEntry.domainLookupStart - activationStart, workerStart);
const tcpStart = Math.max(navigationEntry.connectStart - activationStart, dnsStart);
const sslStart = Math.max(navigationEntry.secureConnectionStart - activationStart, tcpStart);
const requestStart = Math.max(navigationEntry.requestStart - activationStart, sslStart);
const responseStart = Math.max(navigationEntry.responseStart - activationStart, requestStart);
(metric as TTFBMetricWithAttribution).attribution = {
redirectTime: workerStart - activationStart,
workerAndCacheTime: dnsStart - workerStart,
dnsTime: tcpStart - dnsStart,
tcpTime: sslStart - tcpStart,
sslTime: requestStart - sslStart,
requestTime: responseStart - requestStart,
navigationEntry: navigationEntry,
};
return;
}
// Set an empty object if no other attribution has been set.
(metric as TTFBMetricWithAttribution).attribution = {
redirectTime: 0,
workerAndCacheTime: 0,
dnsTime: 0,
tcpTime: 0,
sslTime: 0,
requestTime: 0
};
};
/**
* Calculates the [TTFB](https://web.dev/articles/ttfb) value for the
* current page and calls the `callback` function once the page has loaded,
* along with the relevant `navigation` performance entry used to determine the
* value. The reported value is a `DOMHighResTimeStamp`.
*
* Note, this function waits until after the page is loaded to call `callback`
* in order to ensure all properties of the `navigation` entry are populated.
* This is useful if you want to report on other metrics exposed by the
* [Navigation Timing API](https://w3c.github.io/navigation-timing/). For
* example, the TTFB metric starts from the page's [time
* origin](https://www.w3.org/TR/hr-time-2/#sec-time-origin), which means it
* includes time spent on DNS lookup, connection negotiation, network latency,
* and server processing time.
*/
export const onTTFB = (
onReport: TTFBReportCallbackWithAttribution,
opts?: ReportOpts
) => {
unattributedOnTTFB(
((metric: TTFBMetricWithAttribution) => {
attributeTTFB(metric);
onReport(metric);
}) as TTFBReportCallback,
opts
);
};
and types/ttfb.js
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type {Metric} from './base.js';
import {NavigationTimingPolyfillEntry} from './polyfills.js';
/**
* A TTFB-specific version of the Metric object.
*/
export interface TTFBMetric extends Metric {
name: 'TTFB';
entries: PerformanceNavigationTiming[] | NavigationTimingPolyfillEntry[];
}
/**
* An object containing potentially-helpful debugging information that
* can be sent along with the TTFB value for the current page visit in order
* to help identify issues happening to real-users in the field.
*/
export interface TTFBAttribution {
/**
* The total redirect time.s.
*/
redirectTime: number;
/**
* The total worker and cache time.
*/
workerAndCacheTime: number;
/**
* The total dns time.
*/
dnsTime: number;
/**
* The total tcp time.
*/
tcpTime: number;
/**
* The total ssl time.
*/
sslTime: number;
/**
* The total reqquest time.
*/
requestTime: number;
/**
* The `navigation` entry of the current page, which is useful for diagnosing
* general page load issues. This can be used to access `serverTiming` for example:
* navigationEntry?.serverTiming
*/
navigationEntry?: PerformanceNavigationTiming | NavigationTimingPolyfillEntry;
}
/**
* A TTFB-specific version of the Metric object with attribution.
*/
export interface TTFBMetricWithAttribution extends TTFBMetric {
attribution: TTFBAttribution;
}
/**
* A TTFB-specific version of the ReportCallback function.
*/
export interface TTFBReportCallback {
(metric: TTFBMetric): void;
}
/**
* A TTFB-specific version of the ReportCallback function with attribution.
*/
export interface TTFBReportCallbackWithAttribution {
(metric: TTFBMetricWithAttribution): void;
}
from web-vitals.
Decided to add as additional experimental breakdowns
redirectTime
[and]cacheTime
. We will leavewaitingTime
as is for now, but deprecate it, and drop it for v4.
Hey @tunetheweb + @philipwalton given that the first 4.0-beta has been cut, is it fair to guess that redirectTime
won't make it into v4?
from web-vitals.
This would be a nice one to get in before v4 since it would be a breaking change if we change waitingTime. Will have a look this week and see if we can add to v4.
from web-vitals.
Related Issues (20)
- Webpack Module is undefined HOT 2
- iOS Chrome Support HOT 1
- differences in TTFB in safari vs chromium based browsers HOT 1
- LCP value setting to 0 intermittently HOT 7
- Add INP breakdown entries to the attribution build HOT 3
- User consent policy HOT 1
- Firefox LCP tests failing
- Bug: LCP attribution can include `resourceLoadDelay` attribution timings far greater than the LCP value in cases where `onTTFB` discards the navigation data HOT 1
- FCP and TTFB are triggered multiple times HOT 7
- contributing LoAF basesd code that idenifies LoAF, INF and JS long tasks data HOT 1
- Capturing metadata with events HOT 2
- Allow to reset the INP calculation HOT 2
- v4 giving undefined TypeError HOT 1
- INP: Probably a bad measurement metric for web vitals - even with simple HTML structure pages with no CSS will cause bad value. HOT 1
- Expose INP target element as first class citizen in attribution HOT 3
- Expose ability to reset internal state of metrics (specifically INP) HOT 2
- v4 INP attribution ending processingEnd time in the wrong animation frame HOT 8
- CDN JS - Web server is returning an unknown error HOT 4
- CLS and INP are being calculated incorrectly HOT 2
- Suggestion to track all attributions HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from web-vitals.