PerformanceResourceTiming: serverTiming 属性
注意:此功能在 Web Workers 中可用。
只读属性 serverTiming 返回一个包含服务器计时指标的 PerformanceServerTiming 条目数组。
服务器计时指标需要服务器发送 Server-Timing 标头。例如:
http
Server-Timing: cache;desc="Cache Read";dur=23.2
serverTiming 条目可以存在于 navigation 和 resource 条目上。
值
一个 PerformanceServerTiming 条目数组。
示例
记录服务器计时条目
您可以使用 PerformanceObserver 来观察 PerformanceServerTiming 条目。每个服务器条目的持续时间将被记录到控制台。
使用 PerformanceObserver 的示例,它会在浏览器性能时间线中记录新的 resource 性能条目时通知。使用 buffered 选项可以访问观察者创建之前的条目。
js
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
entry.serverTiming.forEach((serverEntry) => {
console.log(`${serverEntry.name} duration: ${serverEntry.duration}`);
});
});
});
["navigation", "resource"].forEach((type) =>
observer.observe({ type, buffered: true }),
);
使用 Performance.getEntriesByType() 的示例,它只显示在调用此方法时浏览器性能时间线中存在的 resource 性能条目
js
for (const entryType of ["navigation", "resource"]) {
for (const { name: url, serverTiming } of performance.getEntriesByType(
entryType,
)) {
if (serverTiming) {
for (const { name, duration } of serverTiming) {
console.log(`${url}: ${name} duration: ${duration}`);
}
}
}
}
跨域服务器计时信息
对服务器计时信息的访问仅限于同源。要公开跨域计时信息,需要设置 Timing-Allow-Origin HTTP 响应标头。
例如,要允许 https://mdn.org.cn 查看服务器计时信息,跨域资源应发送:
http
Timing-Allow-Origin: https://mdn.org.cn
规范
| 规范 |
|---|
| 服务器计时 # servertiming-attribute |
浏览器兼容性
加载中…