MicrosoftEdge / MicrosoftEdge/WebView2Feedback
[Problem/Bug]: WebView2 doesn't fire standard browser unload events when Disposed
Nobody has claimed this yet.
- 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:
beforeunloadeventpagehideeventunloadeventvisibilitychangeevent (withdocument.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
- Create a Blazor Server application
- Host it in a WinForms WebView2 control
- Add event listeners for
beforeunload,pagehide,unloadin JavaScript - Close the WinForms application or call
webView2Control.Dispose() - 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
remove this fix, can reproduce this issue with below steps.
steps:
open blazor page with webview2:
in blazor server console, no circuit disconnect shows.
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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