From 0b891ffb1e293650a967b284ef2c4e39c46d5daa Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Wed, 25 Mar 2026 04:07:55 -0700 Subject: [PATCH] Fix browser pane restore after reopen --- Sources/Panels/BrowserPanel.swift | 43 +++++++++++++++++++++++++++++-- Sources/Workspace.swift | 11 +++----- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/Sources/Panels/BrowserPanel.swift b/Sources/Panels/BrowserPanel.swift index f0bb3778ac95..47d7ae1f3851 100644 --- a/Sources/Panels/BrowserPanel.swift +++ b/Sources/Panels/BrowserPanel.swift @@ -2987,16 +2987,41 @@ final class BrowserPanel: Panel, ObservableObject { ) { let restoredBack = Self.sanitizedSessionHistoryURLs(backHistoryURLStrings) let restoredForward = Self.sanitizedSessionHistoryURLs(forwardHistoryURLStrings) - guard !restoredBack.isEmpty || !restoredForward.isEmpty else { return } + let restoredCurrent = Self.sanitizedSessionHistoryURL(currentURLString) + guard !restoredBack.isEmpty || !restoredForward.isEmpty || restoredCurrent != nil else { return } usesRestoredSessionHistory = true restoredBackHistoryStack = restoredBack // Store nearest-forward entries at the end to make stack pop operations trivial. restoredForwardHistoryStack = Array(restoredForward.reversed()) - restoredHistoryCurrentURL = Self.sanitizedSessionHistoryURL(currentURLString) + restoredHistoryCurrentURL = restoredCurrent refreshNavigationAvailability() } + func restoreSessionSnapshot(_ snapshot: SessionBrowserPanelSnapshot) { + let restoredURL = Self.sanitizedSessionHistoryURL(snapshot.urlString) + + restoreSessionNavigationHistory( + backHistoryURLStrings: snapshot.backHistoryURLStrings ?? [], + forwardHistoryURLStrings: snapshot.forwardHistoryURLStrings ?? [], + currentURLString: snapshot.urlString + ) + + currentURL = snapshot.shouldRenderWebView ? restoredURL : nil + shouldRenderWebView = snapshot.shouldRenderWebView + + guard snapshot.shouldRenderWebView, let restoredURL else { + refreshNavigationAvailability() + return + } + + navigateWithoutInsecureHTTPPrompt( + to: restoredURL, + recordTypedNavigation: false, + preserveRestoredSessionHistory: true + ) + } + private func setupObservers(for webView: WKWebView) { let observedWebViewInstanceID = webViewInstanceID @@ -4132,6 +4157,20 @@ extension BrowserPanel { /// Reload the current page func reload() { webView.customUserAgent = BrowserUserAgentSettings.safariUserAgent + if Self.serializableSessionHistoryURLString(Self.remoteProxyDisplayURL(for: webView.url)) == nil { + let fallbackURL = resolvedCurrentSessionHistoryURL() + ?? Self.remoteProxyDisplayURL(for: navigationDelegate?.lastAttemptedURL) + + if let fallbackURL, + Self.serializableSessionHistoryURLString(fallbackURL) != nil { + navigateWithoutInsecureHTTPPrompt( + to: fallbackURL, + recordTypedNavigation: false, + preserveRestoredSessionHistory: usesRestoredSessionHistory + ) + return + } + } webView.reload() } diff --git a/Sources/Workspace.swift b/Sources/Workspace.swift index 6bfab1c7c3d4..8a231de4d280 100644 --- a/Sources/Workspace.swift +++ b/Sources/Workspace.swift @@ -596,10 +596,9 @@ extension Workspace { applySessionPanelMetadata(snapshot, toPanelId: terminalPanel.id) return terminalPanel.id case .browser: - let initialURL = snapshot.browser?.urlString.flatMap { URL(string: $0) } guard let browserPanel = newBrowserSurface( inPane: paneId, - url: initialURL, + url: nil, focus: false, preferredProfileID: snapshot.browser?.profileID ) else { @@ -655,17 +654,13 @@ extension Workspace { if let browserSnapshot = snapshot.browser, let browserPanel = browserPanel(for: panelId) { - browserPanel.restoreSessionNavigationHistory( - backHistoryURLStrings: browserSnapshot.backHistoryURLStrings ?? [], - forwardHistoryURLStrings: browserSnapshot.forwardHistoryURLStrings ?? [], - currentURLString: browserSnapshot.urlString - ) - let pageZoom = CGFloat(max(0.25, min(5.0, browserSnapshot.pageZoom))) if pageZoom.isFinite { _ = browserPanel.setPageZoomFactor(pageZoom) } + browserPanel.restoreSessionSnapshot(browserSnapshot) + if browserSnapshot.developerToolsVisible { _ = browserPanel.showDeveloperTools() browserPanel.requestDeveloperToolsRefreshAfterNextAttach(reason: "session_restore")