Skip to content

Load the URL of WebView only when it changed - #18

Merged
Jeehut merged 3 commits into
mainfrom
wip/webview-load-on-url-change
Oct 15, 2026
Merged

Jeehut merged 3 commits into
mainfrom
wip/webview-load-on-url-change

Conversation

@Jeehut

@Jeehut Jeehut commented Oct 14, 2026

Copy link
Copy Markdown
Member

Overview

WebView called webView.load(…) in every updateUIView/updateNSView, i.e. on every state change of the surrounding view. That reset the page the user had navigated to and the scroll position. The coordinator now remembers the loaded URL and loads only when it changed. It also updates parent, so a new onOpenExternalURL closure takes effect.

Split out of #7.

How this was verified

WebViewTests call the representable's real update(_:coordinator:) with a WKWebView subclass that records load requests. Three updates with the same URL load once; a URL change loads the new URL once and updates the coordinator's parent.

main has no update(_:coordinator:), so for the red run the URL check was removed, which is main's behavior (always load):

✘ repeatedUpdatesLoadTheURLOnlyOnce(): webView.loadedURLs == [self.pageURL]
✘ updateWithChangedURLLoadsTheNewURL(): webView.loadedURLs == [self.pageURL, self.otherPageURL]
✘ Test run with 2 tests in 1 suite failed with 2 issues.

Green, this branch:

✔ Test run with 2 tests in 1 suite passed.

SwiftUI calls updateUIView/updateNSView on every state change of the
surrounding view. WebView loaded its URL in there each time, which
threw away pages the user had navigated to and reset the scroll
position. The coordinator now remembers the loaded URL and also keeps
its parent current, so a changed onOpenExternalURL closure is used.
The tests now call the representable's update method with a web view
that records load requests, so they cover the loaded-URL bookkeeping
and the delegate and parent updates, not only the comparison. The
suite runs on the main actor, which removes the isolation warning.
The helper only wrapped an inequality check and is no longer needed by
the tests, which drive the update path directly.
@Jeehut
Jeehut merged commit b202eba into main Oct 15, 2026
5 checks passed
@Jeehut
Jeehut deleted the wip/webview-load-on-url-change branch October 15, 2026 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant