Skip to content

Commit 2eb851a

Browse files
Revert "Disconnect circuit on 'beforeunload' event" (#26297) (#26894)
* Add tests for failing disconnect scenarios * Remove beforeunload call and add public API * Add additional test case * Update src/Components/test/testassets/BasicTestApp/GracefulTermination.razor Co-authored-by: Steve Sanderson <SteveSandersonMS@users.noreply.github.com> Co-authored-by: Steve Sanderson <SteveSandersonMS@users.noreply.github.com> Co-authored-by: Steve Sanderson <SteveSandersonMS@users.noreply.github.com>
1 parent 84e3ff5 commit 2eb851a

File tree

7 files changed

+85
-7
lines changed

7 files changed

+85
-7
lines changed

src/Components/Web.JS/dist/Release/blazor.server.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/src/Boot.Server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ async function boot(userOptions?: Partial<CircuitStartOptions>): Promise<void> {
6666
}
6767
};
6868

69-
window.addEventListener('beforeunload', cleanup, { capture: false, once: true });
69+
window['Blazor'].disconnect = cleanup;
70+
7071
window.addEventListener('unload', cleanup, { capture: false, once: true });
7172

7273
window['Blazor'].reconnect = reconnect;

src/Components/test/E2ETest/ServerExecutionTests/CircuitGracefulTerminationTests.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public override async Task InitializeAsync()
4343
protected override void InitializeAsyncCore()
4444
{
4545
Navigate(ServerPathBase, noReload: false);
46-
Browser.MountTestComponent<CounterComponent>();
47-
Browser.Equal("Current count: 0", () => Browser.FindElement(By.TagName("p")).Text);
46+
Browser.MountTestComponent<GracefulTermination>();
47+
Browser.Equal("Graceful Termination", () => Browser.FindElement(By.TagName("h1")).Text);
4848

4949
GracefulDisconnectCompletionSource = new TaskCompletionSource<object>(TaskContinuationOptions.RunContinuationsAsynchronously);
5050
Sink = _serverFixture.Host.Services.GetRequiredService<TestSink>();
@@ -88,6 +88,45 @@ public async Task ClosingTheBrowserWindow_GracefullyDisconnects_WhenNavigatingAw
8888
Assert.Contains((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages);
8989
}
9090

91+
[Fact]
92+
public async Task NavigatingToProtocolLink_DoesNotGracefullyDisconnect_TheCurrentCircuit()
93+
{
94+
// Arrange & Act
95+
var element = Browser.FindElement(By.Id("mailto-link"));
96+
element.Click();
97+
await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);
98+
99+
// Assert
100+
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages);
101+
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages);
102+
}
103+
104+
[Fact]
105+
public async Task DownloadAction_DoesNotGracefullyDisconnect_TheCurrentCircuit()
106+
{
107+
// Arrange & Act
108+
var element = Browser.FindElement(By.Id("download-link"));
109+
element.Click();
110+
await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);
111+
112+
// Assert
113+
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages);
114+
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages);
115+
}
116+
117+
[Fact]
118+
public async Task DownloadHref_DoesNotGracefullyDisconnect_TheCurrentCircuit()
119+
{
120+
// Arrange & Act
121+
var element = Browser.FindElement(By.Id("download-href"));
122+
element.Click();
123+
await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);
124+
125+
// Assert
126+
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages);
127+
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages);
128+
}
129+
91130
private void Log(WriteContext wc)
92131
{
93132
if ((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully") == (wc.LogLevel, wc.EventId.Name))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@inject NavigationManager navigationManager
2+
3+
<h1>Graceful Termination</h1>
4+
5+
<a href="mailto:test@example.com" id="mailto-link">Send Email</a>
6+
<a href="download" download id="download-href">Download Link</a>
7+
<button @onclick="DownloadFile" id="download-link">Download File</button>
8+
9+
@code {
10+
private void DownloadFile()
11+
{
12+
navigationManager.NavigateTo("/subdir/download", true);
13+
}
14+
}

src/Components/test/testassets/BasicTestApp/Index.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<option value="BasicTestApp.FormsTest.TypicalValidationComponent">Typical validation</option>
3636
<option value="BasicTestApp.FormsTest.TypicalValidationComponentUsingExperimentalValidator">Typical validation using experimental validator</option>
3737
<option value="BasicTestApp.GlobalizationBindCases">Globalization Bind Cases</option>
38+
<option value="BasicTestApp.GracefulTermination">Graceful Termination</option>
3839
<option value="BasicTestApp.HierarchicalImportsTest.Subdir.ComponentUsingImports">Imports statement</option>
3940
<option value="BasicTestApp.HtmlBlockChildContent">ChildContent HTML Block</option>
4041
<option value="BasicTestApp.HtmlEncodedChildContent">ChildContent HTML Encoded Block</option>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
namespace Components.TestServer.Controllers
7+
{
8+
public class DownloadController : Controller
9+
{
10+
11+
[HttpGet("~/download")]
12+
public FileStreamResult Download()
13+
{
14+
var buffer = Encoding.UTF8.GetBytes("The quick brown fox jumped over the lazy dog.");
15+
var stream = new MemoryStream(buffer);
16+
17+
var result = new FileStreamResult(stream, "text/plain");
18+
result.FileDownloadName = "test.txt";
19+
return result;
20+
}
21+
}
22+
}

src/Components/test/testassets/TestServer/ServerStartup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3939
app.UseEndpoints(endpoints =>
4040
{
4141
endpoints.MapBlazorHub();
42+
endpoints.MapControllerRoute("mvc", "{controller}/{action}");
4243
endpoints.MapFallbackToPage("/_ServerHost");
4344
});
4445
});

0 commit comments

Comments
 (0)