diff --git a/src/Components/Components/src/HotReload/HotReloadManager.cs b/src/Components/Components/src/HotReload/HotReloadManager.cs index 699b20a08315901dbda9c4f85f6603f4aa0ff916..3361cf4e25a868326483f1d0c972edba78d1e625 100644 --- a/src/Components/Components/src/HotReload/HotReloadManager.cs +++ b/src/Components/Components/src/HotReload/HotReloadManager.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; using System.Reflection.Metadata; using Microsoft.AspNetCore.Components.HotReload; @@ -11,13 +10,16 @@ namespace Microsoft.AspNetCore.Components.HotReload { internal static class HotReloadManager { - internal static event Action? OnDeltaApplied; + public static event Action? OnDeltaApplied; - public static void DeltaApplied() - { - OnDeltaApplied?.Invoke(); - } + /// <summary> + /// Gets a value that determines if OnDeltaApplied is subscribed to. + /// </summary> + public static bool IsSubscribedTo => OnDeltaApplied is not null; + /// <summary> + /// MetadataUpdateHandler event. This is invoked by the hot reload host via reflection. + /// </summary> public static void UpdateApplication(Type[]? _) => OnDeltaApplied?.Invoke(); } } diff --git a/src/Components/test/E2ETest/ServerExecutionTests/PrerenderingTest.cs b/src/Components/test/E2ETest/ServerExecutionTests/PrerenderingTest.cs index 8036f0eee7e09a0e4c5078a8c4f3f993eea20509..f66890396a5ee1ab33b512e22d769aed70cfde14 100644 --- a/src/Components/test/E2ETest/ServerExecutionTests/PrerenderingTest.cs +++ b/src/Components/test/E2ETest/ServerExecutionTests/PrerenderingTest.cs @@ -1,15 +1,11 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; using System.Net; -using System.Net.Http; -using System.Threading.Tasks; -using BasicTestApp; +using System.Net.Http.Json; using Microsoft.AspNetCore.Components.E2ETest.Infrastructure; using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures; using Microsoft.AspNetCore.E2ETesting; -using Microsoft.AspNetCore.Testing; using OpenQA.Selenium; using TestServer; using Xunit; @@ -138,6 +134,28 @@ namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests Browser.Equal($"Hello, {interactiveUsername ?? "anonymous"}!", () => Browser.Exists(By.TagName("h1")).Text); } + [Fact] + public async Task NoHotReloadListenersAreOrdinarilyRegistered() + { + Navigate("/prerendered/prerendered-transition"); + + // Prerendered output shows "not connected" + Browser.Equal("not connected", () => Browser.Exists(By.Id("connected-state")).Text); + + // Once connected, output changes + BeginInteractivity(); + Browser.Equal("connected", () => Browser.Exists(By.Id("connected-state")).Text); + + // Once connected, output changes + BeginInteractivity(); + Browser.Equal("connected", () => Browser.Exists(By.Id("connected-state")).Text); + + // Now query the hot reload manager and verify nothing is still wired up by default. + var httpClient = new HttpClient { BaseAddress = _serverFixture.RootUri }; + var hasEventHandlers = await httpClient.GetFromJsonAsync<bool>("/prerendered/ishotreloadsubscribedto"); + Assert.False(hasEventHandlers); + } + private void BeginInteractivity() { Browser.Exists(By.Id("load-boot-script")).Click(); diff --git a/src/Components/test/testassets/TestServer/Controllers/ReloadController.cs b/src/Components/test/testassets/TestServer/Controllers/ReloadController.cs index cf71bbed36660cd20f77546b83fe35320793130d..87cc90cfd381782b599071ee8d79ffaf0366f40c 100644 --- a/src/Components/test/testassets/TestServer/Controllers/ReloadController.cs +++ b/src/Components/test/testassets/TestServer/Controllers/ReloadController.cs @@ -12,7 +12,7 @@ namespace ComponentsApp.Server [HttpGet("/rerender")] public IActionResult Rerender() { - HotReloadManager.DeltaApplied(); + HotReloadManager.UpdateApplication(default); return Ok(); } diff --git a/src/Components/test/testassets/TestServer/PrerenderedStartup.cs b/src/Components/test/testassets/TestServer/PrerenderedStartup.cs index c4c10b25bfcb6820de1c0f05faad0ca7b0db097c..8b2339fa94680ecdbf2fdce3fa5ebc6e714995f4 100644 --- a/src/Components/test/testassets/TestServer/PrerenderedStartup.cs +++ b/src/Components/test/testassets/TestServer/PrerenderedStartup.cs @@ -1,14 +1,10 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Globalization; using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; +using Microsoft.AspNetCore.Components.HotReload; using Microsoft.AspNetCore.Components.WebAssembly.Services; -using System.Globalization; namespace TestServer { @@ -51,6 +47,8 @@ namespace TestServer app.UseRouting(); app.UseEndpoints(endpoints => { + endpoints.MapGet("ishotreloadsubscribedto", () => HotReloadManager.IsSubscribedTo); + endpoints.MapRazorPages(); endpoints.MapFallbackToPage("/PrerenderedHost"); endpoints.MapBlazorHub();