Skip to content

Commit 02112e4

Browse files
committed
Fix fetch request in enanced navigation
1 parent cd1d256 commit 02112e4

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/Components/Web.JS/src/Services/NavigationEnhancement.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
import { synchronizeDomContent } from '../Rendering/DomMerging/DomSync';
5-
import { attachProgrammaticEnhancedNavigationHandler, handleClickForNavigationInterception, hasInteractiveRouter, isForSamePath, isSamePageWithHash, notifyEnhancedNavigationListeners, performScrollToElementOnTheSamePage } from './NavigationUtils';
5+
import { attachProgrammaticEnhancedNavigationHandler, handleClickForNavigationInterception, hasInteractiveRouter, isForSamePath, isSamePageWithHash, notifyEnhancedNavigationListeners, performScrollToElementOnTheSamePage, isHashOnlyChange } from './NavigationUtils';
66
import { resetScrollAfterNextBatch, resetScrollIfNeeded } from '../Rendering/Renderer';
77

88
/*
@@ -120,6 +120,10 @@ function onPopState(state: PopStateEvent) {
120120
return;
121121
}
122122

123+
if (isHashOnlyChange(currentContentUrl, location.href)){
124+
return;
125+
}
126+
123127
// load the new page
124128
performEnhancedPageLoad(location.href, /* interceptedLink */ false);
125129
}

src/Components/Web.JS/src/Services/NavigationUtils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ export function isSamePageWithHash(absoluteHref: string): boolean {
5252
return url.hash !== '' && location.origin === url.origin && location.pathname === url.pathname && location.search === url.search;
5353
}
5454

55+
export function isHashOnlyChange(oldUrl: string, newUrl: string): boolean {
56+
try {
57+
const a = new URL(oldUrl);
58+
const b = new URL(newUrl);
59+
return a.origin === b.origin && a.pathname === b.pathname
60+
&& a.search === b.search && a.hash !== b.hash;
61+
} catch {
62+
return false;
63+
}
64+
}
65+
5566
export function isForSamePath(url1: string, url2: string) {
5667
// We are going to use the scheme, host, port and path to determine if the two URLs are compatible.
5768
// We do not account for the query string as we want to allow for the query string to change.

src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTest.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.AspNetCore.InternalTesting;
1111
using OpenQA.Selenium;
1212
using OpenQA.Selenium.BiDi.Communication;
13+
using OpenQA.Selenium.DevTools;
1314
using OpenQA.Selenium.Support.Extensions;
1415
using TestServer;
1516
using Xunit.Abstractions;
@@ -195,6 +196,34 @@ public void CanScrollToHashWithoutPerformingFullNavigation()
195196
.EndsWith("scroll-to-hash", StringComparison.Ordinal));
196197
}
197198

199+
[Fact]
200+
public void NonEnhancedNavCanScrollToHashWithoutFetchingPage()
201+
{
202+
Navigate($"{ServerPathBase}/nav/scroll-to-hash");
203+
Browser.Equal("Scroll to hash", () => Browser.Exists(By.TagName("h1")).Text);
204+
205+
var javascript = (IJavaScriptExecutor)Browser;
206+
javascript.ExecuteScript(@"
207+
window.testFetchCalls = [];
208+
const originalFetch = window.fetch;
209+
window.fetch = function(...args) {
210+
window.testFetchCalls.push(args[0]);
211+
return originalFetch.apply(this, args);
212+
};");
213+
214+
Browser.Exists(By.Id("scroll-anchor-enhance-nav-false")).Click();
215+
Browser.True(() => Browser.GetScrollY() > 500);
216+
Browser.True(() => Browser
217+
.Exists(By.Id("uri-on-page-load-enhance-nav-false"))
218+
.GetDomAttribute("data-value")
219+
.EndsWith("scroll-to-hash", StringComparison.Ordinal));
220+
221+
var fetchCalls = javascript.ExecuteScript("return window.testFetchCalls;") as IEnumerable<object>;
222+
var relevantCalls = fetchCalls?.Where(call => call.ToString().Contains("scroll-to-hash")) ?? Enumerable.Empty<object>();
223+
224+
Assert.Empty(relevantCalls);
225+
}
226+
198227
[Theory]
199228
[InlineData("server")]
200229
[InlineData("webassembly")]

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/EnhancedNav/PageForScrollingToHash.razor

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
<div id="uri-on-page-load" style="display: none" data-value="@uriOnPageLoad"></div>
1414
</p>
1515

16+
<p data-enhance-nav="false">
17+
<a id="scroll-anchor-enhance-nav-false" href="nav/scroll-to-hash#some-content">Scroll via anchor</a>
18+
<div id="uri-on-page-load-enhance-nav-false" style="display: none" data-value="@uriOnPageLoad"></div>
19+
</p>
20+
1621
<div style="height: 2000px; border: 2px dashed red;">spacer</div>
1722

1823
@if (showContent)

0 commit comments

Comments
 (0)