请求:isHistoryNavigation 属性

可用性有限

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

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

Request 接口的只读属性 isHistoryNavigation 是一个布尔值,指示请求是否是历史导航。

历史导航是通过调用 History.go()History.back()History.forward()Navigation.traverseTo()Navigation.back()Navigation.forward(),或直接点击浏览器后退或前进导航按钮而在浏览器历史记录中进行的导航。

一个布尔值。

示例

此示例在服务工作线程中执行。它监听 fetch 事件。在事件处理程序中,服务工作线程会检查 isHistoryNavigation 属性,以了解请求是否是由于历史导航而发生的。如果是,它会尝试使用缓存的响应进行响应。如果缓存中不包含此请求的响应,服务工作线程会从网络获取响应,缓存其副本,并使用网络响应进行响应。

js
self.addEventListener("request", (event) => {
  // …

  if (event.request.isHistoryNavigation) {
    event.respondWith(
      caches.match(event.request).then((response) => {
        if (response !== undefined) {
          return response;
        }
        return fetch(event.request).then((response) => {
          const responseClone = response.clone();

          caches
            .open("v1")
            .then((cache) => cache.put(event.request, responseClone));

          return response;
        });
      }),
    );
  }

  // …
});

规范

规范
Fetch
# ref-for-dom-request-ishistorynavigation①

浏览器兼容性

另见