-
Notifications
You must be signed in to change notification settings - Fork 22.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docs for
Request.isHistoryNavigation
(#36514)
* Add docs for `Request.isHistoryNavigation` * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update files/en-us/web/api/request/ishistorynavigation/index.md * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: wbamberg <[email protected]> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: wbamberg <[email protected]>
- Loading branch information
1 parent
a748228
commit 22526bd
Showing
3 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
title: "Request: isHistoryNavigation property" | ||
short-title: isHistoryNavigation | ||
slug: Web/API/Request/isHistoryNavigation | ||
page-type: web-api-instance-property | ||
browser-compat: api.Request.isHistoryNavigation | ||
--- | ||
|
||
{{APIRef("Fetch API")}}{{AvailableInWorkers}} | ||
|
||
The **`isHistoryNavigation`** read-only property of the {{domxref("Request")}} interface is a boolean indicating whether the request is a history navigation. | ||
|
||
A history navigation is a navigation within the browser's history, made by calling {{domxref("History.go()")}}, {{domxref("History.back()")}}, {{domxref("History.forward()")}}, {{domxref("Navigation.traverseTo()")}}, {{domxref("Navigation.back()")}}, {{domxref("Navigation.forward()")}}, or directly by clicking the browser's back or forward navigation button. | ||
|
||
## Value | ||
|
||
A boolean value. | ||
|
||
## Examples | ||
|
||
This example executes in a service worker. It listens for the {{domxref("ServiceWorkerGlobalScope/fetch_event", "fetch")}} event. In the event handler, the service worker checks the `isHistoryNavigation` property to know whether the request happened because of a history navigation. If so, it attempts to respond with a cached response. If the cache does not contain a response for this request, the service worker fetches a response from the network, caches a clone of it, and responds with the network response. | ||
|
||
```js | ||
self.addEventListener("request", (event) => { | ||
// ... | ||
|
||
if (event.request.isHistoryNavigation) { | ||
event.respondWith( | ||
caches.match(event.request).then((response) => { | ||
if (response !== undefined) { | ||
return response; | ||
} else { | ||
return fetch(event.request).then((response) => { | ||
let responseClone = response.clone(); | ||
|
||
caches.open("v1").then((cache) => { | ||
cache.put(event.request, responseClone); | ||
}); | ||
|
||
return response; | ||
}); | ||
} | ||
}), | ||
); | ||
} | ||
|
||
// ... | ||
}); | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("History API", "", "", 1)}} | ||
- {{domxref("Navigation API", "", "", 1)}} | ||
- {{domxref("Service Worker API", "", "", 1)}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters