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)