Work around WKWebView latency that happens when specifying baseURL. Instead, make it nil, and add a base href tag to the actual HTML.

This commit is contained in:
Brent Simmons 2019-04-14 12:54:17 -07:00
parent 463f17f600
commit 6c8481d805
2 changed files with 37 additions and 34 deletions

View File

@ -16,11 +16,13 @@ struct ArticleRenderer {
private let article: Article?
private let articleStyle: ArticleStyle
private let title: String
private let baseURL: String?
private init(article: Article?, style: ArticleStyle) {
self.article = article
self.articleStyle = style
self.title = article?.title ?? ""
self.baseURL = article?.baseURL?.absoluteString
}
// MARK: - API
@ -309,6 +311,9 @@ private extension ArticleRenderer {
func renderHTML(withBody body: String) -> String {
var s = "<!DOCTYPE html><html><head>\n\n"
if let baseURL = baseURL {
s += ("<base href=\"" + baseURL + "\"\n>")
}
s += title.htmlBySurroundingWithTag("title")
s += styleString().htmlBySurroundingWithTag("style")
@ -345,3 +350,34 @@ private extension ArticleRenderer {
return s
}
}
// MARK: - Article extension
private extension Article {
var baseURL: URL? {
var s = url
if s == nil {
s = feed?.homePageURL
}
if s == nil {
s = feed?.url
}
guard let urlString = s else {
return nil
}
var urlComponents = URLComponents(string: urlString)
if urlComponents == nil {
return nil
}
// Cant use url-with-fragment as base URL. The webview wont load. See scripting.com/rss.xml for example.
urlComponents!.fragment = nil
guard let url = urlComponents!.url, url.scheme == "http" || url.scheme == "https" else {
return nil
}
return url
}
}

View File

@ -116,7 +116,6 @@ private extension DetailWebViewController {
func reloadHTML() {
let style = ArticleStylesManager.shared.currentStyle
let html: String
var baseURL: URL? = nil
switch state {
case .noSelection:
@ -125,10 +124,9 @@ private extension DetailWebViewController {
html = ArticleRenderer.multipleSelectionHTML(style: style)
case .article(let article):
html = ArticleRenderer.articleHTML(article: article, style: style)
baseURL = article.baseURL
}
webview.loadHTMLString(html, baseURL: baseURL)
webview.loadHTMLString(html, baseURL: nil)
}
func fetchScrollInfo(_ callback: @escaping (ScrollInfo?) -> Void) {
@ -150,37 +148,6 @@ private extension DetailWebViewController {
}
}
// MARK: - Article extension
private extension Article {
var baseURL: URL? {
var s = url
if s == nil {
s = feed?.homePageURL
}
if s == nil {
s = feed?.url
}
guard let urlString = s else {
return nil
}
var urlComponents = URLComponents(string: urlString)
if urlComponents == nil {
return nil
}
// Cant use url-with-fragment as base URL. The webview wont load. See scripting.com/rss.xml for example.
urlComponents!.fragment = nil
guard let url = urlComponents!.url, url.scheme == "http" || url.scheme == "https" else {
return nil
}
return url
}
}
// MARK: - ScrollInfo
private struct ScrollInfo {