MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
437 stars 51 forks source link

[Problem/Bug]: WebResourceRequested not called from CSS with custom URI scheme #4362

Open receiver1 opened 7 months ago

receiver1 commented 7 months ago

What happened?

Hello! I'm developing Embedded Desktop Application using webview2 like GUI. I want statically link resources, so I registered a custom URI scheme and add WebResourceRequested handler. I ran into a problem that loading resources in CSS does not trigger this event.

My CSS:

@font-face {
  font-family: 'Consolas Bold';
  src: url('res://consolasbold.woff2');
  font-weight: bold;
  font-style: normal;
  font-display: swap;
}

My WebResourceRequested handler: (cut down version)

HRESULT STDMETHODCALLTYPE Invoke(
    ICoreWebView2 *sender, ICoreWebView2WebResourceRequestedEventArgs *args) {
  ICoreWebView2WebResourceRequest *request{};
  LPWSTR requestUrl{};
  args->get_Request(&request);
  request->get_Uri(&requestUrl);
  std::printf("%ws\n", requestUrl);
  return S_OK;
}

My scheme registration code:

const WCHAR *allowedOrigins[1] = {L"*"};
auto customSchemeRegistration =
    Microsoft::WRL::Make<CoreWebView2CustomSchemeRegistration>(L"res");
customSchemeRegistration->put_TreatAsSecure(TRUE);
customSchemeRegistration->SetAllowedOrigins(1, allowedOrigins);
customSchemeRegistration->put_HasAuthorityComponent(TRUE);
ICoreWebView2CustomSchemeRegistration *registrations[1] = {
    customSchemeRegistration.Get()};
options->SetCustomSchemeRegistrations(
    1,
    static_cast<ICoreWebView2CustomSchemeRegistration **>(registrations));

Importance

Blocking. My app's basic functions are not working due to this issue.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

No response

SDK Version

No response

Framework

Win32

Operating System

Windows 10

OS Version

No response

Repro steps

The behavior I expect is for the WebResourceRequested event to be raised when using a custom schema in CSS. The current behavior outputs to me in the developer console: Failed to load resource: the server responded with a status of 401 (Unauthorized) and when refreshing the page GET res://consolasbold.woff2/ net::ERR_ABORTED 401 (Unauthorized) Moreover, if you make the same request through the Fetch API in JavaScript, the resource is perfectly defined and obtained. The only difference between these two requests that I found is the presence of "Origin: null" in the headers on the CSS side of the request

request made by CSS: (does not raise the WebResourceRequested event) image request executed JS: (raises the WebResourceRequested event) image

Repros in Edge Browser

No

Regression

No, this never worked

Last working version (if regression)

No response

ray007 commented 6 months ago

I think virtual host with http(s) scheme should work. Custom protocols seem a bit broken for many things.

omegakode commented 6 months ago

So far my experience with custom schemes is that WebResourceRequested never fires, you get a not authorized error in the console. Only NavigationStarted/Ended fire.

jameslund commented 5 months ago

I ran into a this issue as well. To resolve it, I had to add a WebResourceResponseReceived handler (add_WebResourceResponseReceived). Simply returning S_OK from the handler and my WebResourceRequested started receiving calls for the 401 files.

omegakode commented 5 months ago

Thanks, i'll try it