Skip to content

Commit

Permalink
Document events dispatched during preloading
Browse files Browse the repository at this point in the history
Documents [@hotwired/turbo#1034][]

Document the `turbo:before-fetch-request` and
`turbo:before-fetch-response` events dispatched during `<a>` preloading.

[@hotwired/turbo#1034]: hotwired/turbo#1034
  • Loading branch information
seanpdoyle committed Feb 20, 2024
1 parent 5cf3013 commit 7fe0f8e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions _source/handbook/02_drive.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,16 @@ won't have any effect on links that:
It also dovetails nicely with pages that leverage [Eager-Loading Frames](/reference/frames#eager-loaded-frame) or [Lazy-Loading Frames](/reference/frames#lazy-loaded-frame). As you can preload the structure of the page and show the user a meaningful loading state while the interesting content loads.
<br><br>
Note that preloaded `<a>` elements will dispatch [turbo:before-fetch-request](/reference/events) and [turbo:before-fetch-response](/reference/events) events. To distinguish a preloading `turbo:before-fetch-request` initiated event from an event initiated by another mechanism, check whether the request's `X-Sec-Purpose` header (read from the `event.detail.fetchOptions.headers["X-Sec-Purpose"]` property) is set to `"prefetch"`:
```js
addEventListener("turbo:before-fetch-request", (event) => {
if (event.detail.fetchOptions.headers["X-Sec-Purpose"] === "prefetch") {
// do additional preloading setup…
} else {
// do something else…
}
})
```
[data-turbo-preload]: /reference/attributes#data-attributes
2 changes: 1 addition & 1 deletion _source/reference/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ initiated the request, the events could fire on:

### `turbo:before-fetch-request`

Fires before Turbo issues a network request to fetch the page. Access the requested location with `event.detail.url` and the fetch options object with `event.detail.fetchOptions`. This event fires on the respective element (`<turbo-frame>` or `<form>` element) which triggers it and can be accessed with [event.target][] property. Request can be canceled and continued with `event.detail.resume` (see [Pausing Requests][]).
Fires before Turbo issues a network request (to fetch a page, submit a form, preload a link, etc.). Access the requested location with `event.detail.url` and the fetch options object with `event.detail.fetchOptions`. This event fires on the respective element (`<turbo-frame>` or `<form>` element) which triggers it and can be accessed with [event.target][] property. Request can be canceled and continued with `event.detail.resume` (see [Pausing Requests][]).

| `event.detail` property | Type | Description
|---------------------------|-----------------------------------|------------
Expand Down

0 comments on commit 7fe0f8e

Please sign in to comment.