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:
parent
463f17f600
commit
6c8481d805
|
@ -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
|
||||
}
|
||||
|
||||
// Can’t use url-with-fragment as base URL. The webview won’t 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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
// Can’t use url-with-fragment as base URL. The webview won’t 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 {
|
||||
|
|
Loading…
Reference in New Issue