写过前端的人都踩过这个坑——配了 Speculation Rules 后效果好不好,只能 F12 打开 Network 面板蹲着看哪些变蓝了、哪些没动静。今天 Chrome 154 和 Edge 154 用一个 API 把这件事彻底原生化了

写过前端的人都踩过这个坑——配了 Speculation Rules 后效果好不好,只能 F12 打开 Network 面板蹲着看哪些变蓝了、哪些没动静。今天 Chrome 154 和 Edge 154 用一个 performance.speculations API 把这件事彻底原生化了。

这个坑是什么

Speculation Rules API 让浏览器提前 prefetch 或 prerender 你指定的页面,配得好可以让导航秒开。但问题在于——你根本不知道实际命中了多少次、浪费了多少次。

以前你能做的:

  • 在 DevTools Application → Preload 面板里肉眼扫
  • 在 Network 面板里等链接变蓝
  • 靠感觉好像变快了

这些都是定性判断,不是数据。一个真实场景:你的运营在首页放了 20 个预渲染链接,但用户实际只点了 3 个——剩下的 17 次预渲染全是带宽和 CPU 浪费,你完全不知道。

performance.speculations 是什么

Chrome 154+ / Edge 154+ 新增了一个 speculation 性能条目类型。直接读:

const entries = performance.getEntriesByType(“speculation”);
console.log(entries);

每个 entry 大概长这样:

{
name: “/blog/post-1”,
entryType: “speculation”,
speculationType: “prerender”,
// 0 = 没触发,1 = 触发但没激活,2 = 激活了
status: 2,
startTime: 1234.5,
duration: 0
}

status 的三个值含义:

  • 0:预加载已触发但还没用到(用户没点这条链接)
  • 1:预加载触发后被丢弃(被新的 speculation 规则覆盖了)
  • 2:预加载真正激活了(用户点了这条链接,页面瞬间出来)

实战:算清你的 speculation 命中率

有了这个 API,终于可以量化 speculation 规则的真实效率:

function reportSpeculationStats() {
const entries = performance.getEntriesByType(“speculation”);

const byType = { prerender: [], prefetch: [] };
entries.forEach(e => byType[e.speculationType].push(e));

const stats = {};
for (const [type, items] of Object.entries(byType)) {
const activated = items.filter(e => e.status === 2).length;
const triggered = items.filter(e => e.status >= 0).length;
stats[type] = {
total: items.length,
activated,
wasteRate: triggered > 0
? ((triggered – activated) / triggered * 100).toFixed(1) + “%”
: “0%”
};
}

navigator.sendBeacon(“/rum”, JSON.stringify({
url: location.pathname,
speculation: stats,
timestamp: Date.now()
}));
}

document.addEventListener(“visibilitychange”, () => {
if (document.visibilityState === “hidden”) {
reportSpeculationStats();
}
});

输出大概长这样:

{
“url”: “/blog”,
“speculation”: {
“prerender”: { “total”: 12, “activated”: 2, “wasteRate”: “83.3%” },
“prefetch”: { “total”: 8, “activated”: 5, “wasteRate”: “37.5%” }
}
}

首页 prerender 规则浪费了 83% 的预渲染资源,该收窄到高概率点击的链接了。

结合 deliveryType 算 CDN 节省量

Resource Timing 条目里有个 deliveryType 属性,能告诉你这个资源是从哪儿来的:

const cached = performance.getEntriesByType(“resource”)
.filter(e => e.deliveryType === “navigational-prefetch”);

console.log(“从 speculation 缓存来的资源:” + cached.length + ” 个”);

结合这两套数据,你可以完整回答三个问题:

  1. 我的 speculation 命中率是多少?→ status=2 / total
  2. 哪些 speculation 被浪费了?→ status=0 或 1 的条目
  3. speculation 实际给我省了多少请求?→ deliveryType === “navigational-prefetch” 的资源数

兼容性与渐进增强

performance.speculations 目前只有 Chrome 154+ 和 Edge 154+ 支持,约 70-75% 全球覆盖。Safari 和 Firefox 会静默忽略,不影响任何功能。

if (PerformanceObserver.supportedEntryTypes.includes(“speculation”)) {
new PerformanceObserver(list => {
list.getEntries().forEach(entry => {
console.log(“speculation event:”, entry.name, entry.status);
});
}).observe({ type: “speculation”, buffered: true });
}

下一步

如果你已经在用 Speculation Rules,今天加这一段代码成本不到 10 行。下一步:

  1. 把 reportSpeculationStats() 接入你的 RUM 系统
  2. 跑一周数据,看看各页面的 waste rate 是多少
  3. waste rate > 70% 的页面,把 eagerness 从 immediate 降到 moderate 或 conservative

配了这么久 Speculation Rules,今天终于可以拿着数据说话,而不是靠感觉变快了。

评论区

0 条评论

登录后可评论。

阿速·性能优化 1013 阅读