Skip to content

Commit

Permalink
Handle same-page anchor clicks by checking for absence of popstate data.
Browse files Browse the repository at this point in the history
Absent popstate event data suggests that the event originates from a same-page anchor click. Reconcile the empty state by incrementing the History's currentIndex, replacing the null history entry, setting the View's lastRenderedLocation, and then caching it
  • Loading branch information
domchristie committed Jul 20, 2024
1 parent 7adbf9f commit 4ba8aea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/core/drive/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,17 @@ export class History {
// Event handlers

onPopState = (event) => {
const { turbo } = event.state || {}
if (turbo) {
this.location = new URL(window.location.href)
const { restorationIdentifier, restorationIndex } = turbo
this.location = new URL(window.location.href)

if (event.state?.turbo) {
const { restorationIdentifier, restorationIndex } = event.state.turbo
this.restorationIdentifier = restorationIdentifier
const direction = restorationIndex > this.currentIndex ? "forward" : "back"
this.delegate.historyPoppedToLocationWithRestorationIdentifierAndDirection(this.location, restorationIdentifier, direction)
this.currentIndex = restorationIndex
} else {
this.currentIndex++
this.delegate.historyPoppedWithEmptyState(this.location)
}
}
}
12 changes: 11 additions & 1 deletion src/core/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ export class Session {
}
}

historyPoppedWithEmptyState(location) {
this.#reconcileEmptyHistoryEntry(location)
}

// Scroll observer delegate

scrollPositionChanged(position) {
Expand Down Expand Up @@ -233,7 +237,7 @@ export class Session {
// Navigator delegate

allowsVisitingLocationWithAction(location, action) {
return this.locationWithActionIsSamePage(location, action) || this.applicationAllowsVisitingLocation(location)
return this.applicationAllowsVisitingLocation(location)
}

visitProposedToLocation(location, options) {
Expand Down Expand Up @@ -469,6 +473,12 @@ export class Session {
get snapshot() {
return this.view.snapshot
}

#reconcileEmptyHistoryEntry(location) {
this.history.replace(location)
this.view.lastRenderedLocation = location
this.view.cacheSnapshot()
}
}

// Older versions of the Turbo Native adapters referenced the
Expand Down

0 comments on commit 4ba8aea

Please sign in to comment.