Skip to content

Commit 2e7b294

Browse files
Revert "Disconnect circuit on 'beforeunload' event" (#26297)
* 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>
1 parent 284a270 commit 2e7b294

File tree

7 files changed

+82
-4
lines changed

7 files changed

+82
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
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
@@ -67,7 +67,8 @@ async function boot(userOptions?: Partial<CircuitStartOptions>): Promise<void> {
6767
}
6868
};
6969

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

7374
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
@@ -44,8 +44,8 @@ public override async Task InitializeAsync()
4444
protected override void InitializeAsyncCore()
4545
{
4646
Navigate(ServerPathBase, noReload: false);
47-
Browser.MountTestComponent<CounterComponent>();
48-
Browser.Equal("Current count: 0", () => Browser.FindElement(By.TagName("p")).Text);
47+
Browser.MountTestComponent<GracefulTermination>();
48+
Browser.Equal("Graceful Termination", () => Browser.FindElement(By.TagName("h1")).Text);
4949

5050
GracefulDisconnectCompletionSource = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
5151
Sink = _serverFixture.Host.Services.GetRequiredService<TestSink>();
@@ -91,6 +91,45 @@ public async Task ClosingTheBrowserWindow_GracefullyDisconnects_WhenNavigatingAw
9191
Assert.Contains((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages);
9292
}
9393

94+
[Fact]
95+
public async Task NavigatingToProtocolLink_DoesNotGracefullyDisconnect_TheCurrentCircuit()
96+
{
97+
// Arrange & Act
98+
var element = Browser.FindElement(By.Id("mailto-link"));
99+
element.Click();
100+
await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);
101+
102+
// Assert
103+
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages);
104+
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages);
105+
}
106+
107+
[Fact]
108+
public async Task DownloadAction_DoesNotGracefullyDisconnect_TheCurrentCircuit()
109+
{
110+
// Arrange & Act
111+
var element = Browser.FindElement(By.Id("download-link"));
112+
element.Click();
113+
await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);
114+
115+
// Assert
116+
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages);
117+
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages);
118+
}
119+
120+
[Fact]
121+
public async Task DownloadHref_DoesNotGracefullyDisconnect_TheCurrentCircuit()
122+
{
123+
// Arrange & Act
124+
var element = Browser.FindElement(By.Id("download-href"));
125+
element.Click();
126+
await Task.WhenAny(Task.Delay(10000), GracefulDisconnectCompletionSource.Task);
127+
128+
// Assert
129+
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitTerminatedGracefully"), Messages);
130+
Assert.DoesNotContain((Extensions.Logging.LogLevel.Debug, "CircuitDisconnectedPermanently"), Messages);
131+
}
132+
94133
private void Log(WriteContext wc)
95134
{
96135
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
@@ -40,6 +40,7 @@
4040
<option value="BasicTestApp.FormsTest.InputFileComponent">Input file</option>
4141
<option value="BasicTestApp.NavigateOnSubmit">Navigate to submit</option>
4242
<option value="BasicTestApp.GlobalizationBindCases">Globalization Bind Cases</option>
43+
<option value="BasicTestApp.GracefulTermination">Graceful Termination</option>
4344
<option value="BasicTestApp.HierarchicalImportsTest.Subdir.ComponentUsingImports">Imports statement</option>
4445
<option value="BasicTestApp.HtmlBlockChildContent">ChildContent HTML Block</option>
4546
<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)