Skip to content

Commit b0300dc

Browse files
committed
feat: add CoalesceOptions.PreventAspNetBrowserRefresh
1 parent 81f8f4e commit b0300dc

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 6.0.4
22
- AuditLogging: Stored procedure cache was not being correctly persisted to avoid redundant `SELECT OBJECT_DEFINITION` queries.
3+
- Added `CoalesceOptions.PreventAspNetBrowserRefresh()` utility method to block unwanted `aspnetcore-browser-refresh.js` script.
34

45
# 6.0.3
56
- `c-select` now correctly respects the `filter` prop for enum collections.

docs/topics/startup.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,20 @@ Defaults to `true`.
124124

125125
Coalesce provides several helper extension methods to simplify common application setup tasks:
126126

127+
<Prop def="public static void CoalesceOptions.PreventAspNetBrowserRefresh()" />
128+
129+
Prevents ASP.NET Core from injecting the browser refresh script during development. This script, `aspnetcore-browser-refresh.js`, is redundant with Vite's HMR and attempts to refresh your whole browser whenever a C# file changes.
130+
131+
Call this method at the very start of your `Program.cs`, before creating the `WebApplicationBuilder`:
132+
133+
``` c#
134+
// Program.cs
135+
CoalesceOptions.PreventAspNetBrowserRefresh();
136+
137+
var builder = WebApplication.CreateBuilder(args);
138+
// ... rest of your configuration
139+
```
140+
127141
<Prop def="public static IApplicationBuilder UseViteStaticFiles(this IApplicationBuilder app, ViteStaticFilesOptions? options = null)" />
128142

129143
Configures static file middleware with optimizations for Vite build output. This middleware:

src/IntelliTect.Coalesce/Application/CoalesceOptions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,43 @@
11
using IntelliTect.Coalesce.DataAnnotations;
22
using IntelliTect.Coalesce.Models;
33
using Microsoft.AspNetCore.Mvc.Filters;
4+
using Microsoft.AspNetCore.Hosting;
45
using System;
56
using System.ComponentModel.DataAnnotations;
67

78
namespace IntelliTect.Coalesce;
89

910
public class CoalesceOptions
1011
{
12+
/// <summary>
13+
/// Excludes the browser refresh hosting startup assembly from being loaded.
14+
/// This prevents the undesirable "aspnetcore-browser-refresh.js" script from being injected.
15+
/// Call this method at the very start of your Program.cs, before creating the WebApplicationBuilder.
16+
/// <see href="https://github.com/dotnet/aspnetcore/issues/45213#issuecomment-3488619651">See here for more details.</see>
17+
/// </summary>
18+
public static void PreventAspNetBrowserRefresh()
19+
{
20+
const string key = "ASPNETCORE_" + WebHostDefaults.HostingStartupExcludeAssembliesKey;
21+
const string assemblyToExclude = "Microsoft.AspNetCore.Watch.BrowserRefresh";
22+
23+
var existing = Environment.GetEnvironmentVariable(key);
24+
25+
if (string.IsNullOrWhiteSpace(existing))
26+
{
27+
Environment.SetEnvironmentVariable(key, assemblyToExclude);
28+
}
29+
else if (!existing.Contains(assemblyToExclude))
30+
{
31+
Environment.SetEnvironmentVariable(key, $"{existing};{assemblyToExclude}");
32+
}
33+
34+
var hostingStartupAssemblies = Environment.GetEnvironmentVariable("ASPNETCORE_" + WebHostDefaults.HostingStartupAssembliesKey);
35+
if (!string.IsNullOrWhiteSpace(hostingStartupAssemblies) && hostingStartupAssemblies.Contains(assemblyToExclude))
36+
{
37+
Console.WriteLine($"CoalesceOptions.DisableBrowserRefresh: {assemblyToExclude} has been suppressed.");
38+
}
39+
}
40+
1141
/// <summary>
1242
/// Determines whether API controllers will return the <see cref="Exception.Message"/>
1343
/// of unhandled exceptions or not.

templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Web/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#endif
3737
#endif
3838

39+
CoalesceOptions.PreventAspNetBrowserRefresh();
40+
3941
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
4042
{
4143
Args = args,

templates/Coalesce.Vue.Template/content/Coalesce.Starter.Vue.Web/Properties/launchSettings.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
"Kestrel": {
44
"commandName": "Project",
55
"launchBrowser": true,
6-
7-
// Disables injection of MSFT's undesirable JS that force-refreshes the page when C# files are changed.
8-
"hotReloadEnabled": false,
9-
106
"environmentVariables": {
117
"ASPNETCORE_ENVIRONMENT": "Development"
128
},

0 commit comments

Comments
 (0)