Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit89f8531

Browse files
Merge pull request#63915 from dotnet/backport/pr-63449-to-release/10.0
[release/10.0] Fix fetch request in data-enhance-nav="false" for same-page anchors
1 parentb1ba55c commit89f8531

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

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

Lines changed: 7 additions & 2 deletions
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,notifyEnhancedNavigationListeners,performScrollToElementOnTheSamePage,isSamePageWithHash}from'./NavigationUtils';
66
import{resetScrollAfterNextBatch,resetScrollIfNeeded}from'../Rendering/Renderer';
77

88
/*
@@ -99,7 +99,7 @@ function onDocumentClick(event: MouseEvent) {
9999
handleClickForNavigationInterception(event,absoluteInternalHref=>{
100100
constoriginalLocation=location.href;
101101

102-
constshouldScrollToHash=isSamePageWithHash(absoluteInternalHref);
102+
constshouldScrollToHash=isSamePageWithHash(originalLocation,absoluteInternalHref);
103103
history.pushState(null,/* ignored title */'',absoluteInternalHref);
104104

105105
if(shouldScrollToHash){
@@ -120,6 +120,11 @@ function onPopState(state: PopStateEvent) {
120120
return;
121121
}
122122

123+
if(state.state==null&&isSamePageWithHash(currentContentUrl,location.href)){
124+
currentContentUrl=location.href;
125+
return;
126+
}
127+
123128
// load the new page
124129
performEnhancedPageLoad(location.href,/* interceptedLink */false);
125130
}

‎src/Components/Web.JS/src/Services/NavigationManager.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function performExternalNavigation(uri: string, replace: boolean) {
150150
asyncfunctionperformInternalNavigation(absoluteInternalHref:string,interceptedLink:boolean,replace:boolean,state:string|undefined=undefined,skipLocationChangingCallback=false){
151151
ignorePendingNavigation();
152152

153-
if(isSamePageWithHash(absoluteInternalHref)){
153+
if(isSamePageWithHash(location.href,absoluteInternalHref)){
154154
saveToBrowserHistory(absoluteInternalHref,replace,state);
155155
performScrollToElementOnTheSamePage(absoluteInternalHref);
156156
return;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ export function isWithinBaseUriSpace(href: string) {
4747
&&(nextChar===''||nextChar==='/'||nextChar==='?'||nextChar==='#');
4848
}
4949

50-
exportfunctionisSamePageWithHash(absoluteHref:string):boolean{
51-
consturl=newURL(absoluteHref);
52-
returnurl.hash!==''&&location.origin===url.origin&&location.pathname===url.pathname&&location.search===url.search;
50+
exportfunctionisSamePageWithHash(oldUrl:string,newUrl:string):boolean{
51+
consta=newURL(oldUrl);
52+
constb=newURL(newUrl);
53+
returna.origin===b.origin&&a.pathname===b.pathname
54+
&&a.search===b.search&&b.hash!=='';
5355
}
5456

5557
exportfunctionisForSamePath(url1:string,url2:string){

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

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

199+
[Fact]
200+
publicvoidNonEnhancedNavCanScrollToHashWithoutFetchingPageAnchor()
201+
{
202+
Navigate($"{ServerPathBase}/nav/scroll-to-hash");
203+
varoriginalTextElem=Browser.Exists(By.CssSelector("#anchor #text"));
204+
Browser.Equal("Text",()=>originalTextElem.Text);
205+
206+
Browser.Exists(By.CssSelector("#anchor #scroll-anchor")).Click();
207+
Browser.True(()=>Browser.GetScrollY()>500);
208+
Browser.True(()=>Browser
209+
.Exists(By.CssSelector("#anchor #uri-on-page-load"))
210+
.GetDomAttribute("data-value")
211+
.EndsWith("scroll-to-hash",StringComparison.Ordinal));
212+
213+
Browser.Equal("Text",()=>originalTextElem.Text);
214+
}
215+
216+
[Fact]
217+
publicvoidNonEnhancedNavCanScrollToHashWithoutFetchingPageNavLink()
218+
{
219+
Navigate($"{ServerPathBase}/nav/scroll-to-hash");
220+
varoriginalTextElem=Browser.Exists(By.CssSelector("#navlink #text"));
221+
Browser.Equal("Text",()=>originalTextElem.Text);
222+
223+
Browser.Exists(By.CssSelector("#navlink #scroll-anchor")).Click();
224+
Browser.True(()=>Browser.GetScrollY()>500);
225+
Browser.True(()=>Browser
226+
.Exists(By.CssSelector("#navlink #uri-on-page-load"))
227+
.GetDomAttribute("data-value")
228+
.EndsWith("scroll-to-hash",StringComparison.Ordinal));
229+
230+
Browser.Equal("Text",()=>originalTextElem.Text);
231+
}
232+
198233
[Theory]
199234
[InlineData("server")]
200235
[InlineData("webassembly")]

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@page"/nav/scroll-to-hash"
22
@attribute [StreamRendering]
33
@injectNavigationManagerNavigationManager
4+
@usingMicrosoft.AspNetCore.Components.Forms
45

56
<PageTitle>Page for scrolling to hash</PageTitle>
67

@@ -13,6 +14,18 @@
1314
<divid="uri-on-page-load"style="display:none"data-value="@uriOnPageLoad"></div>
1415
</p>
1516

17+
<divdata-enhance-nav="false"id="anchor">
18+
<aid="scroll-anchor"href="nav/scroll-to-hash#some-content">Scroll via anchor</a>
19+
<divid="uri-on-page-load"style="display:none"data-value="@uriOnPageLoad"></div>
20+
<pid="text">Text</p>
21+
</div>
22+
23+
<divdata-enhance-nav="false"id="navlink">
24+
<NavLinkid="scroll-anchor"href="nav/scroll-to-hash#some-content">Scroll via NavLink</NavLink>
25+
<divid="uri-on-page-load"style="display:none"data-value="@uriOnPageLoad"></div>
26+
<pid="text">Text</p>
27+
</div>
28+
1629
<divstyle="height:2000px;border:2pxdashedred;">spacer</div>
1730

1831
@if (showContent)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp