Skip to content

Commit 507be8c

Browse files
committed
updated with changes from coderabbit's comments
1 parent 78d8cce commit 507be8c

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

app/types/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ declare module '#app' {
1010
* @default 70
1111
*/
1212
scrollMargin?: number
13+
/**
14+
* preserve scroll position when only query params change on same path/hash
15+
*/
16+
preserveScrollOnQuery?: boolean
1317
}
1418
}

test/unit/app/router.options.spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { describe, expect, it } from 'vitest'
22
import routerOptions from '../../../app/router.options'
33

4-
function createRoute(overrides: Record<string, unknown> = {}) {
4+
type ScrollBehavior = NonNullable<typeof routerOptions.scrollBehavior>
5+
type RouteArg = Parameters<ScrollBehavior>[0]
6+
7+
function createRoute(overrides: Partial<RouteArg> = {}) {
58
return {
69
path: '/',
710
hash: '',
11+
query: {},
812
meta: {},
913
...overrides,
10-
} as any
14+
} as RouteArg
1115
}
1216

1317
describe('router scrollBehavior', () => {
@@ -22,16 +26,31 @@ describe('router scrollBehavior', () => {
2226
it('preserves scroll on query-only updates for pages that opt in', () => {
2327
const to = createRoute({
2428
path: '/compare',
29+
query: { packages: 'vue,nuxt', facets: 'downloads,license' },
2530
meta: { preserveScrollOnQuery: true },
2631
})
2732
const from = createRoute({
2833
path: '/compare',
34+
query: { packages: 'vue', facets: 'downloads' },
2935
meta: { preserveScrollOnQuery: true },
3036
})
3137

3238
expect(routerOptions.scrollBehavior(to, from, null)).toBe(false)
3339
})
3440

41+
it('does not preserve scroll on query-only updates without opt-in', () => {
42+
const to = createRoute({
43+
path: '/compare',
44+
query: { packages: 'vue,nuxt', facets: 'downloads,license' },
45+
})
46+
const from = createRoute({
47+
path: '/compare',
48+
query: { packages: 'vue', facets: 'downloads' },
49+
})
50+
51+
expect(routerOptions.scrollBehavior(to, from, null)).toEqual({ left: 0, top: 0 })
52+
})
53+
3554
it('scrolls to hash anchors', () => {
3655
const to = createRoute({
3756
hash: '#section-function',

0 commit comments

Comments
 (0)