Skip to content

Commit 3697b47

Browse files
SteveSandersonMSmkArtakMSFT
authored andcommitted
Workaround Chrome navigation issue (#10839)
Thanks @javiercn and @SteveSandersonMS!
1 parent 23d1d11 commit 3697b47

File tree

6 files changed

+55
-2
lines changed

6 files changed

+55
-2
lines changed

src/Components/Browser.JS/dist/Debug/blazor.server.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14941,9 +14941,19 @@ function enableNavigationInterception() {
1494114941
function navigateTo(uri, forceLoad) {
1494214942
var absoluteUri = toAbsoluteUri(uri);
1494314943
if (!forceLoad && isWithinBaseUriSpace(absoluteUri)) {
14944+
// It's an internal URL, so do client-side navigation
1494414945
performInternalNavigation(absoluteUri, false);
1494514946
}
14947+
else if (forceLoad && location.href === uri) {
14948+
// Force-loading the same URL you're already on requires special handling to avoid
14949+
// triggering browser-specific behavior issues.
14950+
// For details about what this fixes and why, see https://github.com/aspnet/AspNetCore/pull/10839
14951+
var temporaryUri = uri + '?';
14952+
history.replaceState(null, '', temporaryUri);
14953+
location.replace(uri);
14954+
}
1494614955
else {
14956+
// It's either an external URL, or forceLoad is requested, so do a full page load
1494714957
location.href = uri;
1494814958
}
1494914959
}

src/Components/Browser.JS/dist/Debug/blazor.webassembly.js

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

src/Components/Browser.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/Browser.JS/dist/Release/blazor.webassembly.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/Browser.JS/src/Services/UriHelper.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,17 @@ export function navigateTo(uri: string, forceLoad: boolean) {
6565
const absoluteUri = toAbsoluteUri(uri);
6666

6767
if (!forceLoad && isWithinBaseUriSpace(absoluteUri)) {
68+
// It's an internal URL, so do client-side navigation
6869
performInternalNavigation(absoluteUri, false);
70+
} else if (forceLoad && location.href === uri) {
71+
// Force-loading the same URL you're already on requires special handling to avoid
72+
// triggering browser-specific behavior issues.
73+
// For details about what this fixes and why, see https://github.com/aspnet/AspNetCore/pull/10839
74+
const temporaryUri = uri + '?';
75+
history.replaceState(null, '', temporaryUri);
76+
location.replace(uri);
6977
} else {
78+
// It's either an external URL, or forceLoad is requested, so do a full page load
7079
location.href = uri;
7180
}
7281
}

src/Components/test/E2ETest/Tests/RoutingTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,30 @@ public void CanFollowLinkToNotAComponent()
301301
Browser.Equal("Not a component!", () => Browser.FindElement(By.Id("test-info")).Text);
302302
}
303303

304+
[Fact]
305+
public void CanGoBackFromNotAComponent()
306+
{
307+
SetUrlViaPushState("/");
308+
309+
// First go to some URL on the router
310+
var app = MountTestComponent<TestRouter>();
311+
app.FindElement(By.LinkText("Other")).Click();
312+
Browser.True(() => Browser.Url.EndsWith("/Other"));
313+
314+
// Now follow a link out of the SPA entirely
315+
app.FindElement(By.LinkText("Not a component")).Click();
316+
Browser.Equal("Not a component!", () => Browser.FindElement(By.Id("test-info")).Text);
317+
Browser.True(() => Browser.Url.EndsWith("/NotAComponent.html"));
318+
319+
// Now click back
320+
// Because of how the tests are structured with the router not appearing until the router
321+
// tests are selected, we can only observe the test selector being there, but this is enough
322+
// to show we did go back to the right place and the Blazor app started up
323+
Browser.Navigate().Back();
324+
Browser.True(() => Browser.Url.EndsWith("/Other"));
325+
WaitUntilTestSelectorReady();
326+
}
327+
304328
[Fact]
305329
public void CanNavigateProgrammatically()
306330
{

0 commit comments

Comments
 (0)