From 0ee7745a7881be89adb9f4696cf5f848815ba3f7 Mon Sep 17 00:00:00 2001 From: Joe Masilotti Date: Wed, 28 Feb 2024 13:38:06 -0800 Subject: [PATCH] Fix snapshot cache issue in Turbo Navigator (#183) * Public functions to clear snapshot cache or reload * Fix snapshot cache issue take 3 --- Source/Session/Session.swift | 20 ++++++++++++++++++++ Source/Turbo Navigator/TurboNavigator.swift | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Source/Session/Session.swift b/Source/Session/Session.swift index bcfdab3..f9ef2fc 100644 --- a/Source/Session/Session.swift +++ b/Source/Session/Session.swift @@ -14,6 +14,8 @@ public class Session: NSObject { private lazy var bridge = WebViewBridge(webView: webView) private var initialized = false private var refreshing = false + private var isShowingStaleContent = false + private var isSnapshotCacheStale = false /// Automatically creates a web view with the passed-in configuration public convenience init(webViewConfiguration: WKWebViewConfiguration? = nil) { @@ -91,6 +93,18 @@ public class Session: NSObject { bridge.clearSnapshotCache() } + // MARK: Caching + + /// Clear the snapshot cache the next time the visitable view appears. + public func markSnapshotCacheAsStale() { + isSnapshotCacheStale = true + } + + /// Reload the `Session` the next time the visitable view appears. + public func markContentAsStale() { + isShowingStaleContent = true + } + // MARK: Visitable activation private var activatedVisitable: Visitable? @@ -228,6 +242,12 @@ extension Session: VisitableDelegate { } else if visitable !== topmostVisit.visitable { // Navigating backward visit(visitable, action: .restore) + } else if isShowingStaleContent { + reload() + isShowingStaleContent = false + } else if isSnapshotCacheStale { + clearSnapshotCache() + isSnapshotCacheStale = false } } diff --git a/Source/Turbo Navigator/TurboNavigator.swift b/Source/Turbo Navigator/TurboNavigator.swift index a23cf45..082ae6e 100644 --- a/Source/Turbo Navigator/TurboNavigator.swift +++ b/Source/Turbo Navigator/TurboNavigator.swift @@ -143,7 +143,7 @@ extension TurboNavigator: SessionDelegate { public func sessionDidFinishFormSubmission(_ session: Session) { if session == modalSession { - self.session.clearSnapshotCache() + self.session.markSnapshotCacheAsStale() } if let url = session.topmostVisitable?.visitableURL { delegate.formSubmissionDidFinish(at: url)