MicrosoftEdge / MicrosoftEdge/WebView2Feedback

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

Open
#5,446 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?

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

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 the linked TestBlazorPageInsideWebview2 reproduction and the WinForms WebView2 control lifecycle, especially Dispose() and form closing. Compare the behavior with the cleanup logic in commit 6aeecd9492c9297133e6ac8d29224675fd0a253c. Done means the listed browser events fire before disposal and the Blazor Server circuit disconnects without the manual workaround.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.