Skip to content

Commit

Permalink
Merge pull request #178 from hotwired/turbo-8-refresh
Browse files Browse the repository at this point in the history
Support Turbo 8 same-page refreshing + morphing
  • Loading branch information
jayohms authored Feb 23, 2024
2 parents 883f32d + 47e393b commit 60577a5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Source/Session/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,16 @@ extension Session: VisitDelegate {
}

func visitWillStart(_ visit: Visit) {
guard !visit.isPageRefresh else { return }

visit.visitable.showVisitableScreenshot()
activateVisitable(visit.visitable)
}

func visitDidStart(_ visit: Visit) {
guard !visit.hasCachedSnapshot else { return }
guard !visit.isPageRefresh else { return }

visit.visitable.showVisitableActivityIndicator()
}

Expand Down
5 changes: 3 additions & 2 deletions Source/Visit/JavaScriptVisit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ final class JavaScriptVisit: Visit {
}

extension JavaScriptVisit: WebViewVisitDelegate {
func webView(_ webView: WebViewBridge, didStartVisitWithIdentifier identifier: String, hasCachedSnapshot: Bool) {
log("didStartVisitWithIdentifier", ["identifier": identifier, "hasCachedSnapshot": hasCachedSnapshot])
func webView(_ webView: WebViewBridge, didStartVisitWithIdentifier identifier: String, hasCachedSnapshot: Bool, isPageRefresh: Bool) {
log("didStartVisitWithIdentifier", ["identifier": identifier, "hasCachedSnapshot": hasCachedSnapshot, "isPageRefresh": isPageRefresh])
self.identifier = identifier
self.hasCachedSnapshot = hasCachedSnapshot
self.isPageRefresh = isPageRefresh

delegate?.visitDidStart(self)
}
Expand Down
1 change: 1 addition & 0 deletions Source/Visit/Visit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Visit: NSObject {
let location: URL

var hasCachedSnapshot: Bool = false
var isPageRefresh: Bool = false
private(set) var state: VisitState

init(visitable: Visitable, options: VisitOptions, bridge: WebViewBridge) {
Expand Down
2 changes: 2 additions & 0 deletions Source/WebView/ScriptMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ extension ScriptMessage {
case pageLoadFailed
case errorRaised
case visitProposed
case visitProposalScrollingToAnchor
case visitProposalRefreshingPage
case visitStarted
case visitRequestStarted
case visitRequestCompleted
Expand Down
8 changes: 6 additions & 2 deletions Source/WebView/WebViewBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protocol WebViewPageLoadDelegate: AnyObject {
}

protocol WebViewVisitDelegate: AnyObject {
func webView(_ webView: WebViewBridge, didStartVisitWithIdentifier identifier: String, hasCachedSnapshot: Bool)
func webView(_ webView: WebViewBridge, didStartVisitWithIdentifier identifier: String, hasCachedSnapshot: Bool, isPageRefresh: Bool)
func webView(_ webView: WebViewBridge, didStartRequestForVisitWithIdentifier identifier: String, date: Date)
func webView(_ webView: WebViewBridge, didCompleteRequestForVisitWithIdentifier identifier: String)
func webView(_ webView: WebViewBridge, didFailRequestForVisitWithIdentifier identifier: String, statusCode: Int)
Expand Down Expand Up @@ -125,8 +125,12 @@ extension WebViewBridge: ScriptMessageHandlerDelegate {
delegate?.webViewDidInvalidatePage(self)
case .visitProposed:
delegate?.webView(self, didProposeVisitToLocation: message.location!, options: message.options!)
case .visitProposalScrollingToAnchor:
break
case .visitProposalRefreshingPage:
break
case .visitStarted:
visitDelegate?.webView(self, didStartVisitWithIdentifier: message.identifier!, hasCachedSnapshot: message.data["hasCachedSnapshot"] as! Bool)
visitDelegate?.webView(self, didStartVisitWithIdentifier: message.identifier!, hasCachedSnapshot: message.data["hasCachedSnapshot"] as! Bool, isPageRefresh: message.data["isPageRefresh"] as! Bool)
case .visitRequestStarted:
visitDelegate?.webView(self, didStartRequestForVisitWithIdentifier: message.identifier!, date: message.date)
case .visitRequestCompleted:
Expand Down
20 changes: 12 additions & 8 deletions Source/WebView/turbo.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,18 @@
// Adapter interface

visitProposedToLocation(location, options) {
if (window.Turbo && typeof Turbo.navigator.locationWithActionIsSamePage === "function") {
if (Turbo.navigator.locationWithActionIsSamePage(location, options.action)) {
Turbo.navigator.view.scrollToAnchorFromLocation(location)
return
}
if (window.Turbo && Turbo.navigator.locationWithActionIsSamePage(location, options.action)) {
// Scroll to the anchor on the page
this.postMessage("visitProposalScrollingToAnchor", { location: location.toString(), options: options })
Turbo.navigator.view.scrollToAnchorFromLocation(location)
} else if (window.Turbo && Turbo.navigator.location?.href === location.href) {
// Refresh the page without native proposal
this.postMessage("visitProposalRefreshingPage", { location: location.toString(), options: options })
this.visitLocationWithOptionsAndRestorationIdentifier(location, options, Turbo.navigator.restorationIdentifier)
} else {
// Propose the visit
this.postMessage("visitProposed", { location: location.toString(), options: options })
}

this.postMessage("visitProposed", { location: location.toString(), options: options })
}

// Turbolinks 5
Expand All @@ -116,7 +120,7 @@

visitStarted(visit) {
this.currentVisit = visit
this.postMessage("visitStarted", { identifier: visit.identifier, hasCachedSnapshot: visit.hasCachedSnapshot() })
this.postMessage("visitStarted", { identifier: visit.identifier, hasCachedSnapshot: visit.hasCachedSnapshot(), isPageRefresh: visit.isPageRefresh || false })
this.issueRequestForVisitWithIdentifier(visit.identifier)
this.changeHistoryForVisitWithIdentifier(visit.identifier)
this.loadCachedSnapshotForVisitWithIdentifier(visit.identifier)
Expand Down

0 comments on commit 60577a5

Please sign in to comment.