-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
Add a middleware for browser refresh. #24574
Conversation
b401d73
to
004d735
Compare
Did you mean to put this in preview8?
|
* Introduce a middleware that can connect to the dotnet-watch change server * dotnet-watch: Inject the middleware in 3.1 or apps using start hooks \ hosting startup #23412
004d735
to
54ad088
Compare
@@ -20,7 +20,7 @@ | |||
The web sdk adds an implicit framework reference. This removes it until we can update our build to use framework references. | |||
--> | |||
<ItemGroup> | |||
<FrameworkReference Remove="Microsoft.AspNetCore.App" /> | |||
<FrameworkReference Remove="Microsoft.AspNetCore.App" Condition="'$(DoNotApplyWorkaroundsToMicrosoftAspNetCoreApp)' != 'true'" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI @wtgodbe
@@ -147,7 +147,7 @@ | |||
<KnownFrameworkReference Condition="'$(UseAspNetCoreSharedRuntime)' != 'true'" Remove="Microsoft.AspNetCore.App" /> | |||
<KnownFrameworkReference Remove="Microsoft.WindowsDesktop.App" /> | |||
|
|||
<KnownFrameworkReference Condition="'$(UseAspNetCoreSharedRuntime)' == 'true'" Update="Microsoft.AspNetCore.App"> | |||
<KnownFrameworkReference Condition="'$(UseAspNetCoreSharedRuntime)' == 'true' AND '$(DoNotApplyWorkaroundsToMicrosoftAspNetCoreApp)' != 'true'" Update="Microsoft.AspNetCore.App"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI @wtgodbe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remind me what the motivation was for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We wanted a middleware that targeted netcoreapp3.1 and compiled against M.AspNetCore.App. UseAspNetCoreSharedRuntime
is required to keep the shared runtime reference around, but I didn't want to compile against 5.0.0-dev.
{ | ||
public static void Initialize() | ||
{ | ||
// We're in! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a bit more details here 😄
src/Tools/dotnet-watch/BrowserRefresh/src/ResponseStreamWrapper.cs
Outdated
Show resolved
Hide resolved
src/Tools/dotnet-watch/BrowserRefresh/src/ResponseStreamWrapper.cs
Outdated
Show resolved
Hide resolved
src/Tools/dotnet-watch/BrowserRefresh/src/ResponseStreamWrapper.cs
Outdated
Show resolved
Hide resolved
src/Tools/dotnet-watch/BrowserRefresh/src/WebSocketScriptInjection.cs
Outdated
Show resolved
Hide resolved
src/Tools/dotnet-watch/BrowserRefresh/src/WebSocketScriptInjection.cs
Outdated
Show resolved
Hide resolved
src/Tools/dotnet-watch/BrowserRefresh/src/WebSocketScriptInjection.cs
Outdated
Show resolved
Hide resolved
src/Tools/dotnet-watch/BrowserRefresh/test/WebSockerScriptInjectionTest.cs
Outdated
Show resolved
Hide resolved
src/Tools/dotnet-watch/BrowserRefresh/src/WebSocketScriptInjection.cs
Outdated
Show resolved
Hide resolved
|
||
private static string GetWebSocketClientJavaScript() | ||
{ | ||
var hostString = Environment.GetEnvironmentVariable("ASPNETCORE_AUTO_RELOAD_WS_ENDPOINT"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this really be pulling direct from env variables vs reading through config and/or options?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The middleware doesn't really interact with the remainder of the user app so an option definitely seems overkill. There isn't really a scenario where we'd want to use values that dotnet-watch did not set. Do you think it's still useful for us to use config here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At a minimum some indirection would make it easier to test.
Hello human! Please make sure you've included the Shiproom Template in a comment or (preferably) the PR description. Also, make sure this PR is not marked as a draft and is ready-to-merge. |
🆙 📅 |
public BrowserRefreshMiddleware(RequestDelegate next, ILogger<BrowserRefreshMiddleware> logger) => | ||
(_next, _logger) = (next, logger); | ||
|
||
public async Task InvokeAsync(HttpContext context) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do I disable this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are environment switches for dotnet-watch to turn this off:
DOTNET_WATCH_SUPPRESS_BROWSER_REFRESH=0
. I should update the documentation for this.
return false; | ||
} | ||
|
||
var typedHeaders = request.GetTypedHeaders(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you do a fast path check for a text\html
content type
src/Tools/dotnet-watch/BrowserRefresh/src/WebSocketScriptInjection.cs
Outdated
Show resolved
Hide resolved
src/Tools/dotnet-watch/BrowserRefresh/src/WebSocketScriptInjection.cs
Outdated
Show resolved
Hide resolved
|
||
if (IsHtmlResponse && !ScriptInjectionPerformed) | ||
{ | ||
ScriptInjectionPerformed = await WebSocketScriptInjection.Instance.TryInjectLiveReloadScriptAsync(_baseStream, buffer, offset, count); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ScriptInjectionPerformed = await WebSocketScriptInjection.Instance.TryInjectLiveReloadScriptAsync(_baseStream, buffer, offset, count); | |
ScriptInjectionPerformed = await WebSocketScriptInjection.Instance.TryInjectLiveReloadScriptAsync(_baseStream, buffer, offset, count, cancellationToken); |
_baseStream, | ||
materialized, | ||
0, | ||
materialized.Length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
materialized.Length); | |
materialized.Length, | |
cancellationToken); |
DescriptionAs of 5.0-preview8, Customer impactChanges to dotnet-watch come from our CLI user testing. We think these are compelling scenarios we'd like to get early feedback for. RegressionNo RiskMedium. This change includes injecting code in to a user's app launched via dotnet watch run and rewriting the output. There's a chance that we break the user output, however we'd like to find these out sooner so we can address it for rc1 and there are switches to turn off this behavior if it affects developers. |
// <autogenerated /> | ||
using System; | ||
using System.Reflection; | ||
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v5.0", FrameworkDisplayName = "")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These shouldn't be checked in should they?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope. I should get to the bottom of why they get generated in these path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First comment is the most important
src/Tools/dotnet-watch/BrowserRefresh/src/Microsoft.AspNetCore.Watch.BrowserRefresh.csproj
Outdated
Show resolved
Hide resolved
PrivateAssets="All" | ||
ReferenceOutputAssembly="false" | ||
SkipGetTargetFrameworkProperties="true" | ||
UndefineProperties="TargetFramework;TargetFrameworks" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not now (because the metadata won't be handled correctly) but when this is merging into 'master', switch to use a @(Reference)
item instead. We're avoiding direct project references as much as possible.
<Target Name="_FixupRuntimeConfig" BeforeTargets="_GenerateRuntimeConfigurationFilesInputCache"> | ||
<ItemGroup> | ||
<_RuntimeFramework Include="@(RuntimeFramework)" /> | ||
<RuntimeFramework Remove="@(RuntimeFramework)" /> | ||
<RuntimeFramework Include="Microsoft.AspNetCore.App" FrameworkName="Microsoft.AspNetCore.App" Version="5.0.0-preview" /> | ||
<RuntimeFramework Include="Microsoft.AspNetCore.App" FrameworkName="Microsoft.AspNetCore.App" Version="5.0.0-preview.7.20329.4" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this specific version instead of letting it float? This looks odd outside eng/Versions.props in any case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this back to the earlier version. I had an earlier build of the runtime locally which didn't have some of the newer runtime types such as the non-generic TaskCompletionSource
. I need to sort this out soon for rc1 since it also affects blazor
...TestProjects/KitchenSink/.net/objDebug/net5.0/.NETCoreApp,Version=v5.0.AssemblyAttributes.cs
Outdated
Show resolved
Hide resolved
cfae514
to
fa40263
Compare
#23412