diff --git a/src/observers/link_prefetch_observer.js b/src/observers/link_prefetch_observer.js
index de08e05da..d204cb2a5 100644
--- a/src/observers/link_prefetch_observer.js
+++ b/src/observers/link_prefetch_observer.js
@@ -100,14 +100,11 @@ export class LinkPrefetchObserver {
request.headers["Sec-Purpose"] = "prefetch"
- if (link.dataset.turboFrame && link.dataset.turboFrame !== "_top") {
- request.headers["Turbo-Frame"] = link.dataset.turboFrame
- } else if (link.dataset.turboFrame !== "_top") {
- const turboFrame = link.closest("turbo-frame")
+ const turboFrame = link.closest("turbo-frame")
+ const turboFrameTarget = link.getAttribute("data-turbo-frame") || turboFrame?.getAttribute("target") || turboFrame?.id
- if (turboFrame) {
- request.headers["Turbo-Frame"] = turboFrame.id
- }
+ if (turboFrameTarget && turboFrameTarget !== "_top") {
+ request.headers["Turbo-Frame"] = turboFrameTarget
}
if (link.hasAttribute("data-turbo-stream")) {
diff --git a/src/tests/fixtures/hover_to_prefetch.html b/src/tests/fixtures/hover_to_prefetch.html
index e0748fe0e..89b94f1bb 100644
--- a/src/tests/fixtures/hover_to_prefetch.html
+++ b/src/tests/fixtures/hover_to_prefetch.html
@@ -41,5 +41,13 @@
Won't prefetch when hovering me
+
+
+ Hover to prefetch me
+
+
+
+ Hover to prefetch me
+