From 4ec2b39dd6c044ce914c34c19c24ae525a82b1de Mon Sep 17 00:00:00 2001
From: afcapel Eager-Loading Frames or Lazy-Loading Frames. As you can preload the structure of the page and show the user a meaningful loading state while the interesting content loads.
Note that preloaded <a>
elements will dispatch turbo:before-fetch-request and turbo:before-fetch-response 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"
:
addEventListener("turbo:before-fetch-request", (event) => {
if (event.detail.fetchOptions.headers["X-Sec-Purpose"] === "prefetch") {
// do additional preloading setup…
} else {
// do something else…
}
})