From fbfd538f259c0fc3f2791b3567eee6a3fb7e5c7f Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Tue, 14 Dec 2021 11:00:20 -0500 Subject: [PATCH] Skip the same-page anchor scrolling behavior for visits initiated from the native side. This ensures we get the proper visit callbacks and the page content is not stale. --- turbo/src/main/assets/js/turbo_bridge.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/turbo/src/main/assets/js/turbo_bridge.js b/turbo/src/main/assets/js/turbo_bridge.js index a787586c..9ecc39c4 100644 --- a/turbo/src/main/assets/js/turbo_bridge.js +++ b/turbo/src/main/assets/js/turbo_bridge.js @@ -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) } } }