Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
c240df08
Unverified
Commit
c240df08
authored
Aug 25, 2025
by
Zack Tanner
Committed by
GitHub
Aug 25, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[backport] fix: aliased navigations should apply scroll handling (#82900) (#83015)
Backports: - #82900
parent
e61151e3
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
62 additions
and
3 deletions
+62
-3
aliased-prefetch-navigations.ts
...components/router-reducer/aliased-prefetch-navigations.ts
+19
-0
navigate-reducer.ts
...nt/components/router-reducer/reducers/navigate-reducer.ts
+1
-1
page.tsx
...e2e/app-dir/router-autoscroll/app/loading-scroll/page.tsx
+16
-2
router-autoscroll.test.ts
test/e2e/app-dir/router-autoscroll/router-autoscroll.test.ts
+26
-0
No files found.
packages/next/src/client/components/router-reducer/aliased-prefetch-navigations.ts
View file @
c240df08
import
type
{
import
type
{
CacheNodeSeedData
,
CacheNodeSeedData
,
FlightRouterState
,
FlightRouterState
,
FlightSegmentPath
,
}
from
'../../../server/app-render/types'
}
from
'../../../server/app-render/types'
import
type
{
CacheNode
}
from
'../../../shared/lib/app-router-context.shared-runtime'
import
type
{
CacheNode
}
from
'../../../shared/lib/app-router-context.shared-runtime'
import
{
import
{
addSearchParamsIfPageSegment
,
addSearchParamsIfPageSegment
,
DEFAULT_SEGMENT_KEY
,
PAGE_SEGMENT_KEY
,
PAGE_SEGMENT_KEY
,
}
from
'../../../shared/lib/segment'
}
from
'../../../shared/lib/segment'
import
type
{
NormalizedFlightData
}
from
'../../flight-data-helpers'
import
type
{
NormalizedFlightData
}
from
'../../flight-data-helpers'
...
@@ -14,6 +16,7 @@ import { createHrefFromUrl } from './create-href-from-url'
...
@@ -14,6 +16,7 @@ import { createHrefFromUrl } from './create-href-from-url'
import
{
createRouterCacheKey
}
from
'./create-router-cache-key'
import
{
createRouterCacheKey
}
from
'./create-router-cache-key'
import
{
fillCacheWithNewSubTreeDataButOnlyLoading
}
from
'./fill-cache-with-new-subtree-data'
import
{
fillCacheWithNewSubTreeDataButOnlyLoading
}
from
'./fill-cache-with-new-subtree-data'
import
{
handleMutable
}
from
'./handle-mutable'
import
{
handleMutable
}
from
'./handle-mutable'
import
{
generateSegmentsFromPatch
}
from
'./reducers/navigate-reducer'
import
type
{
Mutable
,
ReadonlyReducerState
}
from
'./router-reducer-types'
import
type
{
Mutable
,
ReadonlyReducerState
}
from
'./router-reducer-types'
/**
/**
...
@@ -34,6 +37,7 @@ export function handleAliasedPrefetchEntry(
...
@@ -34,6 +37,7 @@ export function handleAliasedPrefetchEntry(
let
currentCache
=
state
.
cache
let
currentCache
=
state
.
cache
const
href
=
createHrefFromUrl
(
url
)
const
href
=
createHrefFromUrl
(
url
)
let
applied
let
applied
let
scrollableSegments
:
FlightSegmentPath
[]
=
[]
if
(
typeof
flightData
===
'string'
)
{
if
(
typeof
flightData
===
'string'
)
{
return
false
return
false
...
@@ -115,6 +119,20 @@ export function handleAliasedPrefetchEntry(
...
@@ -115,6 +119,20 @@ export function handleAliasedPrefetchEntry(
currentCache
=
newCache
currentCache
=
newCache
applied
=
true
applied
=
true
}
}
for
(
const
subSegment
of
generateSegmentsFromPatch
(
treePatch
))
{
const
scrollableSegmentPath
=
[
...
normalizedFlightData
.
pathToSegment
,
...
subSegment
,
]
// Filter out the __DEFAULT__ paths as they shouldn't be scrolled to in this case.
if
(
scrollableSegmentPath
[
scrollableSegmentPath
.
length
-
1
]
!==
DEFAULT_SEGMENT_KEY
)
{
scrollableSegments
.
push
(
scrollableSegmentPath
)
}
}
}
}
if
(
!
applied
)
{
if
(
!
applied
)
{
...
@@ -125,6 +143,7 @@ export function handleAliasedPrefetchEntry(
...
@@ -125,6 +143,7 @@ export function handleAliasedPrefetchEntry(
mutable
.
cache
=
currentCache
mutable
.
cache
=
currentCache
mutable
.
canonicalUrl
=
href
mutable
.
canonicalUrl
=
href
mutable
.
hashFragment
=
url
.
hash
mutable
.
hashFragment
=
url
.
hash
mutable
.
scrollableSegments
=
scrollableSegments
return
handleMutable
(
state
,
mutable
)
return
handleMutable
(
state
,
mutable
)
}
}
...
...
packages/next/src/client/components/router-reducer/reducers/navigate-reducer.ts
View file @
c240df08
...
@@ -48,7 +48,7 @@ export function handleExternalUrl(
...
@@ -48,7 +48,7 @@ export function handleExternalUrl(
return
handleMutable
(
state
,
mutable
)
return
handleMutable
(
state
,
mutable
)
}
}
function
generateSegmentsFromPatch
(
export
function
generateSegmentsFromPatch
(
flightRouterPatch
:
FlightRouterState
flightRouterPatch
:
FlightRouterState
):
FlightSegmentPath
[]
{
):
FlightSegmentPath
[]
{
const
segments
:
FlightSegmentPath
[]
=
[]
const
segments
:
FlightSegmentPath
[]
=
[]
...
...
test/e2e/app-dir/router-autoscroll/app/loading-scroll/page.tsx
View file @
c240df08
import
Link
from
'next/link'
export
const
dynamic
=
'force-dynamic'
export
const
dynamic
=
'force-dynamic'
export
default
async
function
Page
()
{
export
default
async
function
Page
(
props
:
PageProps
<
'/loading-scroll'
>
)
{
await
new
Promise
((
resolve
)
=>
setTimeout
(
resolve
,
5000
))
const
search
=
await
props
.
searchParams
const
skipSleep
=
!!
search
.
skipSleep
if
(
!
skipSleep
)
{
await
new
Promise
((
resolve
)
=>
setTimeout
(
resolve
,
5000
))
}
return
(
return
(
<>
<>
{
search
.
page
?
<
div
id=
"current-page"
>
{
search
.
page
}
</
div
>
:
null
}
<
div
style=
{
{
display
:
'none'
}
}
>
Content that is hidden.
</
div
>
<
div
style=
{
{
display
:
'none'
}
}
>
Content that is hidden.
</
div
>
<
div
id=
"content-that-is-visible"
>
Content which is not hidden.
</
div
>
<
div
id=
"content-that-is-visible"
>
Content which is not hidden.
</
div
>
{
{
...
@@ -12,6 +19,13 @@ export default async function Page() {
...
@@ -12,6 +19,13 @@ export default async function Page() {
<
div
key=
{
i
}
>
{
i
}
</
div
>
<
div
key=
{
i
}
>
{
i
}
</
div
>
))
))
}
}
<
div
id=
"pages"
>
{
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
].
map
((
item
)
=>
(
<
Link
key=
{
item
}
href=
{
`?page=${item}&skipSleep=1`
}
>
{
item
}
</
Link
>
))
}
</
div
>
</>
</>
)
)
}
}
test/e2e/app-dir/router-autoscroll/router-autoscroll.test.ts
View file @
c240df08
...
@@ -248,5 +248,31 @@ describe('router autoscrolling on navigation', () => {
...
@@ -248,5 +248,31 @@ describe('router autoscrolling on navigation', () => {
await
browser
.
waitForElementByCss
(
'#content-that-is-visible'
)
await
browser
.
waitForElementByCss
(
'#content-that-is-visible'
)
await
check
(()
=>
browser
.
eval
(
'window.scrollY'
),
0
)
await
check
(()
=>
browser
.
eval
(
'window.scrollY'
),
0
)
})
})
it
(
'should scroll to top when navigating to same page with different search params'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/loading-scroll?skipSleep=1'
)
await
retry
(
async
()
=>
{
// scroll to the links at the bottom of the page
await
browser
.
eval
(
`document.getElementById("pages").scrollIntoView()`
)
// grab the current scroll position
const
scrollY
=
await
browser
.
eval
(
`window.scrollY`
)
// sanity check: we should not be scrolled to the top
expect
(
scrollY
).
not
.
toBe
(
0
)
})
// click a link
await
browser
.
elementByCss
(
"a[href='?page=2&skipSleep=1']"
).
click
()
// assert the new page id has been committed
expect
(
await
browser
.
elementById
(
'current-page'
).
text
()).
toBe
(
'2'
)
await
retry
(
async
()
=>
{
// we should have scrolled to the top
expect
(
await
browser
.
eval
(
`window.scrollY`
)).
toBe
(
0
)
})
})
})
})
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment