This repository has been archived by the owner on Sep 25, 2021. It is now read-only.
Fix behavior when tapping on a same-page anchor link #126
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an attempt to address the issue with tapping on same-page anchor links as noted by @javan here: turbolinks/turbolinks#285 (comment).
The Problem
When tapping on a link to a same-page anchor, it is expected that the page should scroll to the anchor. In the browser, the hash location and scroll position is added to the history so the that visitor can jump back to where they came from. This behaviour is implemented in turbolinks/turbolinks#285 but is still broken on turbolinks-ios/android.
In native Turbolinks apps, new visits are typically pushed onto a navigation stack. However in the case of same-page anchors, this doesn’t really make sense. Also, the concept of jumping back to a previous scroll position does not make sense, because “Back” usually pops the entire screen from the stack.
The Solution
This PR detects whether the proposed visit is to an anchor on the same page, and rather than start a new visit, it just scrolls to the anchor.
Notes on the implementation
This approach should be relatively easy to port to the turbolinks-android bridge, but there are a few things which could be improved, and I thought I’d get some feedback before proceeding.
Reducing repetition oflocationIsSamePageAnchor
ThelocationIsSamePageAnchor
function could probably be moved toTurbolinks.Controller
. This could then be used in turbolinks, turbolinks-ios, and turbolinks-android.UPDATE:
locationIsSamePageAnchor
has been moved to the controller as ofturbolinks/turbolinks@652ddc9 and updated in this repo in a50049e.
visit.location
goes out of syncBypassing the visit for same-page anchors means that the current visit’s
location
is not accurate. The actual location string will be something likehttp://example.com#anchor
, whereas the visit’s location string will still behttp://example.com
. This could be fixed by adding a setter method to the visit e.g.and then be called from
visitProposedToLocationWithAction
:Bypassing the visit process also means that in-flight visits won’t be cancelled. I’m not sure if this is a big deal, or if there might be a way round it.
Also, thanks to @samoli for helping on this!