Skip to content

[Problem/Bug]: WebView2 doesn't fire standard browser unload events when Disposed #5446

@zuizuihao

Description

@zuizuihao

What happened?

Description

When a WebView2 control is disposed (via Dispose() or when the host application closes), it does not fire the standard browser window/tab close events that web applications rely on for cleanup.

Expected Behavior

When WebView2 is disposed, it should fire the following events in the web content before terminating:

  1. beforeunload event
  2. pagehide event
  3. unload event
  4. visibilitychange event (with document.visibilityState = 'hidden')

This matches the behavior of Chrome/Edge when closing a tab or window.

Actual Behavior

WebView2 immediately terminates without firing these events, leaving web applications unable to perform cleanup operations such as:

  • Closing WebSocket/SignalR connections gracefully
  • Saving state
  • Sending analytics/telemetry
  • Clearing timers/intervals

Impact

This particularly affects Blazor Server applications which rely on these events to disconnect circuits. Without them:

  • Circuits remain active for 30+ seconds until timeout
  • Server resources are held unnecessarily
  • Connection state becomes inconsistent

Reproduction Steps

  1. Create a Blazor Server application
  2. Host it in a WinForms WebView2 control
  3. Add event listeners for beforeunload, pagehide, unload in JavaScript
  4. Close the WinForms application or call webView2Control.Dispose()
  5. Observe that the events are never fired

Current Workaround

Manually inject script to fire these events before disposal:

Importance

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

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

142.0.3595.80

SDK Version

1.0.3595.46

Framework

Winforms

Operating System

Windows 11

OS Version

No response

Repro steps

repo:
https://github.com/zuizuihao/TestBlazorPageInsideWebview2

only works when I add this fix: Add WebView2 cleanup logic on form closing
Image

remove this fix, can reproduce this issue with below steps.
steps:
open blazor page with webview2:
Image

in blazor server console, no circuit disconnect shows.
Image

Repros in Edge Browser

Yes, issue can be reproduced in the corresponding Edge version

Regression

No, this never worked

Last working version (if regression)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions