Skip to content

Commit

Permalink
Fix snapshot cache issue in Turbo Navigator (#183)
Browse files Browse the repository at this point in the history
* Public functions to clear snapshot cache or reload

* Fix snapshot cache issue take 3
  • Loading branch information
joemasilotti authored Feb 28, 2024
1 parent cbb7356 commit 0ee7745
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Source/Session/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Turbo Navigator/TurboNavigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0ee7745

Please sign in to comment.