Skip to content

Commit f77bc47

Browse files
Merge pull request #3384 from mikosg/source_maps
WebView2 source map handling
2 parents d6de540 + 88a76ca commit f77bc47

File tree

4 files changed

+108
-17
lines changed

4 files changed

+108
-17
lines changed

microsoft-edge/webview2/concepts/working-with-local-content.md

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.author: msedgedevrel
66
ms.topic: conceptual
77
ms.service: microsoft-edge
88
ms.subservice: webview
9-
ms.date: 04/10/2024
9+
ms.date: 02/21/2025
1010
---
1111
# Using local content in WebView2 apps
1212

@@ -353,13 +353,27 @@ When loading local content via a virtual host name mapping, you are mapping a vi
353353
<!-- ---------- -->
354354
###### Additional web resources
355355
356-
Local content that's loaded via virtual host name mapping has an HTTP or HTTPS URL which supports relative URL resolution. This means that the loaded document can have references to additional web resources such as CSS, script, or image files which are also served via virtual host name mapping.
356+
Local content that's loaded via virtual host name mapping has an HTTP or HTTPS URL which supports relative URL resolution. This means that the loaded document can have references to additional web resources such as CSS, script, or image files which are also served via virtual host name mapping, except source maps; see [Source maps with virtual host name mapping](#source-maps-with-virtual-host-name-mapping), below.
357357
358358
359359
<!-- ---------- -->
360360
###### Additional web resources resolved in WebView2 process
361361
362-
Virtual host name URLs are resolved in WebView2 processes. This is a faster option than `WebResourceRequested`, which resolves in the host app process UI thread.
362+
Virtual host name URLs are resolved in WebView2 processes. This is a faster option than `WebResourceRequested`, which resolves in the host app process UI thread.
363+
364+
365+
<!-- ---------- -->
366+
###### Source maps with virtual host name mapping
367+
368+
Source maps are needed to debug the source code of compiled content, including:
369+
* Transpiled JavaScript, such as TypeScript or minified JavaScript.
370+
* Compiled CSS, such as SASS or SCSS.
371+
372+
WebView2 doesn't load source maps that are referenced by content which was loaded by using virtual host name mapping.
373+
374+
For example, suppose WebView2 loads `main.js` via virtual host name mapping. If `main.js` references `main.js.map` as its source map, `main.js.map` will not be loaded automatically.
375+
376+
To use source maps along with virtual host name mapping, generate inline source maps during compilation of your content. Inline source maps are embedded within the corresponding compiled file.
363377
364378
365379
<!-- ------------------------------ -->
@@ -422,7 +436,7 @@ webView->Navigate(L"https://demo/index.html");
422436
423437
424438
<!-- ====================================================================== -->
425-
## Loading local content by handling the WebResourceRequested event
439+
## Loading local content by handling the `WebResourceRequested` event
426440
427441
Another way you can host local content in a WebView2 control is by relying on the `WebResourceRequested` event. This event is triggered when the control attempts to load a resource. You can use this event to intercept the request and provide the local content, as described in [Custom management of network requests](../how-to/webresourcerequested.md).
428442
@@ -438,7 +452,7 @@ If you want to use a custom scheme to make the Web Resource Request that generat
438452
439453
440454
<!-- ------------------------------ -->
441-
#### Considerations for loading local content by handling the WebResourceRequested event
455+
#### Considerations for loading local content by handling the `WebResourceRequested` event
442456
443457
444458
<!-- ---------- -->
@@ -462,7 +476,7 @@ When loading local content via `WebResourceRequested`, you specify the local con
462476
<!-- ---------- -->
463477
###### Additional web resources
464478
465-
`WebResourceRequested` modifies the content that's loaded via HTTP or HTTPS URLs, which support relative URL resolution. This means that the resulting document can have references to additional web resources such as CSS, script, or image files that are also served via `WebResourceRequested`.
479+
`WebResourceRequested` modifies the content that's loaded via HTTP or HTTPS URLs, which support relative URL resolution. This means that the resulting document can have references to additional web resources such as CSS, script, or image files that are also served via `WebResourceRequested`, except source maps; see [Source maps with the `WebResourceRequested` event](#source-maps-with-the-webresourcerequested-event), below.
466480
467481
468482
<!-- ---------- -->
@@ -477,24 +491,47 @@ When loading content via a file URL or a virtual host name mapping, the resoluti
477491
This can take some time. Make sure to limit calls to `AddWebResourceRequestedFilter` to only the web resources that must raise the `WebResourceRequested` event.
478492
479493
494+
<!-- ---------- -->
495+
###### Source maps with the `WebResourceRequested` event
496+
497+
Source maps are needed to debug the source code of compiled content, including:
498+
* Transpiled JavaScript, such as TypeScript or minified JavaScript.
499+
* Compiled CSS, such as SASS or SCSS.
500+
501+
WebView2 doesn't load source maps that are referenced by content which was loaded by using the `WebResourceRequested` event.
502+
503+
For example, suppose you load `main.js` in your `WebResourceRequested` event handler by setting the `Response` property of `CoreWebView2WebResourceRequestedEventArgs`. If `main.js` references `main.js.map` as its source map:
504+
* `main.js.map` will not be loaded automatically.
505+
* Your `WebResourceRequested` event handler will not be called again to load `main.js.map`.
506+
507+
To use source maps along with `WebResourceRequested`, use one of the following approaches:
508+
509+
* Generate inline source maps during compilation of your content. Inline source maps are embedded within the corresponding compiled file.
510+
511+
* Or, inline separate source maps to the content at runtime in your `WebResourceRequested` event handler. Use this approach only if your build system doesn't support inline source maps.
512+
513+
480514
<!-- ------------------------------ -->
481-
#### APIs for loading local content by handling the WebResourceRequested event
515+
#### APIs for loading local content by handling the `WebResourceRequested` event
482516
483517
484518
##### [.NET/C#](#tab/dotnetcsharp)
485519
486520
* [CoreWebView2.NavigateWithWebResourceRequest Method](/dotnet/api/microsoft.web.webview2.core.corewebview2.navigatewithwebresourcerequest)
487521
* [CoreWebView2.WebResourceRequested Event](/dotnet/api/microsoft.web.webview2.core.corewebview2.webresourcerequested)
522+
* [CoreWebView2WebResourceRequestedEventArgs.Response Property](/dotnet/api/microsoft.web.webview2.core.corewebview2webresourcerequestedeventargs.response)
488523
489524
##### [WinRT/C#](#tab/winrtcsharp)
490525
491526
* [CoreWebView2.NavigateWithWebResourceRequest Method](/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2#navigatewithwebresourcerequest)
492527
* [CoreWebView2.WebResourceRequested Event](/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2#webresourcerequested)
528+
* [CoreWebView2WebResourceRequestedEventArgs.Response Property](/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2webresourcerequestedeventargs#response)
493529
494530
##### [Win32/C++](#tab/win32cpp)
495531
496532
* [ICoreWebView2_2::NavigateWithWebResourceRequest method](/microsoft-edge/webview2/reference/win32/icorewebview2_2#navigatewithwebresourcerequest)
497533
* [ICoreWebView2::WebResourceRequested event (add](/microsoft-edge/webview2/reference/win32/icorewebview2#add_webresourcerequested), [remove)](/microsoft-edge/webview2/reference/win32/icorewebview2#remove_webresourcerequested)
534+
* [ICoreWebView2WebResourceRequestedEventArgs::get_Response](/microsoft-edge/webview2/reference/win32/icorewebview2webresourcerequestedeventargs#get_response)
498535
499536
---
500537

microsoft-edge/webview2/how-to/debug-devtools.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.author: msedgedevrel
66
ms.topic: conceptual
77
ms.service: microsoft-edge
88
ms.subservice: webview
9-
ms.date: 07/07/2022
9+
ms.date: 02/21/2025
1010
---
1111
# Debug WebView2 apps with Microsoft Edge DevTools
1212

@@ -39,6 +39,24 @@ An app can also use the `OpenDevToolsWindow` API to programmatically open a DevT
3939
If none of the above approaches are available, you can add `--auto-open-devtools-for-tabs` to the browser arguments via an environment variable or registry key. This approach will open a DevTools window when a WebView2 is created.
4040

4141

42+
<!-- ====================================================================== -->
43+
## Source maps with the `WebResourceRequested` event or virtual host name mapping
44+
45+
Source maps are needed to debug the source code of compiled content, including:
46+
* Transpiled JavaScript, such as TypeScript or minified JavaScript.
47+
* Compiled CSS, such as SASS or SCSS.
48+
49+
WebView2 doesn't load source maps that are referenced by content which was loaded by using either approach:
50+
51+
* The `WebResourceRequested` event. See:
52+
* [Loading local content by handling the `WebResourceRequested` event](../concepts/working-with-local-content.md#loading-local-content-by-handling-the-webresourcerequested-event) in _Using local content in WebView2 apps_.
53+
* [Source maps with the `WebResourceRequested` event](../concepts/working-with-local-content.md#source-maps-with-the-webresourcerequested-event) in _Using local content in WebView2 apps_.
54+
55+
* Virtual host name mapping. See:
56+
* [Loading local content by using virtual host name mapping](../concepts/working-with-local-content.md#loading-local-content-by-using-virtual-host-name-mapping) in _Using local content in WebView2 apps_.
57+
* [Source maps with virtual host name mapping](../concepts/working-with-local-content.md#source-maps-with-virtual-host-name-mapping) in _Using local content in WebView2 apps_.
58+
59+
4260
<!-- ====================================================================== -->
4361
## See also
4462

microsoft-edge/webview2/how-to/debug-visual-studio-code.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.author: msedgedevrel
66
ms.topic: conceptual
77
ms.service: microsoft-edge
88
ms.subservice: webview
9-
ms.date: 02/11/2022
9+
ms.date: 02/21/2025
1010
---
1111
# Debug WebView2 apps with Visual Studio Code
1212

@@ -17,9 +17,9 @@ Visual Studio Code has a built-in debugger for browser debugging. See [Browser
1717
<!-- ====================================================================== -->
1818
## Create a launch.json file
1919

20-
To debug your code, your project is required to have a `launch.json` file. A `launch.json` file is a debugger configuration file to configure and customize the Visual Studio Code debugger. One of the properties that's needed to configure the debugger is the `request` property. There are two `request` types, `launch` and `attach`.
20+
To debug your code, your project must have a `launch.json` file. A `launch.json` file is a debugger configuration file to configure and customize the Visual Studio Code debugger. One of the properties that's needed to configure the debugger is the `request` property. There are two `request` types: `launch` and `attach`.
2121

22-
The following code demonstrates launching the app from Visual Studio Code (rather than attaching the debugger to a running instance of the app). To do this, the app must have been built previously. If your project doesn't have a `launch.json` file, create a new `launch.json` file in the `.vscode` subfolder in your current project and paste the following code into it:
22+
The following code demonstrates launching the app from Visual Studio Code (rather than attaching the debugger to a running instance of the app). To do this, the app must have been built previously. If your project doesn't have a `launch.json` file, create a new `launch.json` file in the `.vscode` subfolder in your current project, and paste the following code into it:
2323

2424
```json
2525
"name": "Hello debug world",
@@ -28,12 +28,12 @@ The following code demonstrates launching the app from Visual Studio Code (rathe
2828
"request": "launch",
2929
"runtimeExecutable": "C:/path/to/your/webview2/app.exe",
3030
"env": {
31-
// Customize for your app location if needed
31+
// Customize for your app location if needed.
3232
"Path": "%path%;e:/path/to/your/app/location; "
3333
},
3434
"useWebView": true,
35-
// The following two lines set up source path mapping, where `url` is the start page
36-
// of your app, and `webRoot` is the top level directory with all your code files.
35+
// The following two lines set up source path mapping, where "url" is the start page
36+
// of your app, and "webRoot" is the top-level directory containing all your code files.
3737
"url": "file:///${workspaceFolder}/path/to/your/toplevel/foo.html",
3838
"webRoot": "${workspaceFolder}/path/to/your/assets"
3939
```
@@ -87,7 +87,7 @@ Open `launch.json` and complete the following actions to use targeted WebView2 d
8787
"urlFilter": "file://C:/path/to/my/index.ts",
8888
```
8989

90-
When debugging your app, you might need to step through the code from the beginning of the rendering process. If you are rendering webpages on sites and you don't have access to the source code, you can use the `?=value` option, because webpages ignore unrecognized parameters.
90+
When debugging your app, you might need to step through the code from the beginning of the rendering process. If you are rendering webpages on sites and you don't have access to the source code, you can use the `?=value` option, because webpages ignore unrecognized parameters.
9191

9292

9393
<!-- ---------------------------------- -->
@@ -172,7 +172,7 @@ Saving debug output to a log file:
172172
,"trace": "verbose" // Turn on verbose tracing in the Debug Output pane.
173173
```
174174

175-
Visual Studio Code Debug Output with verbose tracing turned on:
175+
Visual Studio Code output in the **DEBUG CONSOLE** pane, with verbose tracing turned on:
176176

177177
![Visual Studio Code Debug Output with verbose tracing turned on](./debug-visual-studio-code-images/verbose.png)
178178

@@ -219,6 +219,24 @@ If you're debugging Office Add-ins, open the add-in source code in a separate in
219219
![Run and Debug](./debug-visual-studio-code-images/attach-uwp.png)
220220

221221

222+
<!-- ====================================================================== -->
223+
## Source maps with the `WebResourceRequested` event or virtual host name mapping
224+
225+
Source maps are needed to debug the source code of compiled content, including:
226+
* Transpiled JavaScript, such as TypeScript or minified JavaScript.
227+
* Compiled CSS, such as SASS or SCSS.
228+
229+
WebView2 doesn't load source maps that are referenced by content which was loaded by using either approach:
230+
231+
* The `WebResourceRequested` event. See:
232+
* [Loading local content by handling the `WebResourceRequested` event](../concepts/working-with-local-content.md#loading-local-content-by-handling-the-webresourcerequested-event) in _Using local content in WebView2 apps_.
233+
* [Source maps with the `WebResourceRequested` event](../concepts/working-with-local-content.md#source-maps-with-the-webresourcerequested-event) in _Using local content in WebView2 apps_.
234+
235+
* Virtual host name mapping. See:
236+
* [Loading local content by using virtual host name mapping](../concepts/working-with-local-content.md#loading-local-content-by-using-virtual-host-name-mapping) in _Using local content in WebView2 apps_.
237+
* [Source maps with virtual host name mapping](../concepts/working-with-local-content.md#source-maps-with-virtual-host-name-mapping) in _Using local content in WebView2 apps_.
238+
239+
222240
<!-- ====================================================================== -->
223241
## Troubleshoot the debugger
224242

microsoft-edge/webview2/how-to/debug-visual-studio.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.author: msedgedevrel
66
ms.topic: conceptual
77
ms.service: microsoft-edge
88
ms.subservice: webview
9-
ms.date: 09/17/2024
9+
ms.date: 02/21/2025
1010
---
1111
# Debug WebView2 apps with Visual Studio
1212

@@ -161,6 +161,24 @@ After doing the above setup, debug your WebView2 app, as follows.
161161
The app output shows "This is the very first line of code that executes", because of the line `console.log("This is the very first line of code that executes.");` in the file `WebView2Samples\SampleApps\WebView2APISample\assets\ScenarioJavaScriptDebugIndex.html`.
162162

163163

164+
<!-- ====================================================================== -->
165+
## Source maps with the `WebResourceRequested` event or virtual host name mapping
166+
167+
Source maps are needed to debug the source code of compiled content, including:
168+
* Transpiled JavaScript, such as TypeScript or minified JavaScript.
169+
* Compiled CSS, such as SASS or SCSS.
170+
171+
WebView2 doesn't load source maps that are referenced by content which was loaded by using either approach:
172+
173+
* The `WebResourceRequested` event. See:
174+
* [Loading local content by handling the `WebResourceRequested` event](../concepts/working-with-local-content.md#loading-local-content-by-handling-the-webresourcerequested-event) in _Using local content in WebView2 apps_.
175+
* [Source maps with the `WebResourceRequested` event](../concepts/working-with-local-content.md#source-maps-with-the-webresourcerequested-event) in _Using local content in WebView2 apps_.
176+
177+
* Virtual host name mapping. See:
178+
* [Loading local content by using virtual host name mapping](../concepts/working-with-local-content.md#loading-local-content-by-using-virtual-host-name-mapping) in _Using local content in WebView2 apps_.
179+
* [Source maps with virtual host name mapping](../concepts/working-with-local-content.md#source-maps-with-virtual-host-name-mapping) in _Using local content in WebView2 apps_.
180+
181+
164182
<!-- ====================================================================== -->
165183
## Troubleshooting
166184

0 commit comments

Comments
 (0)