PerformanceResourceTiming: deliveryType 属性

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

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

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

只读属性 deliveryType 是一个字符串,用于指示资源的获取方式——例如,是从缓存获取还是从导航预取获取。

一个字符串,可以是以下值之一

"cache"

该资源是从缓存中检索的。

该资源是通过 Speculation Rules API 从内存缓存中存储的预取响应中检索的。

""(空字符串)

如果不适用上述任何一种交付类型,则返回此值。

示例

过滤资源

deliveryType 属性只能用于获取特定的资源计时条目;例如,仅获取那些已缓存的条目。

以下示例使用 PerformanceObserver 来通知新记录在浏览器性能时间线中的 resource 性能条目。buffered 选项用于访问观察者创建之前的条目。

js
const observer = new PerformanceObserver((list) => {
  const cachedResources = list
    .getEntries()
    .filter((entry) => entry.deliveryType === "cache");
  console.log(cachedResources);
});

observer.observe({ type: "resource", buffered: true });

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

js
const scripts = performance
  .getEntriesByType("resource")
  .filter((entry) => entry.deliveryType === "cache");
console.log(scripts);

规范

规范
资源时序
# dom-performanceresourcetiming-deliverytype

浏览器兼容性