Skip to content

Fix 'back' button following navigation to external URL. Fixes #10732 #10839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Components/Browser.JS/dist/Debug/blazor.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14941,9 +14941,19 @@ function enableNavigationInterception() {
function navigateTo(uri, forceLoad) {
var absoluteUri = toAbsoluteUri(uri);
if (!forceLoad && isWithinBaseUriSpace(absoluteUri)) {
// It's an internal URL, so do client-side navigation
performInternalNavigation(absoluteUri, false);
}
else if (forceLoad && location.href === uri) {
// Force-loading the same URL you're already on requires special handling to avoid
// triggering browser-specific behavior issues.
// For details about what this fixes and why, see https://github.com/aspnet/AspNetCore/pull/10839
var temporaryUri = uri + '?';
history.replaceState(null, '', temporaryUri);
location.replace(uri);
}
else {
// It's either an external URL, or forceLoad is requested, so do a full page load
location.href = uri;
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/Components/Browser.JS/dist/Debug/blazor.webassembly.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Components/Browser.JS/dist/Release/blazor.server.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/Components/Browser.JS/src/Services/UriHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,17 @@ export function navigateTo(uri: string, forceLoad: boolean) {
const absoluteUri = toAbsoluteUri(uri);

if (!forceLoad && isWithinBaseUriSpace(absoluteUri)) {
// It's an internal URL, so do client-side navigation
performInternalNavigation(absoluteUri, false);
} else if (forceLoad && location.href === uri) {
// Force-loading the same URL you're already on requires special handling to avoid
// triggering browser-specific behavior issues.
// For details about what this fixes and why, see https://github.com/aspnet/AspNetCore/pull/10839
const temporaryUri = uri + '?';
history.replaceState(null, '', temporaryUri);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Does this run in to the same issue if you hit go back + forward + back?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my experiments, that sequence of actions worked fine after this fix.

The fix causes the temporary client-side history entry to get replaced with a regular server-side one, so all back-or-forward sequences cause a full page load when you go across that barrier.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I've only been trying this on desktop browsers, and only in the specific case where it's about navigation to an external URL.

I don't think it's likely to cause regressions in any other case, since the new code path only applies when you're doing a force-load to the same URL you're already on, which is extremely unlikely to be something you're doing in any other case/

location.replace(uri);
} else {
// It's either an external URL, or forceLoad is requested, so do a full page load
location.href = uri;
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/Components/test/E2ETest/Tests/RoutingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,30 @@ public void CanFollowLinkToNotAComponent()
Browser.Equal("Not a component!", () => Browser.FindElement(By.Id("test-info")).Text);
}

[Fact]
public void CanGoBackFromNotAComponent()
{
SetUrlViaPushState("/");

// First go to some URL on the router
var app = MountTestComponent<TestRouter>();
app.FindElement(By.LinkText("Other")).Click();
Browser.True(() => Browser.Url.EndsWith("/Other"));

// Now follow a link out of the SPA entirely
app.FindElement(By.LinkText("Not a component")).Click();
Browser.Equal("Not a component!", () => Browser.FindElement(By.Id("test-info")).Text);
Browser.True(() => Browser.Url.EndsWith("/NotAComponent.html"));

// Now click back
// Because of how the tests are structured with the router not appearing until the router
// tests are selected, we can only observe the test selector being there, but this is enough
// to show we did go back to the right place and the Blazor app started up
Browser.Navigate().Back();
Browser.True(() => Browser.Url.EndsWith("/Other"));
WaitUntilTestSelectorReady();
}

[Fact]
public void CanNavigateProgrammatically()
{
Expand Down