Skip to content

Commit a3ca937

Browse files
committed
Reuse the logic from navigation exception.
1 parent 3521c8f commit a3ca937

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private void SetNotFoundResponse(object? sender, EventArgs args)
8989

9090
private async Task OnNavigateTo(string uri)
9191
{
92-
if (_httpContext.Response.HasStarted || _httpContext.Request.Method == HttpMethods.Post)
92+
if (_httpContext.Response.HasStarted)
9393
{
9494
var defaultBufferSize = 16 * 1024;
9595
await using var writer = new HttpResponseStreamWriter(_httpContext.Response.Body, Encoding.UTF8, defaultBufferSize, ArrayPool<byte>.Shared, ArrayPool<char>.Shared);
@@ -99,7 +99,7 @@ private async Task OnNavigateTo(string uri)
9999
}
100100
else
101101
{
102-
_httpContext.Response.Redirect(uri);
102+
await HandleNavigationBeforeResponseStarted(_httpContext, uri);
103103
}
104104
SignalRendererToFinishRendering();
105105
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,28 @@ public static ValueTask<PrerenderedComponentHtmlContent> HandleNavigationExcepti
206206
"response and avoid using features like FlushAsync() before all components on the page have been rendered to prevent failed navigation commands.",
207207
navigationException);
208208
}
209-
else if (IsPossibleExternalDestination(httpContext.Request, navigationException.Location)
209+
else
210+
{
211+
return HandleNavigationBeforeResponseStarted(httpContext, navigationException.Location);
212+
}
213+
}
214+
215+
private static ValueTask<PrerenderedComponentHtmlContent> HandleNavigationBeforeResponseStarted(HttpContext httpContext, string destinationLocation)
216+
{
217+
if (IsPossibleExternalDestination(httpContext.Request, destinationLocation)
210218
&& IsProgressivelyEnhancedNavigation(httpContext.Request))
211219
{
212220
// For progressively-enhanced nav, we prefer to use opaque redirections for external URLs rather than
213221
// forcing the request to be retried, since that allows post-redirect-get to work, plus avoids a
214222
// duplicated request. The client can't rely on receiving this header, though, since non-Blazor endpoints
215223
// wouldn't return it.
216224
httpContext.Response.Headers.Add("blazor-enhanced-nav-redirect-location",
217-
OpaqueRedirection.CreateProtectedRedirectionUrl(httpContext, navigationException.Location));
225+
OpaqueRedirection.CreateProtectedRedirectionUrl(httpContext, destinationLocation));
218226
return new ValueTask<PrerenderedComponentHtmlContent>(PrerenderedComponentHtmlContent.Empty);
219227
}
220228
else
221229
{
222-
httpContext.Response.Redirect(navigationException.Location);
230+
httpContext.Response.Redirect(destinationLocation);
223231
return new ValueTask<PrerenderedComponentHtmlContent>(PrerenderedComponentHtmlContent.Empty);
224232
}
225233
}

0 commit comments

Comments
 (0)