Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify same page anchor visits #1285

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 12 additions & 30 deletions src/core/drive/history.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { nextMicrotask, uuid } from "../../util"
import { uuid } from "../../util"

export class History {
location
restorationIdentifier = uuid()
restorationData = {}
started = false
pageLoaded = false
currentIndex = 0

constructor(delegate) {
Expand All @@ -15,7 +14,6 @@ export class History {
start() {
if (!this.started) {
addEventListener("popstate", this.onPopState, false)
addEventListener("load", this.onPageLoad, false)
this.currentIndex = history.state?.turbo?.restorationIndex || 0
this.started = true
this.replace(new URL(window.location.href))
Expand All @@ -25,7 +23,6 @@ export class History {
stop() {
if (this.started) {
removeEventListener("popstate", this.onPopState, false)
removeEventListener("load", this.onPageLoad, false)
this.started = false
}
}
Expand Down Expand Up @@ -81,32 +78,17 @@ export class History {
// Event handlers

onPopState = (event) => {
if (this.shouldHandlePopState()) {
const { turbo } = event.state || {}
if (turbo) {
this.location = new URL(window.location.href)
const { restorationIdentifier, restorationIndex } = turbo
this.restorationIdentifier = restorationIdentifier
const direction = restorationIndex > this.currentIndex ? "forward" : "back"
this.delegate.historyPoppedToLocationWithRestorationIdentifierAndDirection(this.location, restorationIdentifier, direction)
this.currentIndex = restorationIndex
}
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)
}
}

onPageLoad = async (_event) => {
await nextMicrotask()
this.pageLoaded = true
}

// Private

shouldHandlePopState() {
// Safari dispatches a popstate event after window's load event, ignore it
return this.pageIsLoaded()
}

pageIsLoaded() {
return this.pageLoaded || document.readyState == "complete"
}
}
18 changes: 4 additions & 14 deletions src/core/drive/navigator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getVisitAction } from "../../util"
import { FormSubmission } from "./form_submission"
import { expandURL, getAnchor, getRequestURL } from "../url"
import { expandURL } from "../url"
import { Visit } from "./visit"
import { PageSnapshot } from "./page_snapshot"

Expand Down Expand Up @@ -128,20 +128,10 @@ export class Navigator {
delete this.currentVisit
}

// Same-page links are no longer handled with a Visit.
// This method is still needed for Turbo Native adapters.
locationWithActionIsSamePage(location, action) {
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, newURL) {
this.delegate.visitScrolledToSamePageLocation(oldURL, newURL)
return false
}

// Visits
Expand Down
25 changes: 2 additions & 23 deletions src/core/drive/visit.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export class Visit {
this.snapshot = snapshot
this.snapshotHTML = snapshotHTML
this.response = response
this.isSamePage = this.delegate.locationWithActionIsSamePage(this.location, this.action)
this.isPageRefresh = this.view.isPageRefresh(this)
this.visitCachedSnapshot = visitCachedSnapshot
this.willRender = willRender
Expand All @@ -110,10 +109,6 @@ export class Visit {
return this.history.getRestorationDataForIdentifier(this.restorationIdentifier)
}

get silent() {
return this.isSamePage
}

start() {
if (this.state == VisitState.initialized) {
this.recordTimingMetric(TimingMetric.visitStart)
Expand Down Expand Up @@ -250,7 +245,7 @@ export class Visit {
const isPreview = this.shouldIssueRequest()
this.render(async () => {
this.cacheSnapshot()
if (this.isSamePage || this.isPageRefresh) {
if (this.isPageRefresh) {
this.adapter.visitRendered(this)
} else {
if (this.view.renderPromise) await this.view.renderPromise
Expand Down Expand Up @@ -278,17 +273,6 @@ export class Visit {
}
}

goToSamePageAnchor() {
if (this.isSamePage) {
this.render(async () => {
this.cacheSnapshot()
this.performScroll()
this.changeHistory()
this.adapter.visitRendered(this)
})
}
}

// Fetch request delegate

prepareRequest(request) {
Expand Down Expand Up @@ -350,9 +334,6 @@ export class Visit {
} else {
this.scrollToAnchor() || this.view.scrollToTop()
}
if (this.isSamePage) {
this.delegate.visitScrolledToSamePageLocation(this.view.lastRenderedLocation, this.location)
}

this.scrolled = true
}
Expand Down Expand Up @@ -401,9 +382,7 @@ export class Visit {
}

shouldIssueRequest() {
if (this.isSamePage) {
return false
} else if (this.action == "restore") {
if (this.action == "restore") {
return !this.hasCachedSnapshot()
} else {
return this.willRender
Expand Down
1 change: 0 additions & 1 deletion src/core/native/browser_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export class BrowserAdapter {
this.location = visit.location
visit.loadCachedSnapshot()
visit.issueRequest()
visit.goToSamePageAnchor()
}

visitRequestStarted(visit) {
Expand Down
37 changes: 13 additions & 24 deletions 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 All @@ -249,9 +253,7 @@ export class Session {
this.view.markVisitDirection(visit.direction)
}
extendURLWithDeprecatedProperties(visit.location)
if (!visit.silent) {
this.notifyApplicationAfterVisitingLocation(visit.location, visit.action)
}
this.notifyApplicationAfterVisitingLocation(visit.location, visit.action)
}

visitCompleted(visit) {
Expand All @@ -260,14 +262,6 @@ export class Session {
this.notifyApplicationAfterPageLoad(visit.getTimingMetrics())
}

locationWithActionIsSamePage(location, action) {
return this.navigator.locationWithActionIsSamePage(location, action)
}

visitScrolledToSamePageLocation(oldURL, newURL) {
this.notifyApplicationAfterVisitingSamePageLocation(oldURL, newURL)
}

// Form submit observer delegate

willSubmitForm(form, submitter) {
Expand Down Expand Up @@ -307,9 +301,7 @@ export class Session {
// Page view delegate

viewWillCacheSnapshot() {
if (!this.navigator.currentVisit?.silent) {
this.notifyApplicationBeforeCachingSnapshot()
}
this.notifyApplicationBeforeCachingSnapshot()
}

allowsImmediateRender({ element }, options) {
Expand Down Expand Up @@ -401,15 +393,6 @@ export class Session {
})
}

notifyApplicationAfterVisitingSamePageLocation(oldURL, newURL) {
dispatchEvent(
new HashChangeEvent("hashchange", {
oldURL: oldURL.toString(),
newURL: newURL.toString()
})
)
}

notifyApplicationAfterFrameLoad(frame) {
return dispatch("turbo:frame-load", { target: frame })
}
Expand Down Expand Up @@ -469,6 +452,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
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export function doesNotTargetIFrame(name) {
}

export function findLinkFromClickTarget(target) {
return findClosestRecursively(target, "a[href]:not([target^=_]):not([download])")
return findClosestRecursively(target, "a[href]:not([href^='#']):not([target^=_]):not([download])")
}

export function getLocationForLink(link) {
Expand Down
Loading