Repository navigation
Description
Current behavior 🐛
On WebAssembly, CoreWebView2.Navigate (also used when WebView2.Source is set) and NavigateToString raise NavigationCompleted synchronously, before NavigationStarting and before the frame starts loading. When the frame has loaded, NavigationCompleted is raised a second time, for the actual document. So each navigation completes twice.
The first, early NavigationCompleted:
- comes before
NavigationStarting, with theNavigationIdof the previous navigation (0 for the first one); - reports
about:blank, withIsSuccesstrue andHttpStatusCode200 (it reads the frame'ssrcattribute, whichNavigatedoesn't set, and falls back toabout:blank); - sets
CoreWebView2.Sourcetoabout:blank. IfSourceheld another URL,SourceChangedis raised, andCoreWebView2.SourceandWebView2.Sourcestayabout:blankuntil the new page has loaded.
A runtime test on Skia WebAssembly (headless Chrome) on master at b40e235 recorded the NavigationCompleted events per navigation. The code involved is the same at 4744356.
| Navigation | NavigationCompleted raised |
|---|---|
| Same-origin page | the early one with about:blank, then one for the page |
NavigateToString |
the early one with about:blank, then one for about:srcdoc |
Cross-origin page or data: URL |
only the early one (the second one is missing because of #25735) |
Expected behavior 🎯
WebView2 raises one NavigationStarting and one NavigationCompleted per navigation, in that order and with the same NavigationId. The documented sequence is: NavigationStarting, SourceChanged, ContentLoading, HistoryChanged, DOMContentLoaded, NavigationCompleted. NavigationCompleted comes when the content has loaded, and reports the URL of the loaded document.
How to reproduce it (as minimally and precisely as possible) 🔬
In a WebAssembly app:
var webView = new WebView2 { Width = 400, Height = 300 };
// Add webView to the visual tree, then:
await webView.EnsureCoreWebView2Async();
webView.NavigationStarting += (s, e) => Console.WriteLine($"NavigationStarting #{e.NavigationId}");
webView.NavigationCompleted += (s, e) => Console.WriteLine($"NavigationCompleted #{e.NavigationId} {s.CoreWebView2.Source}");
webView.NavigateToString("<p>Hello</p>");The console shows:
NavigationCompleted #0 about:blank
NavigationStarting #1
NavigationCompleted #1 about:srcdoc
Expected:
NavigationStarting #1
NavigationCompleted #1 about:srcdoc
CoreWebView2.Navigate to a same-origin page gives the same pattern, with the page's URL in the last line.
Workaround 🛠️
Ignore a NavigationCompleted whose NavigationId isn't that of the last NavigationStarting:
ulong? pendingNavigation = null;
webView.NavigationStarting += (s, e) => pendingNavigation = e.NavigationId;
webView.NavigationCompleted += (s, e) =>
{
if (e.NavigationId != pendingNavigation)
{
return; // early event on WebAssembly
}
pendingNavigation = null;
// ...
};Affected platforms 📱💻🖥️
WebAssembly
Uno.Sdk version (and other relevant versions) 📦
master at 4744356 (Uno 7.0 dev). The same code is on servicing/6.8.
IDE version 🧑💻
N/A
Anything else we need to know? 💬
The early event comes from NativeWebView.wasm.cs:
- Both
ProcessNavigationoverloads scheduleNavigationStartingon the dispatcher, then callOnNavigationCompleted(this, null)right away (L157-L158, L163-L164;ScheduleNavigationStartingat L117-L128). - With a
nullURL,OnNavigationCompletedfalls back to the frame'ssrcattribute, orabout:blank(L87-L103).navigate()inWebView.tssetscontentWindow.location.href, notsrc(L48-L60), so it'sabout:blank. CoreWebView2.RaiseNavigationCompletedsetsSourceand uses the current_navigationId(L243-L251), which onlyRaiseNavigationStartingincrements (L228).- The real event comes from the frame's
loadhandler (onLoadinWebView.ts, L100-L103).
NativeWebView.OnLoaded also raises NavigationCompleted, without a NavigationStarting, each time the WebView2 is loaded (L187-L192).
Nearby, but not part of this issue: for Navigate, NavigationStarting.Uri is a data:text/html;charset=utf-8;base64,… URI that wraps the target URL as if it were HTML content. ScheduleNavigationStarting passes the URL as a string, which RaiseNavigationStarting treats as HTML (L220-L226).
The open #23754 removes both early calls and the one in OnLoaded, as part of a larger change. The open #25737 and #25738 also change this file but keep these calls.
Activity
mikernet commentedon Oct 29, 2026
One more case of the same kind: adding a loaded
WebView2to the tree again (removing it and adding it back, for example to move it to another parent) also raisesNavigationCompleted, although nothing navigated. WinUI raises nothing then.NativeWebView.OnLoaded(NativeWebView.wasm.csL187-L192) callsDispatchLoadEvent(_elementId, null)on everyLoaded, which raisesNavigationCompletedwithIsSuccesstrue, status 200 and the currentNavigationId. With no URL passed, it reads the frame'ssrcattribute, whichWebView2doesn't set, so it reportsabout:blankand setsSourceback toabout:blankwhile the frame still shows its page.WebView2::OnLoadedonly callsTryCompleteInitialization();FireNavigationCompletedis called only from theCoreWebView2.NavigationCompletedhandler (src/controls/dev/WebView2/WebView2.cpp).WebView2, setWindowContenttonulland loaded the control again sawSourcego back toabout:blank, which only thisNavigationCompletedsets.#25737 keeps
Sourceright here (it reports the frame's current URL instead of thesrcattribute) but still raises the event. #23754'sOnLoadedonly registers the web view, so it removes this one along with the early event. Whoever removes it should keep the case this dispatch probably exists for: a load that finished before the native web view was registered.