Skip to content

Commit 4f810fb

Browse files
authored
Fix browser refresh after project restart (#46172)
2 parents 4dda672 + 2adaf56 commit 4f810fb

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

src/BuiltInTools/dotnet-watch/Browser/BrowserConnector.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ await Task.WhenAll(serversToDispose.Select(async server =>
4444
}));
4545
}
4646

47-
public async ValueTask<BrowserRefreshServer?> LaunchOrRefreshBrowserAsync(
47+
/// <summary>
48+
/// A single browser refresh server is created for each project that supports browser launching.
49+
/// When the project is rebuilt we reuse the same refresh server and browser instance.
50+
/// Reload message is sent to the browser in that case.
51+
/// </summary>
52+
public async ValueTask<BrowserRefreshServer?> GetOrCreateBrowserRefreshServerAsync(
4853
ProjectGraphNode projectNode,
4954
ProcessSpec processSpec,
5055
EnvironmentVariablesBuilder environmentBuilder,
@@ -64,12 +69,9 @@ await Task.WhenAll(serversToDispose.Select(async server =>
6469
}
6570
}
6671

67-
// Attach trigger to the process that launches browser on URL found in the process output.
68-
// Only do so for root projects, not for child processes.
69-
if (projectOptions.IsRootProject)
70-
{
71-
processSpec.OnOutput += GetBrowserLaunchTrigger(projectNode, projectOptions, server, cancellationToken);
72-
}
72+
// Attach trigger to the process that detects when the web server reports to the output that it's listening.
73+
// Launches browser on the URL found in the process output for root projects.
74+
processSpec.OnOutput += GetBrowserLaunchTrigger(projectNode, projectOptions, server, cancellationToken);
7375

7476
if (server == null)
7577
{
@@ -81,14 +83,10 @@ await Task.WhenAll(serversToDispose.Select(async server =>
8183
{
8284
// Start the server we just created:
8385
await server.StartAsync(cancellationToken);
84-
server.SetEnvironmentVariables(environmentBuilder);
85-
}
86-
else
87-
{
88-
// Notify the browser of a project rebuild (delta applier notifies of updates):
89-
await server.SendWaitMessageAsync(cancellationToken);
9086
}
9187

88+
server.SetEnvironmentVariables(environmentBuilder);
89+
9290
return server;
9391
}
9492

@@ -137,10 +135,10 @@ void handler(OutputLine line)
137135

138136
matchFound = true;
139137

140-
var projectAddedToAttemptedSet = ImmutableInterlocked.Update(ref _browserLaunchAttempted, static (set, projectNode) => set.Add(projectNode), projectNode);
141-
if (projectAddedToAttemptedSet)
138+
if (projectOptions.IsRootProject &&
139+
ImmutableInterlocked.Update(ref _browserLaunchAttempted, static (set, projectNode) => set.Add(projectNode), projectNode))
142140
{
143-
// first iteration:
141+
// first build iteration of a root project:
144142
LaunchBrowser(launchProfile, match.Groups["url"].Value, server);
145143
}
146144
else if (server != null)

src/BuiltInTools/dotnet-watch/DotNetWatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public override async Task WatchAsync(CancellationToken shutdownCancellationToke
6262
};
6363

6464
var browserRefreshServer = (projectRootNode != null)
65-
? await browserConnector.LaunchOrRefreshBrowserAsync(projectRootNode, processSpec, environmentBuilder, Context.RootProjectOptions, shutdownCancellationToken)
65+
? await browserConnector.GetOrCreateBrowserRefreshServerAsync(projectRootNode, processSpec, environmentBuilder, Context.RootProjectOptions, shutdownCancellationToken)
6666
: null;
6767

6868
environmentBuilder.SetProcessEnvironmentVariables(processSpec);

src/BuiltInTools/dotnet-watch/HotReload/ProjectLauncher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public EnvironmentOptions EnvironmentOptions
9494
}
9595
}
9696

97-
var browserRefreshServer = await browserConnector.LaunchOrRefreshBrowserAsync(projectNode, processSpec, environmentBuilder, projectOptions, cancellationToken);
97+
var browserRefreshServer = await browserConnector.GetOrCreateBrowserRefreshServerAsync(projectNode, processSpec, environmentBuilder, projectOptions, cancellationToken);
9898

9999
var arguments = new List<string>()
100100
{

0 commit comments

Comments
 (0)