Skip to content

Commit

Permalink
Router base path (#5630)
Browse files Browse the repository at this point in the history
* The undocumented Router protocol currently only allows for specifyingrelative paths. This change makes it possible to point to an entirely different base url (which is needed for portals for capacitor)

* chore: fix added router test

* chore: Move router call in the else branch of WebViewAssetHandler. No need to run the method if it's not needed.
  • Loading branch information
Steven0351 committed May 19, 2022
1 parent 5fac1b2 commit 9e2a5e4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
6 changes: 4 additions & 2 deletions ios/Capacitor/Capacitor/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ import Foundation

public protocol Router {
func route(for path: String) -> String
var basePath: String { get set }
}

// swiftlint:disable:next type_name
internal struct _Router: Router {
var basePath: String = ""
func route(for path: String) -> String {
let pathUrl = URL(fileURLWithPath: path)

// If there's no path extension it also means the path is empty or a SPA route
if pathUrl.pathExtension.isEmpty {
return "/index.html"
return basePath + "/index.html"
}

return path
return basePath + path
}
}
10 changes: 4 additions & 6 deletions ios/Capacitor/Capacitor/WebViewAssetHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@ import MobileCoreServices

@objc(CAPWebViewAssetHandler)
internal class WebViewAssetHandler: NSObject, WKURLSchemeHandler {
private let router: Router
private var basePath: String = ""
private var router: Router

init(router: Router) {
self.router = router
super.init()
}

func setAssetPath(_ assetPath: String) {
self.basePath = assetPath
router.basePath = assetPath
}

func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
var startPath = self.basePath
let startPath: String
let url = urlSchemeTask.request.url!
let stringToLoad = url.path

let resolvedRoute = router.route(for: stringToLoad)
if stringToLoad.starts(with: CapacitorBridge.fileStartIdentifier) {
startPath = stringToLoad.replacingOccurrences(of: CapacitorBridge.fileStartIdentifier, with: "")
} else {
startPath.append(resolvedRoute)
startPath = router.route(for: stringToLoad)
}

let localUrl = URL.init(string: url.absoluteString)!
Expand Down
19 changes: 14 additions & 5 deletions ios/Capacitor/CapacitorTests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@ import XCTest
@testable import Capacitor

class RouterTests: XCTestCase {
let router = _Router()

func testRouterReturnsIndexWhenProvidedEmptyPath() {
XCTAssertEqual(router.route(for: ""), "/index.html")
checkRouter(path: "", expected: "/index.html")
}

func testRouterReturnsIndexWhenProviedPathWithoutExtension() {
XCTAssertEqual(router.route(for: "/a/valid/path/no/ext"), "/index.html")
checkRouter(path: "/a/valid/path/no/ext", expected: "/index.html")
}

func testRouterReturnsPathWhenProvidedValidPath() {
XCTAssertEqual(router.route(for: "/a/valid/path.ext"), "/a/valid/path.ext")
checkRouter(path: "/a/valid/path.ext", expected: "/a/valid/path.ext")
}

func testRouterReturnsPathWhenProvidedValidPathWithExtensionAndSpaces() {
XCTAssertEqual(router.route(for: "/a/valid/file path.ext"), "/a/valid/file path.ext")
checkRouter(path: "/a/valid/file path.ext", expected: "/a/valid/file path.ext")
}

func checkRouter(path: String, expected: String) {
XCTContext.runActivity(named: "router creates route path correctly") { _ in
var router = _Router()
XCTAssertEqual(router.route(for: path), expected)
router.basePath = "/A/Route"
XCTAssertEqual(router.route(for: path), "/A/Route" + expected)
}
}

}

0 comments on commit 9e2a5e4

Please sign in to comment.