Skip to content

Expose local-content serving in NativeWebView (custom scheme + response synthesis) — implement on WebKitGTK/WPE #57

Description

@dcuccia

Is your feature request related to a problem? Please describe.

NativeWebView can embed the engine, navigate, run JS, exchange messages, and it raises WebResourceRequested on every platform (including GTK/WPE) — but there's no way to fulfill an intercepted request with app-local bytes, and no custom URI-scheme registration. That makes it impossible to serve a local app bundle offline (an SPA, or a Blazor-/Hybrid-style app). On WKWebView/WebKitGTK/WPE this specifically requires a custom scheme, because those engines can't intercept http:// (as noted in Microsoft's own PhotinoWebViewManager).

Concretely, on GTK the current WebResourceRequested is wired to WebKit's resource-load-started signal, which is observational (can't supply a response body), so it can't serve local content.

Describe the solution you'd like

A small primitive on NativeWebView, mirroring approaches already present in this repo and in Microsoft's shipping stack:

  1. Register an app origin scheme (default app://), via the existing *EnvironmentRequested seam (WebKitGTK/WKWebView must register scheme handlers at context/config creation time).
  2. Respond to an intercepted request with a stream — e.g. SetResponse(Stream content, int statusCode, string reason, string mimeType, headers) (+ a deferral for async/large assets).

The machinery already exists on two backends — AndroidWebViewAdapter.ShouldInterceptRequest returns a WebResourceResponse, and ICoreWebView2Environment.CreateWebResourceResponse is bound for WebView2 — so this is essentially surfacing that through the unified API and implementing it on GTK (webkit_web_context_register_uri_scheme + webkit_uri_scheme_request_finish) and WPE. Structurally it's a near-1:1 of MAUI's WKWebView IWKUrlSchemeHandler.

Describe alternatives you've considered

  • NavigateToString + a file:// base URI works for the top document but inherits file:// origin/security limits (fetch/XHR, ES-module CORS, service workers, and the virtual origin SPAs expect).
  • Header-only mutation on the current WebResourceRequested event isn't enough — there's no response body.
  • A localhost HTTP server defeats the offline/no-server goal.

Additional context

I put together a design write-up and drafted a potential approach (an illustrative spike — not yet build-wired) that mirrors your existing patterns and Microsoft's shipping code:

Main questions before I take it further:

  1. Is local-content serving intended / on the roadmap for NativeWebView?
  2. Preferred shape — response synthesis on the interception surface, a scheme/virtual-host registration API, or both?
  3. WebKitGTK vs. WPE as the primary Linux target?

Glad to shape a PR to your preferences.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions