Skip to content
This repository has been archived by the owner on Sep 25, 2021. It is now read-only.

Update Visitable.visitableURL after following a redirect #156

Open
wants to merge 1 commit into
base: master
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
8 changes: 6 additions & 2 deletions Turbolinks/Visit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,10 @@ class ColdBootVisit: Visit, WKNavigationDelegate, WebViewPageLoadDelegate {

// MARK: WebViewPageLoadDelegate

func webView(_ webView: WebView, didLoadPageWithRestorationIdentifier restorationIdentifier: String) {
func webView(_ webView: WebView, didLoadPageWithRestorationIdentifier restorationIdentifier: String, location: URL) {
self.restorationIdentifier = restorationIdentifier
self.location = location
visitable.visitableURL = location
delegate?.visitDidRender(self)
complete()
}
Expand Down Expand Up @@ -314,9 +316,11 @@ class JavaScriptVisit: Visit, WebViewVisitDelegate {
}
}

func webView(_ webView: WebView, didCompleteVisitWithIdentifier identifier: String, restorationIdentifier: String) {
func webView(_ webView: WebView, didCompleteVisitWithIdentifier identifier: String, restorationIdentifier: String, location: URL) {
if identifier == self.identifier {
self.restorationIdentifier = restorationIdentifier
self.location = location
visitable.visitableURL = location
complete()
}
}
Expand Down
2 changes: 1 addition & 1 deletion Turbolinks/Visitable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public protocol VisitableDelegate: class {
public protocol Visitable: class {
var visitableDelegate: VisitableDelegate? { get set }
var visitableView: VisitableView! { get }
var visitableURL: URL! { get }
var visitableURL: URL! { get set }
func visitableDidRender()
}

Expand Down
6 changes: 4 additions & 2 deletions Turbolinks/WebView.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
WebView.prototype = {
pageLoaded: function() {
var restorationIdentifier = this.controller.restorationIdentifier
this.postMessageAfterNextRepaint("pageLoaded", { restorationIdentifier: restorationIdentifier })
var location = this.controller.location
this.postMessageAfterNextRepaint("pageLoaded", { restorationIdentifier: restorationIdentifier, location: location.absoluteURL })
},

errorRaised: function(error) {
Expand Down Expand Up @@ -83,7 +84,8 @@
},

visitCompleted: function(visit) {
this.postMessageAfterNextRepaint("visitCompleted", { identifier: visit.identifier, restorationIdentifier: visit.restorationIdentifier })
visit.followRedirect()
this.postMessageAfterNextRepaint("visitCompleted", { identifier: visit.identifier, restorationIdentifier: visit.restorationIdentifier, location: visit.location.absoluteURL })
},

pageInvalidated: function() {
Expand Down
8 changes: 4 additions & 4 deletions Turbolinks/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ protocol WebViewDelegate: class {
}

protocol WebViewPageLoadDelegate: class {
func webView(_ webView: WebView, didLoadPageWithRestorationIdentifier restorationIdentifier: String)
func webView(_ webView: WebView, didLoadPageWithRestorationIdentifier restorationIdentifier: String, location: URL)
}

protocol WebViewVisitDelegate: class {
Expand All @@ -17,7 +17,7 @@ protocol WebViewVisitDelegate: class {
func webView(_ webView: WebView, didFailRequestForVisitWithIdentifier identifier: String, statusCode: Int)
func webView(_ webView: WebView, didFinishRequestForVisitWithIdentifier identifier: String)
func webView(_ webView: WebView, didRenderForVisitWithIdentifier identifier: String)
func webView(_ webView: WebView, didCompleteVisitWithIdentifier identifier: String, restorationIdentifier: String)
func webView(_ webView: WebView, didCompleteVisitWithIdentifier identifier: String, restorationIdentifier: String, location: URL)
}

class WebView: WKWebView {
Expand Down Expand Up @@ -126,7 +126,7 @@ extension WebView: WKScriptMessageHandler {

switch message.name {
case .PageLoaded:
pageLoadDelegate?.webView(self, didLoadPageWithRestorationIdentifier: message.restorationIdentifier!)
pageLoadDelegate?.webView(self, didLoadPageWithRestorationIdentifier: message.restorationIdentifier!, location: message.location!)
case .PageInvalidated:
delegate?.webViewDidInvalidatePage(self)
case .VisitProposed:
Expand All @@ -144,7 +144,7 @@ extension WebView: WKScriptMessageHandler {
case .VisitRendered:
visitDelegate?.webView(self, didRenderForVisitWithIdentifier: message.identifier!)
case .VisitCompleted:
visitDelegate?.webView(self, didCompleteVisitWithIdentifier: message.identifier!, restorationIdentifier: message.restorationIdentifier!)
visitDelegate?.webView(self, didCompleteVisitWithIdentifier: message.identifier!, restorationIdentifier: message.restorationIdentifier!, location: message.location!)
case .ErrorRaised:
let error = message.data["error"] as? String
NSLog("JavaScript error: %@", error ?? "<unknown error>")
Expand Down