|
| 1 | +--- |
| 2 | +title: Resolve Hot Reload BrowserLink error with enabled CSP |
| 3 | +description: Explanation and solution for the browserLink javascript error when enabling CSP and running a project from Microsoft Visual Studio. |
| 4 | +type: troubleshooting |
| 5 | +page_title: Resolve Hot Reload BrowserLink error with enabled CSP |
| 6 | +slug: csp-browserlink |
| 7 | +tags: csp, error, browserlink, hot, reload |
| 8 | +res_type: kb |
| 9 | +--- |
| 10 | + |
| 11 | +## Environment |
| 12 | + |
| 13 | +<table> |
| 14 | + <tr> |
| 15 | + <td>Product</td> |
| 16 | + <td>Telerik® UI for ASP.NET Core</td> |
| 17 | + </tr> |
| 18 | +</table> |
| 19 | + |
| 20 | + |
| 21 | +## Description |
| 22 | + |
| 23 | +New versions of Microsoft Visual Studio provide the [Hot Reload](https://learn.microsoft.com/en-us/aspnet/core/test/hot-reload?view=aspnetcore-9.0) and [Browser Link Refresh](https://learn.microsoft.com/en-us/aspnet/core/client-side/using-browserlink?view=aspnetcore-9.0) features in order to facilitiate working with ASP.NET Core projects. |
| 24 | + |
| 25 | +This creates a communication channel between the development environment and one or more web browsers. And since this communication is done with a javascript file connection, when you implement the Content Security Policy meta tag for your application you can see these errors: |
| 26 | + |
| 27 | +```js |
| 28 | +browserLink:4 Refused to connect to 'http://localhost:.../8d05de7…/browserLinkSignalR/…owserLink.initializationData.browserId&browserId=...' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback. |
| 29 | +send @ browserLink:4 |
| 30 | + |
| 31 | +browserLink:4 Refused to connect to 'http://localhost:.../8d05de7…/browserLinkSignalR/…owserLink.initializationData.browserId&browserId=...' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback. |
| 32 | +send @ browserLink:4 |
| 33 | + |
| 34 | +aspnetcore-browser-refresh.js:329 Refused to connect to 'wss://localhost:.../TelerikProjectName/' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback. |
| 35 | +(anonymous) @ aspnetcore-browser-refresh.js:329 |
| 36 | +``` |
| 37 | + |
| 38 | +## Solution |
| 39 | + |
| 40 | +To resolve this issue you can add `connect-src ws: http: 'self';` to your CSP meta tag in the `_Layout.cshtml` file: |
| 41 | + |
| 42 | +```html |
| 43 | + <meta http-equiv="Content-Security-Policy" content="default-src 'self'; img-src 'self' data:; script-src 'self' https://kendo.cdn.telerik.com https://code.jquery.com/ https://cdn.kendostatic.com https://unpkg.com 'nonce-Telerik-Examples'; style-src 'self' https://kendo.cdn.telerik.com https://unpkg.com; font-src 'self' https://unpkg.com; connect-src ws: http: 'self';" /> |
| 44 | +``` |
| 45 | + |
| 46 | +## See Also |
| 47 | + |
| 48 | +* [Content Security Policy Documentation](https://docs.telerik.com/aspnet-core/html-helpers/helper-basics/content-security-policy) |
| 49 | +* [Telerik UI for Core CSP Example](https://github.com/telerik/ui-for-aspnet-core-examples/tree/master/Telerik.Examples.ContentSecurityPolicy) |
0 commit comments