Skip to content

Add support for hot reload to hosted BlazorWasm apps #16701

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

Merged
merged 1 commit into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ setTimeout(function () {

function applyBlazorDeltas(deltas) {
deltas.forEach(d => window.Blazor._internal.applyHotReload(d.moduleId, d.metadataDelta, d.ilDelta));
notifyHotReloadApplied();
}

function displayDiagnostics(diagnostics) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public AspNetCoreDeltaApplier(IReporter reporter)
_reporter = reporter;
}

public bool SuppressBrowserRefreshAfterApply { get; init; }

public async ValueTask InitializeAsync(DotNetWatchContext context, CancellationToken cancellationToken)
{
if (_pipe is not null)
Expand Down Expand Up @@ -104,7 +106,7 @@ public async ValueTask<bool> Apply(DotNetWatchContext context, string changedFil
return false;
}

if (context.BrowserRefreshServer != null)
if (!SuppressBrowserRefreshAfterApply && context.BrowserRefreshServer is not null)
{
if (result == ApplyResult.Success_RefreshBrowser)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public async ValueTask<bool> Apply(DotNetWatchContext context, string changedFil
Deltas = solutionUpdate.Select(c => new UpdateDelta
{
ModuleId = c.ModuleId,
MetadataDelta = c.MetadataDelta,
ILDelta = c.ILDelta,
MetadataDelta = c.MetadataDelta.ToArray(),
ILDelta = c.ILDelta.ToArray(),
}),
};

Expand All @@ -49,7 +49,18 @@ public async ValueTask<bool> Apply(DotNetWatchContext context, string changedFil
return true;
}

public ValueTask ReportDiagnosticsAsync(DotNetWatchContext context, IEnumerable<string> diagnostics, CancellationToken cancellationToken) => throw new NotImplementedException();
public async ValueTask ReportDiagnosticsAsync(DotNetWatchContext context, IEnumerable<string> diagnostics, CancellationToken cancellationToken)
{
if (context.BrowserRefreshServer != null)
{
var message = new HotReloadDiagnostics
{
Diagnostics = diagnostics
};

await context.BrowserRefreshServer.SendJsonSerlialized(message, cancellationToken);
}
}

public void Dispose()
{
Expand All @@ -65,8 +76,15 @@ private readonly struct UpdatePayload
private readonly struct UpdateDelta
{
public Guid ModuleId { get; init; }
public ImmutableArray<byte> MetadataDelta { get; init; }
public ImmutableArray<byte> ILDelta { get; init; }
public byte[] MetadataDelta { get; init; }
public byte[] ILDelta { get; init; }
}

public readonly struct HotReloadDiagnostics
{
public string Type => "HotReloadDiagnosticsv1";

public IEnumerable<string> Diagnostics { get; init; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.ExternalAccess.Watch.Api;
using Microsoft.Extensions.Tools.Internal;

namespace Microsoft.DotNet.Watcher.Tools
{
internal class BlazorWebAssemblyHostedDeltaApplier : IDeltaApplier
{
private readonly BlazorWebAssemblyDeltaApplier _wasmApplier;
private readonly AspNetCoreDeltaApplier _hostApplier;

public BlazorWebAssemblyHostedDeltaApplier(IReporter reporter)
{
_wasmApplier = new BlazorWebAssemblyDeltaApplier(reporter);
_hostApplier = new AspNetCoreDeltaApplier(reporter)
{
SuppressBrowserRefreshAfterApply = true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This flag is saying that if an update was made to the host then browser doesn't need to refresh because unlike other AspNetCore scenarios the app being served in the browser is not running in the host but in the Blazor WASM side.

Is my understanding of that correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default AspNetCoreDeltaApplier will attempt to either re-render components or refresh the browser after a delta has been applied. Since the WebAssembly one takes care of re-rendering, we kinda want this to no-op.

};
}

public async ValueTask InitializeAsync(DotNetWatchContext context, CancellationToken cancellationToken)
{
await _wasmApplier.InitializeAsync(context, cancellationToken);
await _hostApplier.InitializeAsync(context, cancellationToken);
}

public async ValueTask<bool> Apply(DotNetWatchContext context, string changedFile, ImmutableArray<WatchHotReloadService.Update> solutionUpdate, CancellationToken cancellationToken)
{
return await _hostApplier.Apply(context, changedFile, solutionUpdate, cancellationToken) &&
await _wasmApplier.Apply(context, changedFile, solutionUpdate, cancellationToken);
}

public async ValueTask ReportDiagnosticsAsync(DotNetWatchContext context, IEnumerable<string> diagnostics, CancellationToken cancellationToken)
{
// Both WASM and Host have similar implementations for diagnostics. We could pick either to report diagnostics.
await _hostApplier.ReportDiagnosticsAsync(context, diagnostics, cancellationToken);
}

public void Dispose()
{
_hostApplier.Dispose();
_wasmApplier.Dispose();
}
}
}
9 changes: 6 additions & 3 deletions src/BuiltInTools/dotnet-watch/HotReload/CompilationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ public async ValueTask InitializeAsync(DotNetWatchContext context, CancellationT
{
if (_deltaApplier is null)
{
_deltaApplier = context.DefaultLaunchSettingsProfile.HotReloadProfile == "blazorwasm" ?
new BlazorWebAssemblyDeltaApplier(_reporter) :
new AspNetCoreDeltaApplier(_reporter);
_deltaApplier = context.DefaultLaunchSettingsProfile.HotReloadProfile switch
{
"blazorwasm" => new BlazorWebAssemblyDeltaApplier(_reporter),
"blazorwasmhosted" => new BlazorWebAssemblyHostedDeltaApplier(_reporter),
_ => new AspNetCoreDeltaApplier(_reporter),
};
}

await _deltaApplier.InitializeAsync(context, cancellationToken);
Expand Down