Skip to content

Force full reload on popstate #54877

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 2 commits into from
Apr 4, 2024
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
6 changes: 3 additions & 3 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion src/Components/Web.JS/src/Services/NavigationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,9 @@ async function onBrowserInitiatedPopState(state: PopStateEvent) {
await navigateHistoryWithoutPopStateCallback(delta);
}

await notifyLocationChanged(false);
// We don't know if popstate was triggered for a navigation that can be handled by the client-side router,
// so we treat it as a intercepted link to be safe.
await notifyLocationChanged(/* interceptedLink */ true);
}

async function notifyLocationChanged(interceptedLink: boolean, internalDestinationHref?: string) {
Expand Down
28 changes: 28 additions & 0 deletions src/Components/test/E2ETest/Tests/GlobalInteractivityTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Components.TestServer.RazorComponents;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
using Microsoft.AspNetCore.E2ETesting;
using OpenQA.Selenium;
using TestServer;
using Xunit.Abstractions;

namespace Microsoft.AspNetCore.Components.E2ETests.Tests;

public class GlobalInteractivityTest(
BrowserFixture browserFixture,
BasicTestAppServerSiteFixture<RazorComponentEndpointsStartup<GlobalInteractivityApp>> serverFixture,
ITestOutputHelper output)
: ServerTestBase<BasicTestAppServerSiteFixture<RazorComponentEndpointsStartup<GlobalInteractivityApp>>>(browserFixture, serverFixture, output)
{
[Fact]
public void CanFindStaticallyRenderedPageAfterClickingBrowserBackButtonOnDynamicallyRenderedPage()
{
Navigate("/subdir/static");

Browser.Click(By.CssSelector("a[href=dynamic]"));
Browser.Navigate().Back();

var heading = Browser.Exists(By.TagName("h1"));
Browser.Equal("Statically Rendered", () => heading.Text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public static async Task Main(string[] args)
["Blazor web with server-side blazor root component"] = (BuildWebHost<RazorComponentEndpointsStartup<Root>>(CreateAdditionalArgs(args)), "/subdir"),
["Hosted client-side blazor"] = (BuildWebHost<ClientStartup>(CreateAdditionalArgs(args)), "/subdir"),
["Hot Reload"] = (BuildWebHost<HotReloadStartup>(CreateAdditionalArgs(args)), "/subdir"),
["Dev server client-side blazor"] = CreateDevServerHost(CreateAdditionalArgs(args))
["Dev server client-side blazor"] = CreateDevServerHost(CreateAdditionalArgs(args)),
["Global Interactivity"] = (BuildWebHost<RazorComponentEndpointsStartup<GlobalInteractivityApp>>(CreateAdditionalArgs(args)), "/subdir"),
};

var mainHost = BuildWebHost(args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<base href="/subdir/" />

<HeadOutlet />
</head>

<body>
<Components.WasmMinimal.Routes @rendermode="@RenderModeForPage" />
<script src="_framework/blazor.web.js" autostart="false"></script>
<script>
Blazor.start({
webAssembly: {
loadBootResource: (type, name, defaultUri, integrity) => `WasmMinimal/_framework/${name}`
}
});
</script>
</body>

</html>


@code {
[CascadingParameter]
private HttpContext HttpContext { get; set; } = default!;

// Statically render pages in the "/Account" subdirectory like we do in the Blazor Web template with Individaul auth.
private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/static")
? null
: RenderMode.InteractiveAuto;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@page "/static"

@* This should be statically rendered by GlobalInteractivityApp. *@
<h1>Statically Rendered</h1>

<ul>
<li><NavLink href="dynamic">Dynamic page</NavLink></li>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web

@rendermode RenderMode.InteractiveWebAssembly

@page "/dynamic"

<h1>Dynamically Rendered</h1>

<ul>
<li><NavLink href="static">Static page</NavLink></li>
<li><NavLink href="persist-wasm-state">Another dynamic page</NavLink></li>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@using Microsoft.AspNetCore.Components.Routing

<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>There's nothing here</NotFound>
</Router>