Skip to content

Commit 9f79f51

Browse files
committed
Handle the case when response started.
1 parent def3e5c commit 9f79f51

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.EventDispatch.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.AspNetCore.Components.Rendering;
55
using Microsoft.AspNetCore.Components.RenderTree;
6+
using Microsoft.AspNetCore.Components.Routing;
67
using Microsoft.AspNetCore.Http;
78
using Microsoft.Extensions.DependencyInjection;
89
using Microsoft.Extensions.Hosting;
@@ -84,6 +85,26 @@ private void SetNotFoundResponse(object? sender, EventArgs args)
8485
SignalRendererToFinishRendering();
8586
}
8687

88+
private void OnNavigateTo(object? sender, LocationChangedEventArgs args)
89+
{
90+
if (_httpContext.Response.HasStarted)
91+
{
92+
// We cannot redirect after the response has already started
93+
RenderMetaRefreshTag(args.Location);
94+
}
95+
else
96+
{
97+
_httpContext.Response.Redirect(args.Location);
98+
}
99+
SignalRendererToFinishRendering();
100+
}
101+
102+
private void RenderMetaRefreshTag(string location)
103+
{
104+
var metaTag = $"<meta http-equiv=\"refresh\" content=\"0;url={location}\" />";
105+
_httpContext.Response.WriteAsync(metaTag);
106+
}
107+
87108
private void UpdateNamedSubmitEvents(in RenderBatch renderBatch)
88109
{
89110
if (renderBatch.NamedEventChanges is { } changes)

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,6 @@ internal async Task InitializeStandardComponentServicesAsync(
136136
}
137137
}
138138

139-
private void OnNavigateTo(object? sender, LocationChangedEventArgs args)
140-
{
141-
_httpContext.Response.Redirect(args.Location);
142-
}
143-
144139
private static void InitializeResourceCollection(HttpContext httpContext)
145140
{
146141

src/Components/test/E2ETest/ServerRenderingTests/NoInteractivityTest.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ public void CanUseServerAuthenticationStateByDefault()
6666
}
6767

6868
[Theory]
69-
[InlineData(true)]
70-
[InlineData(false)]
71-
public void NavigatesWithoutInteractivityByRequestRedirection(bool controlFlowByException)
69+
[InlineData(true, true)]
70+
[InlineData(true, false)]
71+
[InlineData(false, true)]
72+
public void NavigatesWithoutInteractivityByRequestRedirection(bool controlFlowByException, bool isStreaming)
7273
{
7374
AppContext.SetSwitch("Microsoft.AspNetCore.Components.Endpoints.EnableThrowNavigationException", isEnabled: controlFlowByException);
74-
Navigate($"{ServerPathBase}/routing/ssr-navigate-to");
75+
string streaming = isStreaming ? $"streaming-" : "";
76+
Navigate($"{ServerPathBase}/routing/ssr-{streaming}navigate-to");
7577
Browser.Equal("Click submit to navigate to home", () => Browser.Exists(By.Id("test-info")).Text);
7678
Browser.Click(By.Id("redirectButton"));
7779
Browser.Equal("Routing test cases", () => Browser.Exists(By.Id("test-info")).Text);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@page "/routing/ssr-streaming-navigate-to"
2+
@attribute [StreamRendering]
3+
@using Microsoft.AspNetCore.Components.Forms
4+
@inject NavigationManager NavigationManager
5+
6+
<p id="test-info">Click submit to navigate to home</p>
7+
<form method="post" @onsubmit="Submit" @formname="MyUniqueFormName">
8+
<AntiforgeryToken />
9+
<button type="submit" id="redirectButton" class="btn btn-primary">Redirect</button>
10+
</form>
11+
12+
@code {
13+
private async Task Submit()
14+
{
15+
await Task.Delay(1000); // Simulate some async work to let the streaming begin
16+
NavigationManager.NavigateTo("/subdir/routing");
17+
}
18+
}
19+

0 commit comments

Comments
 (0)