If you build a local-first Progressive Web App (PWA) with ES modules, Web Audio, or IndexedDB, shipping a companion Windows desktop .exe is a great way to add native windowing and offline ergonomics.

The common mistake is bundling an embedded HTTP server on 127.0.0.1:PORT. That immediately introduces port collisions, Windows Defender firewall prompts, and localStorage origin resets whenever the port changes.

In Lark Ambient Focus (our MIT-licensed offline Pomodoro timer + procedural Web Audio soundscape mixer — source on GitHub), we ship the exact same static PWA assets inside a lightweight .NET 8 + WebView2 wrapper using virtual host mapping instead of a local HTTP server.

Here are the three implementation details that make it rock-solid in production.

1. Replace localhost With SetVirtualHostNameToFolderMapping

WebView2 lets you map a synthetic HTTPS hostname directly to a folder on disk. Your PWA gets a stable secure context (https://focus.larkandlever.local) so ES modules, localStorage, Canvas, and Web Audio all work identically to a deployed HTTPS site — with zero open TCP ports:

var userData = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "LarkAmbientFocus", "WebView2" ); Directory.CreateDirectory(userData); var env = await CoreWebView2Environment.CreateAsync(null, userData); await _webView.EnsureCoreWebView2Async(env); _webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = true; _webView.CoreWebView2.Settings.IsStatusBarEnabled = false; _webView.CoreWebView2.SetVirtualHostNameToFolderMapping( "focus.larkandlever.local", _webDir, CoreWebView2HostResourceAccessKind.Allow ); _webView.CoreWebView2.Navigate("https://focus.larkandlever.local/index.html"); 

Note two subtle details:

  • Always point userData to %LocalAppData%\<YourApp>\WebView2, never next to the .exe inside Program Files (where non-admin writes fail silently).
  • Call _webView.CreateControl() and touch Handle before initializing if you also support headless/offscreen self-tests.

2. Native Windows 11 Dark Titlebar in 5 Lines

Nothing screams "cheap web wrapper" faster than a bright white Win32 titlebar above a dark-mode app. One dwmapi.dll P/Invoke inside OnHandleCreated fixes it cleanly:

[DllImport("dwmapi.dll")] private static extern int DwmSetWindowAttribute( IntPtr hwnd, int attr, ref int attrValue, int attrSize ); protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); try { int dark = 1; DwmSetWindowAttribute(Handle, 20, ref dark, sizeof(int)); } catch { } } 

Attribute 20 (DWMWA_USE_IMMERSIVE_DARK_MODE) works on Windows 11 and modern Windows 10 builds, and wrapping it in try/catch keeps older Windows builds safe.

3. Automated --self-test Verification (Even in Non-Interactive Desktops)

Before zipping a release, you want an automated smoke check proving the .exe locates its bundled web/ folder, creates its Win32 handle, initializes WebView2, and renders live DOM elements (#clock, #pro-badge).

We pass --self-test <report.json>, hide the window (ShowInTaskbar = false; Opacity = 0;), and handle both normal desktop runs and sandboxed/CI desktop sessions (GetThreadDesktop + GetUserObjectInformationW):

private async Task HandleSelfTestAsync(CoreWebView2NavigationCompletedEventArgs nav) { await Task.Delay(500); string raw = await _webView.CoreWebView2.ExecuteScriptAsync( "JSON.stringify({ title: document.title, clock: document.getElementById('clock')?.textContent || '' })" ); string unescaped = JsonSerializer.Deserialize<string>(raw) ?? "{}"; using var doc = JsonDocument.Parse(unescaped); WriteSelfTest(new { ok = nav.IsSuccess && IsHandleCreated, navigationSuccess = nav.IsSuccess, webDir = _webDir, dom = doc.RootElement, }); Environment.Exit(0); } 

If WebView2 raises a COMException because the test runner lives on a non-Default Windows desktop (like a restricted session), VerifyAssetsInSandbox still validates that index.html, app.js, manifest.webmanifest, and media assets exist at _webDir instead of hanging or failing silently.

You can try the free offline web PWA right in your browser or inspect the Windows .NET 8 desktop source: