Skip to content

Commit 14ad418

Browse files
authored
Merge pull request #208 from hotwired/fix-anchor-visits
Skip the same-page anchor scrolling behavior for visits initiated from the native side
2 parents 8061c9d + fbfd538 commit 14ad418

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

turbo/src/main/assets/js/turbo_bridge.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,25 @@
3434
TurboSession.turboFailedToLoad()
3535
}
3636

37-
visitLocationWithOptionsAndRestorationIdentifier(location, options, restorationIdentifier) {
37+
visitLocationWithOptionsAndRestorationIdentifier(location, optionsJSON, restorationIdentifier) {
38+
let options = JSON.parse(optionsJSON)
39+
let action = options.action
40+
3841
if (window.Turbo) {
39-
Turbo.navigator.startVisit(location, restorationIdentifier, JSON.parse(options))
42+
if (Turbo.navigator.locationWithActionIsSamePage(new URL(location), action)) {
43+
// Skip the same-page anchor scrolling behavior for visits initiated from the native
44+
// side. The page content may be stale and we want a fresh request from the network.
45+
Turbo.navigator.startVisit(location, restorationIdentifier, { "action": "replace" })
46+
} else {
47+
Turbo.navigator.startVisit(location, restorationIdentifier, options)
48+
}
4049
} else if (window.Turbolinks) {
4150
if (Turbolinks.controller.startVisitToLocationWithAction) {
4251
// Turbolinks 5
43-
Turbolinks.controller.startVisitToLocationWithAction(location, JSON.parse(options).action, restorationIdentifier)
52+
Turbolinks.controller.startVisitToLocationWithAction(location, action, restorationIdentifier)
4453
} else {
4554
// Turbolinks 5.3
46-
Turbolinks.controller.startVisitToLocation(location, restorationIdentifier, JSON.parse(options))
55+
Turbolinks.controller.startVisitToLocation(location, restorationIdentifier, options)
4756
}
4857
}
4958
}

0 commit comments

Comments
 (0)