Skip to content

Commit 538ce7b

Browse files
committed
chore: add routes utils and expose new stuff
1 parent afdb6e0 commit 538ce7b

File tree

5 files changed

+52
-29
lines changed

5 files changed

+52
-29
lines changed

packages/router/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ export { createRouter } from './router'
141141
export { createNavigationApiRouter } from './navigation-api'
142142
export type { Router, RouterOptions, RouterScrollBehavior } from './router'
143143
export type { RouterApiOptions } from './navigation-api'
144+
export type { TransitionMode, RouterViewTransition } from './transition'
145+
export type { ClientRouterOptions } from './client-router'
146+
export { injectTransitionMode, transitionModeKey } from './transition'
147+
export { createClientRouter } from './client-router'
144148

145149
export { NavigationFailureType, isNavigationFailure } from './errors'
146150
export type {
@@ -162,6 +166,7 @@ export type {
162166
UseLinkReturn,
163167
} from './RouterLink'
164168
export { RouterView } from './RouterView'
169+
export { isChangingPage } from './utils/routes'
165170
export type { RouterViewProps } from './RouterView'
166171

167172
export type { TypesConfig } from './config'

packages/router/src/navigation-api/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
} from '../history/common'
6161
import { RouteRecordNormalized } from '../matcher/types'
6262
import { TransitionMode, transitionModeKey } from '../transition'
63+
import { isChangingPage } from '../utils/routes'
6364

6465
export interface RouterApiOptions extends Omit<RouterOptions, 'history'> {
6566
base?: string
@@ -854,7 +855,8 @@ export function createNavigationApiRouter(
854855
info?.isForwardBrowserButton ||
855856
transitionMode === false ||
856857
(transitionMode !== 'always' &&
857-
window.matchMedia('(prefers-reduced-motion: reduce)').matches)
858+
window.matchMedia('(prefers-reduced-motion: reduce)').matches) ||
859+
!isChangingPage(to, from)
858860
) {
859861
next(true)
860862
return

packages/router/src/router.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ import {
6666
routerViewLocationKey,
6767
} from './injectionSymbols'
6868
import { addDevtools } from './devtools'
69-
import { _LiteralUnion } from './types/utils'
7069
import { RouteLocationAsRelativeTyped } from './typed-routes/route-location'
7170
import { RouteMap } from './typed-routes/route-map'
7271
import {
7372
RouterViewTransition,
7473
TransitionMode,
7574
transitionModeKey,
7675
} from './transition'
76+
import { isChangingPage } from './utils/routes'
7777

7878
/**
7979
* Internal type to define an ErrorHandler
@@ -1251,7 +1251,7 @@ export function createRouter(
12511251
let beforeResolveTransitionGuard: (() => void) | undefined
12521252
let afterEachTransitionGuard: (() => void) | undefined
12531253
let onErrorTransitionGuard: (() => void) | undefined
1254-
let popStateListener: (() => void) | undefined
1254+
let popStateListener: ((event: PopStateEvent) => void) | undefined
12551255

12561256
const router: Router = {
12571257
currentRoute,
@@ -1401,27 +1401,6 @@ function extractChangingRecords(
14011401
return [leavingRecords, updatingRecords, enteringRecords]
14021402
}
14031403

1404-
function isChangingPage(
1405-
to: RouteLocationNormalized,
1406-
from: RouteLocationNormalized
1407-
) {
1408-
if (to === from || from === START_LOCATION) {
1409-
return false
1410-
}
1411-
1412-
// If route keys are different then it will result in a rerender
1413-
if (generateRouteKey(to) !== generateRouteKey(from)) {
1414-
return true
1415-
}
1416-
1417-
const areComponentsSame = to.matched.every(
1418-
(comp, index) =>
1419-
comp.components &&
1420-
comp.components.default === from.matched[index]?.components?.default
1421-
)
1422-
return !areComponentsSame
1423-
}
1424-
14251404
function enableViewTransition(router: Router, options: RouterViewTransition) {
14261405
let transition: undefined | ViewTransition
14271406
let hasUAVisualTransition = false

packages/router/src/transition.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,3 @@ export interface RouterViewTransition {
2020
/** Hook called if the transition is aborted */
2121
onAborted?: (transition: ViewTransition) => void
2222
}
23-
24-
export interface RouterTransitionOptions<T extends TransitionMode = 'auto'> {
25-
mode: T
26-
options?: T extends 'view-transition' ? ViewTransitionOptions : undefined
27-
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { RouteLocationNormalized } from '../typed-routes'
2+
import { START_LOCATION } from '../index'
3+
4+
// from Nuxt
5+
const ROUTE_KEY_PARENTHESES_RE = /(:\w+)\([^)]+\)/g
6+
const ROUTE_KEY_SYMBOLS_RE = /(:\w+)[?+*]/g
7+
const ROUTE_KEY_NORMAL_RE = /:\w+/g
8+
// TODO: consider refactoring into single utility
9+
// See https://github.com/nuxt/nuxt/tree/main/packages/nuxt/src/pages/runtime/utils.ts#L8-L19
10+
function generateRouteKey(route: RouteLocationNormalized) {
11+
const source =
12+
route?.meta.key ??
13+
route.path
14+
.replace(ROUTE_KEY_PARENTHESES_RE, '$1')
15+
.replace(ROUTE_KEY_SYMBOLS_RE, '$1')
16+
.replace(
17+
ROUTE_KEY_NORMAL_RE,
18+
r => route.params[r.slice(1)]?.toString() || ''
19+
)
20+
return typeof source === 'function' ? source(route) : source
21+
}
22+
23+
export function isChangingPage(
24+
to: RouteLocationNormalized,
25+
from: RouteLocationNormalized
26+
) {
27+
if (to === from || from === START_LOCATION) {
28+
return false
29+
}
30+
31+
// If route keys are different then it will result in a rerender
32+
if (generateRouteKey(to) !== generateRouteKey(from)) {
33+
return true
34+
}
35+
36+
const areComponentsSame = to.matched.every(
37+
(comp, index) =>
38+
comp.components &&
39+
comp.components.default === from.matched[index]?.components?.default
40+
)
41+
return !areComponentsSame
42+
}

0 commit comments

Comments
 (0)