Skip to content

Update 'dotnet watch' doc to detail new .NET 7 behavior #31949

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

Closed
DamianEdwards opened this issue Oct 20, 2022 · 7 comments · Fixed by #32481
Closed

Update 'dotnet watch' doc to detail new .NET 7 behavior #31949

DamianEdwards opened this issue Oct 20, 2022 · 7 comments · Fixed by #32481
Assignees
Labels
🏁 Release: .NET 7 Work items for the .NET 7 release cli/subsvc dotnet-fundamentals/svc 📌 seQUESTered Identifies that an issue has been imported into Quest.

Comments

@DamianEdwards
Copy link
Member

DamianEdwards commented Oct 20, 2022

@MackinnonBuck

The browser refresh middleware used by dotnet watch and Visual Studio to auto-refresh the browser when code changes are detected was updated in .NET 7 to make it more resilient to failures under certain app configurations, including when response compression is enabled.

In these cases it will log a warning to the application output like the following:

warn: Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware[4]
      Unable to configure browser refresh script injection on the response. This may have been caused by the response's Content-Encoding: 'br'. Consider disabling response compression.

We should update this doc (and perhaps the VS Hot Reload docs too) with the details of this change, including that folks can manually add the browser refresh JavaScript reference to their app's pages to get browser refresh working, e.g.:

@if (Environment.GetEnvironmentVariable("__ASPNETCORE_BROWSER_TOOLS") is not null)
{
<script src="/_framework/aspnetcore-browser-refresh.js"></script>
}

Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.


Associated WorkItem - 34608

@tdykstra
Copy link
Contributor

cc @Mikejo5000

@Mikejo5000
Copy link
Contributor

@tdykstra Thanks. I'll do a pass on the Hot Reload doc in the next Sprint, and likely call this out.

@gewarren gewarren added the 🏁 Release: .NET 7 Work items for the .NET 7 release label Oct 25, 2022
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label Nov 9, 2022
@tdykstra tdykstra added the 🗺️ reQUEST Triggers an issue to be imported into Quest. label Nov 9, 2022
@tdykstra tdykstra self-assigned this Nov 9, 2022
@tdykstra tdykstra moved this to 🔖 Ready in dotnet/docs November Sprint Nov 9, 2022
@ghost ghost added the in-pr This issue will be closed (fixed) by an active pull request. label Nov 10, 2022
@github-actions github-actions bot added 📌 seQUESTered Identifies that an issue has been imported into Quest. and removed 🗺️ reQUEST Triggers an issue to be imported into Quest. labels Nov 10, 2022
Repository owner moved this from 🔖 Ready to ✅ Done in dotnet/docs November Sprint Nov 10, 2022
@ghost ghost removed the in-pr This issue will be closed (fixed) by an active pull request. label Nov 10, 2022
@shapeh
Copy link

shapeh commented Feb 12, 2023

Got this error in .NET 7 ASP.NET MVC project. Spent a few hours and finally found this issue, so thanks @DamianEdwards for this.

warn: Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware[4]
Unable to configure browser refresh script injection on the response. This may have been caused by the response's Content-Encoding: 'br'. Consider disabling response compression.

The information on both these links are outdated:


var builder = WebApplication.CreateBuilder(args);
var mvcBuilder = builder.Services.AddRazorPages();
if (builder.Environment.IsDevelopment())
{
    mvcBuilder.AddRazorRuntimeCompilation();
}

Neither of these links solve the problem.
For me, what works is to remove the following from Program.cs / Startup.cs:
app.UseResponseCompression();

Or even better to put it in Program.cs / Startup.cs conditionally like so:

if (!app.Environment.IsDevelopment()) {
    app.UseResponseCompression();
}

@changhuixu
Copy link
Contributor

Should we still disable UseResponseCompression? Would .NET fix it internally? Thank you.

@tdykstra
Copy link
Contributor

@changhuixu Yes, you still need to disable response compression, or manually add the browser refresh JavaScript reference to the app's pages, as explained in the dotnet watch article.

@lonix1
Copy link

lonix1 commented May 3, 2023

I'm not using response compression, and see a similar warning:

warn: Microsoft.AspNetCore.Watch.BrowserRefresh.BrowserRefreshMiddleware[3]
Unable to configure browser refresh script injection on the response. Consider manually adding '<script src="/_framework/aspnetcore-browser-refresh.js"></script>' to the body of the page.

This is the only issue I could find on this matter. Are they related? And if so, do I use the same approach as above?

@tdykstra
Copy link
Contributor

tdykstra commented May 3, 2023

This error message is outside the scope of this article, but the message itself recommends the same resolution, so that would be worth a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏁 Release: .NET 7 Work items for the .NET 7 release cli/subsvc dotnet-fundamentals/svc 📌 seQUESTered Identifies that an issue has been imported into Quest.
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

8 participants