Skip to content

Commit 077df0e

Browse files
Support more types of redirection during prerendering. Fixes #11591 (#12418)
* E2E test to show current behavior * Actually support base-relative, root-relative, and absolute redirections during prerendering * Fix MVC functional test
1 parent 178374d commit 077df0e

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

src/Components/Server/src/Circuits/RemoteUriHelper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ protected override void NavigateToCore(string uri, bool forceLoad)
8888

8989
if (_jsRuntime == null)
9090
{
91-
throw new NavigationException(uri);
91+
var absoluteUriString = ToAbsoluteUri(uri).ToString();
92+
throw new NavigationException(absoluteUriString);
9293
}
94+
9395
_jsRuntime.InvokeAsync<object>(Interop.NavigateTo, uri, forceLoad);
9496
}
9597

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Threading.Tasks;
48
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
59
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
610
using Microsoft.AspNetCore.E2ETesting;
@@ -75,6 +79,24 @@ public void CanReadUrlHashOnlyOnceConnected()
7579
() => Browser.FindElement(By.TagName("strong")).Text);
7680
}
7781

82+
[Theory]
83+
[InlineData("base/relative", "prerendered/base/relative")]
84+
[InlineData("/root/relative", "/root/relative")]
85+
[InlineData("http://absolute/url", "http://absolute/url")]
86+
public async Task CanRedirectDuringPrerendering(string destinationParam, string expectedRedirectionLocation)
87+
{
88+
var requestUri = new Uri(
89+
_serverFixture.RootUri,
90+
"prerendered/prerendered-redirection?destination=" + destinationParam);
91+
92+
var httpClient = new HttpClient(new HttpClientHandler { AllowAutoRedirect = false });
93+
var response = await httpClient.GetAsync(requestUri);
94+
95+
var expectedUri = new Uri(_serverFixture.RootUri, expectedRedirectionLocation);
96+
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
97+
Assert.Equal(expectedUri, response.Headers.Location);
98+
}
99+
78100
private void BeginInteractivity()
79101
{
80102
Browser.FindElement(By.Id("load-boot-script")).Click();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@page "/prerendered-redirection"
2+
@inject IUriHelper UriHelper
3+
4+
@{
5+
throw new InvalidOperationException("The rendering logic should never be executed");
6+
}
7+
8+
@code {
9+
protected override Task OnInitializedAsync()
10+
{
11+
var uri = UriHelper.GetAbsoluteUri();
12+
var destination = uri.Substring(uri.IndexOf("?destination=") + 13);
13+
UriHelper.NavigateTo(destination);
14+
return Task.CompletedTask;
15+
}
16+
}

src/Mvc/test/Mvc.FunctionalTests/ComponentRenderingFunctionalTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public async Task Redirects_Navigation_Component()
8686

8787
// Assert
8888
await response.AssertStatusCodeAsync(HttpStatusCode.Redirect);
89-
Assert.Equal("/navigation-redirect", response.Headers.Location.ToString());
89+
Assert.Equal("http://localhost/navigation-redirect", response.Headers.Location.ToString());
9090
}
9191

9292
[Fact]
@@ -99,10 +99,9 @@ public async Task Redirects_Navigation_ComponentInteractive()
9999

100100
var response = await client.GetAsync("http://localhost/components/Navigation/false");
101101

102-
// Assert
103102
// Assert
104103
await response.AssertStatusCodeAsync(HttpStatusCode.Redirect);
105-
Assert.Equal("/navigation-redirect", response.Headers.Location.ToString());
104+
Assert.Equal("http://localhost/navigation-redirect", response.Headers.Location.ToString());
106105
}
107106

108107
[Fact]

0 commit comments

Comments
 (0)