MicrosoftEdge / MicrosoftEdge/WebView2Feedback

[Problem/Bug]: WebResourceResponseReceived for a new window doesn't fire for initial navigation

Open
#4,799 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

What happened?

I'm trying to detect if a window (result of window.open(...)) contains a PDF.

When I create my WebView2 control, I add a WebResourceResponseReceived event handler, but it doesn't fire when the WebView2 finishes navigating (to the PDF or to another site)

I have found that the only wait to get it to fire is to cancel the initial navigation and navigate it myself via CoreWebView2::Navigate(...) (code below)

Importance

Moderate. My app's user experience is affected, but still usable.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

128.0.2739.67

SDK Version

1.0.2535.41

Framework

Winforms

Operating System

Windows 11

OS Version

22631.4037

Repro steps

If you open the WinForms sample and:
In BrowserForm.cs::WebView2Control_CoreWebView2InitializationCompleted, add this.webView2Control.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;

	private async void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
	{
		e.Handled = true;
		using (e.GetDeferral())
		{
			Form form = new Form();
			WebView2 webView = new WebView2();
			await webView.EnsureCoreWebView2Async();
			form.Controls.Add(webView);
			form.Visible = true;
			webView.Dock = DockStyle.Fill;
			webView.CoreWebView2.WebResourceResponseReceived += CoreWebView2_WebResourceResponseReceived;
			webView.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;
			e.NewWindow = webView.CoreWebView2;
		}
	}

	private void CoreWebView2_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
	{
		// this fires
	}

	private void CoreWebView2_WebResourceResponseReceived(object sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
	{
		bool loadingPDF = e.Response.StatusCode == 200 && e.Response.Headers.Contains("Content-Type") && e.Response.Headers.GetHeader("Content-Type") == "application/pdf";
		// doesn't fire
	}
  1. start app
  2. open devtools
  3. window.open("https://google.com");
  4. the webresourceResponseReceived handler doesn't fire

For PDFs specifically, you can open a PDF to, say, https://www.sos.state.tx.us/corp/status-example.pdf

I have found that if I cancel the initial navigation and navigate it myself, it work fine. So in CoreWebView2_NewWindowRequested, add webView.CoreWebView2.NavigationStarting +=(sender2, e2) => CoreWebView2_NavigationStarting(webView, e2); and then add:

	private Dictionary<WebView2, bool> _initialNavigationStopped = new Dictionary<WebView2, bool>();

	private void CoreWebView2_NavigationStarting(WebView2 webView, CoreWebView2NavigationStartingEventArgs e)
	{
		if (!_initialNavigationStopped.ContainsKey(webView))
		{
			_initialNavigationStopped[webView] = true;
			webView.CoreWebView2.WebResourceResponseReceived += CoreWebView2_WebResourceResponseReceived;

			e.Cancel = true;
			Task.Run(() =>
			{
				webView.Invoke(new Action(() => { webView.CoreWebView2.Navigate(e.Uri); }));
			});
		}
	}

Then, the WebResourceResponseReceived handler fires and loadingPDF is true.

Repros in Edge Browser

No, issue does not reproduce in the corresponding Edge version

Regression

No, this never worked

Last working version (if regression)

No response

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with BrowserForm.cs::WebView2Control_CoreWebView2InitializationCompleted and reproduce the NewWindowRequested flow using the listed Google and PDF URLs. Compare the initial navigation's NavigationCompleted and WebResourceResponseReceived events with the cancel-and-CoreWebView2.Navigate workaround. Done means the initial new-window navigation behavior is explained and the event either works as expected or the issue is documented with a confirmed limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.