Skip to content

WebView2 on WebAssembly raises NavigationCompleted before NavigationStarting, so each navigation completes twice #25806

Open

Description

@mikernet

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 the NavigationId of the previous navigation (0 for the first one);
  • reports about:blank, with IsSuccess true and HttpStatusCode 200 (it reads the frame's src attribute, which Navigate doesn't set, and falls back to about:blank);
  • sets CoreWebView2.Source to about:blank. If Source held another URL, SourceChanged is raised, and CoreWebView2.Source and WebView2.Source stay about:blank until 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 ProcessNavigation overloads schedule NavigationStarting on the dispatcher, then call OnNavigationCompleted(this, null) right away (L157-L158, L163-L164; ScheduleNavigationStarting at L117-L128).
  • With a null URL, OnNavigationCompleted falls back to the frame's src attribute, or about:blank (L87-L103). navigate() in WebView.ts sets contentWindow.location.href, not src (L48-L60), so it's about:blank.
  • CoreWebView2.RaiseNavigationCompleted sets Source and uses the current _navigationId (L243-L251), which only RaiseNavigationStarting increments (L228).
  • The real event comes from the frame's load handler (onLoad in WebView.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

  1. mikernet commented on Oct 29, 2026

    @mikernet
    ContributorAuthor

    One more case of the same kind: adding a loaded WebView2 to the tree again (removing it and adding it back, for example to move it to another parent) also raises NavigationCompleted, although nothing navigated. WinUI raises nothing then.

    • Uno: NativeWebView.OnLoaded (NativeWebView.wasm.cs L187-L192) calls DispatchLoadEvent(_elementId, null) on every Loaded, which raises NavigationCompleted with IsSuccess true, status 200 and the current NavigationId. With no URL passed, it reads the frame's src attribute, which WebView2 doesn't set, so it reports about:blank and sets Source back to about:blank while the frame still shows its page.
    • WinUI: WebView2::OnLoaded only calls TryCompleteInitialization(); FireNavigationCompleted is called only from the CoreWebView2.NavigationCompleted handler (src/controls/dev/WebView2/WebView2.cpp).
    • A runtime test on Skia WebAssembly (headless Chrome 156) that navigated a WebView2, set WindowContent to null and loaded the control again saw Source go back to about:blank, which only this NavigationCompleted sets.

    #25737 keeps Source right here (it reports the frame's current URL instead of the src attribute) but still raises the event. #23754's OnLoaded only 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions