dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.28k stars 9.96k forks source link

Support disabling auto-injection of browser refresh JavaScript via launch profile property #44805

Open DamianEdwards opened 1 year ago

DamianEdwards commented 1 year ago

As a result of the problems experienced by some customers in their ASP.NET Core apps when the browser refresh script is auto-injected, as detailed in #32767, I’m proposing that we add a new launch settings/profile property that can be used to disable the automatic injection of the browser refresh script.

The property would be named disableBrowserRefreshScriptInjection and would default to false. Setting this property to true in a launch profile in launchSettings.json would prevent dotnet watch and Visual Studio from injecting the middleware that writes out the browser refresh JavaScript to HTML responses from the app, e.g.:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:50226/",
      "sslPort": 44355
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "disableBrowserRefreshScriptInjection": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "https": {
      "commandName": "Project",
      "launchBrowser": true,
      "disableBrowserRefreshScriptInjection": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:7089;http://localhost:5089"
    }
  }
}

Note that this setting should not disable any other part of the mechanism that enables browser refreshing on Hot Reload, project build, CSS file change, etc., so that the app can manually add the required JavaScript reference manually, e.g.

@* Render the browser reload script *@
@if (Environment.GetEnvironmentVariable("__ASPNETCORE_BROWSER_TOOLS") != null)
{
<script src="/_framework/aspnetcore-browser-refresh.js"></script>
}
Muchiachio commented 1 year ago

This is also needed when using front end frameworks which have their own HMR. Getting whole page refresh on controller change isn't really necessary and results in lost state.

jsheely commented 1 year ago

I would also add that this should work in mixed environments where you have blazor on the page that you want to hot reload and you have a razor page that you want the server side rendering to reload separately

Currently the script aspnetcore-browser-refresh script explicitly denies this capability.

function aspnetCoreHotReloadApplied() {
    if (window.Blazor) {
      // If this page has any Blazor, don't refresh the browser.
      notifyHotReloadApplied();
    } else {
      location.reload();
    }
  }
aKzenT commented 1 year ago

While not perfect, you can already disable the middleware responsible for injecting the JavaScript by using the ASPNETCORE_HOSTINGSTARTUPEXCLUDEASSEMBLIES environment variable. See: https://github.com/dotnet/aspnetcore/issues/33068#issuecomment-1699509484 .

ghost commented 12 months ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost commented 10 months ago

Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.