Skip to content

Commit

Permalink
Treat same-page anchor Visit
Browse files Browse the repository at this point in the history
Closes hotwired#400

Invoke `Visit.goToSamePageAnchor()` earlier in the
`BrowserAdapter.visitStarted()` delegate method.

If a `Visit` is to an anchor on the same page, invoke it at the same
time a simulated request that's pre-populated from a `VisitResponse`
instance in the hopes that the asynchronous Visit delegate callbacks
won't be in competition with the same-page scrolling asynchrony.
  • Loading branch information
seanpdoyle committed Nov 26, 2021
1 parent aa9724d commit ef92095
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/core/drive/visit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ export class Visit implements FetchRequestDelegate {
issueRequest() {
if (this.hasPreloadedResponse()) {
this.simulateRequest()
} else if (this.isSamePage) {
this.goToSamePageAnchor()
} else if (this.shouldIssueRequest() && !this.request) {
this.request = new FetchRequest(this, FetchMethod.get, this.location)
this.request.perform()
Expand Down
1 change: 0 additions & 1 deletion src/core/native/browser_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export class BrowserAdapter implements Adapter {
visit.loadCachedSnapshot()
visit.issueRequest()
visit.changeHistory()
visit.goToSamePageAnchor()
}

visitRequestStarted(visit: Visit) {
Expand Down
6 changes: 6 additions & 0 deletions src/tests/fixtures/visit.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
<title>Turbo</title>
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
<script src="/src/tests/fixtures/test.js"></script>
<style>
.push-below-fold { margin-top: 100vh; }
</style>
</head>
<body>
<section>
<h1>Visit</h1>
<p><a id="same-origin-link" href="/src/tests/fixtures/one.html">Same-origin link</a></p>
<p><a id="same-origin-link-search-params" href="/src/tests/fixtures/one.html?key=value">Same-origin link with ?key=value</a></p>
<p><a id="sample-response" href="/src/tests/fixtures/one.html">Sample response</a></p>
<p><a id="same-page-link" href="/src/tests/fixtures/visit.html">Same page link</a></p>
<hr class="push-below-fold">
<p><h1 id="below-the-fold-heading">Same page heading</h1></p>
</section>
</body>
</html>
24 changes: 24 additions & 0 deletions src/tests/functional/visit_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,30 @@ export class VisitTests extends TurboDriveTestCase {
this.assert.notOk(await this.hasSelector("some-cached-element"))
}

async "can scroll to element after turbo:visit"() {
const id = "below-the-fold-heading"
await this.evaluate((id: string) => {
addEventListener("turbo:load",
() => document.getElementById(id)?.scrollIntoView(),
{ once: true }
)
}, id)

this.assert(await this.isScrolledToTop, "starts unscrolled")

await this.clickSelector("#same-page-link")
await this.nextEventNamed("turbo:load")

this.assert(await this.isScrolledToSelector("#" + id), "scrolls after turbo:load")

await this.clickSelector("#same-origin-link")
await this.nextEventNamed("turbo:load")
await this.goBack()
await this.nextEventNamed("turbo:load")

this.assert(await this.isScrolledToSelector("#" + id), "scrolls after turbo:load")
}

async visitLocation(location: string) {
this.remote.execute((location: string) => window.Turbo.visit(location), [location])
}
Expand Down

0 comments on commit ef92095

Please sign in to comment.