PerformanceServerTiming: name 属性

Baseline 已广泛支持

此功能已成熟,并可在许多设备和浏览器版本上运行。自 2023 年 3 月以来,它已在各种浏览器中可用。

注意:此功能在 Web Workers 中可用。

name 只读属性返回服务器指定的指标名称的字符串值。

字符串。

示例

记录服务器计时条目

服务器计时指标需要服务器发送 Server-Timing 标头。例如:

http
Server-Timing: cache;desc="Cache Read";dur=23.2

serverTiming 条目可以存在于 navigationresource 条目上。

使用 PerformanceObserver 的示例,它会在 navigationresource 性能条目被记录到浏览器性能时间线上时通知。使用 buffered 选项可以访问观察者创建之前的条目。

js
const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    entry.serverTiming.forEach((serverEntry) => {
      console.log(
        `${serverEntry.name} (${serverEntry.description}) duration: ${serverEntry.duration}`,
      );
      // Logs "cache (Cache Read) duration: 23.2"
    });
  });
});

["navigation", "resource"].forEach((type) =>
  observer.observe({ type, buffered: true }),
);

使用 Performance.getEntriesByType() 的示例,它仅显示在调用此方法时存在于浏览器性能时间线中的 navigationresource 性能条目。

js
for (const entryType of ["navigation", "resource"]) {
  for (const { name: url, serverTiming } of performance.getEntriesByType(
    entryType,
  )) {
    if (serverTiming) {
      for (const { name, description, duration } of serverTiming) {
        console.log(`${name} (${description}) duration: ${duration}`);
        // Logs "cache (Cache Read) duration: 23.2"
      }
    }
  }
}

规范

规范
服务器计时
# dom-performanceservertiming-name

浏览器兼容性

另见