Skip to content

Commit

Permalink
Fix page loads when refreshing location with anchor (#324)
Browse files Browse the repository at this point in the history
* Fix same-page replace visits.
The likelihood is that a same-page replace visit will want to load a fresh version from the server

* Test refreshing page with replace link

* Blank anchors have no element.
An alternative to #268

* Fix page loads when refreshing location with anchor.
Update the same-page check, making it possible to refresh a location such as example.com#main
  • Loading branch information
domchristie authored Aug 6, 2021
1 parent f04e25b commit d45bf3f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/core/drive/navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,13 @@ export class Navigator {
}

locationWithActionIsSamePage(location: URL, action?: Action): boolean {
return getRequestURL(location) === getRequestURL(this.view.lastRenderedLocation) &&
(getAnchor(location) != null || action == "restore")
const anchor = getAnchor(location)
const currentAnchor = getAnchor(this.view.lastRenderedLocation)
const isRestorationToTop = action === 'restore' && typeof anchor === 'undefined'

return action !== "replace" &&
getRequestURL(location) === getRequestURL(this.view.lastRenderedLocation) &&
(isRestorationToTop || (anchor != null && anchor !== currentAnchor))
}

visitScrolledToSamePageLocation(oldURL: URL, newURL: URL) {
Expand Down
6 changes: 1 addition & 5 deletions src/core/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ export class Snapshot<E extends Element = Element> {
}

getElementForAnchor(anchor: string | undefined) {
try {
return this.element.querySelector(`[id='${anchor}'], a[name='${anchor}']`)
} catch {
return null
}
return anchor ? this.element.querySelector(`[id='${anchor}'], a[name='${anchor}']`) : null
}

get isConnected() {
Expand Down
2 changes: 2 additions & 0 deletions src/tests/fixtures/navigation.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<body>
<a href="#main">Skip Link</a>

<a href="#main" id="refresh-link" data-turbo-action="replace">Refresh Link</a>

<a href="#ignored-link" id="ignored-link">Skipped Content</a>

<section id="main" style="height: 200vh">
Expand Down
6 changes: 6 additions & 0 deletions src/tests/functional/navigation_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ export class NavigationTests extends TurboDriveTestCase {
this.assert.ok(await this.selectorHasFocus("#main a:first-of-type"), "skips to first interactive element after #main")
}

async "test same-page anchored replace link assumes the intention was a refresh"() {
await this.clickSelector('#refresh-link')
await this.nextBody
this.assert.ok(await this.isScrolledToSelector("#main"), "scrolled to #main")
}

async "test navigating back to anchored URL"() {
await this.clickSelector('a[href="#main"]')
await this.nextBeat
Expand Down

0 comments on commit d45bf3f

Please sign in to comment.