Skip to content

Commit

Permalink
Merge pull request #208 from hotwired/fix-anchor-visits
Browse files Browse the repository at this point in the history
Skip the same-page anchor scrolling behavior for visits initiated from the native side
  • Loading branch information
jayohms authored Dec 14, 2021
2 parents 8061c9d + fbfd538 commit 14ad418
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions turbo/src/main/assets/js/turbo_bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,25 @@
TurboSession.turboFailedToLoad()
}

visitLocationWithOptionsAndRestorationIdentifier(location, options, restorationIdentifier) {
visitLocationWithOptionsAndRestorationIdentifier(location, optionsJSON, restorationIdentifier) {
let options = JSON.parse(optionsJSON)
let action = options.action

if (window.Turbo) {
Turbo.navigator.startVisit(location, restorationIdentifier, JSON.parse(options))
if (Turbo.navigator.locationWithActionIsSamePage(new URL(location), action)) {
// Skip the same-page anchor scrolling behavior for visits initiated from the native
// side. The page content may be stale and we want a fresh request from the network.
Turbo.navigator.startVisit(location, restorationIdentifier, { "action": "replace" })
} else {
Turbo.navigator.startVisit(location, restorationIdentifier, options)
}
} else if (window.Turbolinks) {
if (Turbolinks.controller.startVisitToLocationWithAction) {
// Turbolinks 5
Turbolinks.controller.startVisitToLocationWithAction(location, JSON.parse(options).action, restorationIdentifier)
Turbolinks.controller.startVisitToLocationWithAction(location, action, restorationIdentifier)
} else {
// Turbolinks 5.3
Turbolinks.controller.startVisitToLocation(location, restorationIdentifier, JSON.parse(options))
Turbolinks.controller.startVisitToLocation(location, restorationIdentifier, options)
}
}
}
Expand Down

0 comments on commit 14ad418

Please sign in to comment.