大网数据广告活动
Web 标准与前端开发

Document: prerendering property

· 52CE 我爱测

The **`prerendering`** read-only property of the Document interface returns `true` if the document is currently in the process of prerendering, as initiated via the Speculation Rules API.

Speculation Rules API

The prerendering read-only property of the Document interface returns true if the document is currently in the process of prerendering, as initiated via the Speculation Rules API.

Value

A boolean. Returns true if the document is currently in the process of prerendering, and false if it is not. false will be returned for documents that have finished prerendering, and documents that were not prerendered.

Examples

To run an activity while the page is prerendering, you can check for the prerendering property. You could for example run some analytics:

if (document.prerendering) {
  analytics.sendInfo("got this far during prerendering!");
}

When a prerendered document is activated, PerformanceNavigationTiming.activationStart is set to a DOMHighResTimeStamp value representing the time between when the prerender was started and the document was actually activated. The following function can check for prerendering and prerendered pages:

function pagePrerendered() {
  return (
    document.prerendering ||
    performance.getEntriesByType("navigation")[0]?.activationStart > 0
  );
}

When the prerendered page is activated by the user viewing the page, the Document.prerenderingchange_event event will fire. This can be used to enable activities that previously would be started by default on page load but which you wish to delay until the page is actually viewed by the user. The following code sets up an event listener to run a function once prerendering has finished, on a prerendered page, or runs it immediately on a non-prerendered page:

if (document.prerendering) {
  document.addEventListener("prerenderingchange", initAnalytics, {
    once: true,
  });
} else {
  initAnalytics();
}

[!NOTE] See the Speculation Rules API landing page and particularly the Unsafe speculative loading conditions section for more information on the kinds of activities you might wish to delay.

To measure how often a prerender is activated, combine all three APIs: document.prerendering to detect cases where the page is currently prerendering, prerenderingchange to watch for activations in that case, and activationStart to check for cases where the page was prerendered in the past.

if (document.prerendering) {
  document.addEventListener(
    "prerenderingchange",
    () => {
      console.log("Prerender activated after this script ran");
    },
    { once: true },
  );
} else if (performance.getEntriesByType("navigation")[0]?.activationStart > 0) {
  console.log("Prerender activated before this script ran");
} else {
  console.log("This page load was not via prerendering");
}

Specifications

Browser compatibility

See also

  • Speculation Rules API
  • Document.prerenderingchange_event event
  • PerformanceNavigationTiming.activationStart property

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。