-
Notifications
You must be signed in to change notification settings - Fork 65
Add WebView2 best practices folder with UWP guide #5599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chetanpandey1266
wants to merge
3
commits into
main
Choose a base branch
from
user/chetanpandey/uwp-best-practices
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # WebView2 Best Practices | ||
|
|
||
| This folder documents best practices that WebView2 developers should follow when | ||
| building applications that host the WebView2 control. The goal is to capture | ||
| practical, API‑level guidance — informed by real issues that developers have run | ||
| into — so that new and existing WebView2 apps can avoid common pitfalls around | ||
| lifecycle management, navigation, security, performance, and platform | ||
| integration. | ||
|
|
||
| Each document focuses on a specific app model, platform, or scenario, and is | ||
| organized around two questions for every recommendation: | ||
|
|
||
| - **Best practice** — what you should do. | ||
| - **Why?** — the reasoning, and what can go wrong if you don't. | ||
|
|
||
| ## Available guides | ||
|
|
||
| - [UWP best practices](./uwp-best-practices.md) — guidance for hosting WebView2 | ||
| inside Universal Windows Platform (UWP) applications. | ||
|
|
||
| ## Contributing | ||
|
|
||
| If you have hit a problem in your own app that you think other WebView2 | ||
| developers would benefit from knowing about, feel free to open an issue or a | ||
| pull request proposing a new best practice. Please keep entries: | ||
|
|
||
| - **API‑level and product‑agnostic** — describe the WebView2 API behavior and | ||
| the recommended pattern, not implementation details of any specific app or | ||
| internal product. | ||
| - **Actionable** — readers should be able to apply the guidance directly in | ||
| their own code. | ||
| - **Justified** — always include the *Why?* so readers understand the trade‑off | ||
| and can decide how it applies to their scenario. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # UWP Best Practices for WebView2 | ||
|
|
||
| Guidance for hosting WebView2 inside Universal Windows Platform (UWP) apps. | ||
| Each entry uses the format: | ||
|
|
||
| - **Best practice** — what to do (with a code snippet). | ||
| - **Why?** — the problem this prevents. | ||
|
|
||
| --- | ||
|
|
||
| ## 1. Explicitly close the WebView2 controller on app shutdown | ||
|
|
||
| ### Best practice | ||
|
|
||
| Call `Close()` on every `CoreWebView2Controller` you own before your UWP app | ||
| exits, and release your references afterwards. | ||
|
|
||
| ```cpp | ||
| // C++/WinRT | ||
| void App::OnClosed() | ||
| { | ||
| if (m_controller) | ||
| { | ||
| m_controller.Close(); | ||
| m_controller = nullptr; | ||
| m_webview = nullptr; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```csharp | ||
| // C# | ||
| private void OnClosed(object sender, WindowEventArgs e) | ||
|
chetanpandey1266 marked this conversation as resolved.
Outdated
|
||
| { | ||
| if (_controller != null) | ||
| { | ||
| _controller.Close(); | ||
| _controller = null; | ||
| _webView = null; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Why? | ||
|
|
||
| Without an explicit `Close()`, the WebView2 instance is torn down without | ||
| going through the normal browser shutdown procedure. As a result, state such | ||
| as cookies is not flushed correctly, which can lead to data loss or | ||
|
chetanpandey1266 marked this conversation as resolved.
|
||
| inconsistent state on the next launch. | ||
|
|
||
| --- | ||
|
|
||
| ## 2. Handle external URI schemes via the OS launcher | ||
|
|
||
| ### Best practice | ||
|
|
||
| Subscribe to `CoreWebView2.LaunchingExternalUriScheme`, cancel the default | ||
| launch, and forward the URI to `Windows.System.Launcher.LaunchUriAsync` so | ||
| that the OS performs the activation. | ||
|
|
||
| ```csharp | ||
| // C# | ||
| _webView.CoreWebView2.LaunchingExternalUriScheme += async (s, e) => | ||
| { | ||
| e.Cancel = true; | ||
| await Windows.System.Launcher.LaunchUriAsync(new Uri(e.Uri)); | ||
| }; | ||
|
chetanpandey1266 marked this conversation as resolved.
|
||
| ``` | ||
|
|
||
| ```cpp | ||
| // C++/WinRT | ||
| m_webview.LaunchingExternalUriScheme([](auto&&, auto const& args) | ||
| { | ||
| args.Cancel(true); | ||
| Windows::System::Launcher::LaunchUriAsync(Uri{ args.Uri() }); | ||
| }); | ||
| ``` | ||
|
|
||
| ### Why? | ||
|
|
||
| WebView2 inside a UWP app does not automatically activate external protocol | ||
| handlers (for example `mailto:`, `tel:`, `ms-settings:`, store, or custom | ||
| `myapp:` schemes). Without this handler, clicks on such links silently do | ||
| nothing, which appears as a broken link to the user. `LaunchUriAsync` is the | ||
| supported UWP activation path, and `LaunchingExternalUriScheme` is the | ||
| purpose‑built event for this hand‑off — it fires only for external schemes, | ||
| so the app does not need to filter `http`/`https` navigations itself. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.