Skip to content

Commit edd2fc1

Browse files
committed
feat: unify api name and avoid unnecessary recalculation
1 parent abd8dbd commit edd2fc1

File tree

11 files changed

+78
-88
lines changed

11 files changed

+78
-88
lines changed

packages/client/src/components/RouteLink.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { computed, defineComponent, h } from 'vue'
22
import type { SlotsType, VNode } from 'vue'
33
import { useRoute, useRouter } from 'vue-router'
4-
import { resolveRoutePath } from '../router/index.js'
4+
import { resolveRouteFullPath } from '../router/index.js'
55

66
/**
77
* Forked from https://github.com/vuejs/router/blob/941b2131e80550009e5221d4db9f366b1fea3fd5/packages/router/src/RouterLink.ts#L293
@@ -91,7 +91,7 @@ export const RouteLink = defineComponent({
9191
const path = computed(() =>
9292
props.to.startsWith('#') || props.to.startsWith('?')
9393
? props.to
94-
: `${__VUEPRESS_BASE__}${resolveRoutePath(props.to, route.path).substring(1)}`,
94+
: `${__VUEPRESS_BASE__}${resolveRouteFullPath(props.to, route.path).substring(1)}`,
9595
)
9696

9797
return () =>

packages/client/src/router/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export type { Router, RouteLocationNormalizedLoaded } from 'vue-router'
22
export { useRoute, useRouter } from 'vue-router'
33

44
export * from './resolveRoute.js'
5+
export * from './resolveRouteFullPath.js'
56
export * from './resolveRoutePath.js'

packages/client/src/router/resolveRoute.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolveRouteFullPath } from '@vuepress/shared'
1+
import { resolveRoutePathInfo } from '@vuepress/shared'
22
import { routes } from '../internal/routes.js'
33
import type { Route, RouteMeta } from '../types/index.js'
44
import { resolveRoutePath } from './resolveRoutePath.js'
@@ -16,16 +16,25 @@ export const resolveRoute = <T extends RouteMeta = RouteMeta>(
1616
path: string,
1717
currentPath?: string,
1818
): ResolvedRoute<T> => {
19-
const routePath = resolveRoutePath(path, currentPath)
20-
const [pathname] = resolveRouteFullPath(routePath)
21-
const route = routes.value[pathname] ?? {
22-
...routes.value['/404.html'],
23-
notFound: true,
19+
// get only the pathname from the path
20+
const [pathname, hashAndQueries] = resolveRoutePathInfo(path)
21+
22+
// resolve the route path
23+
const routePath = resolveRoutePath(pathname, currentPath)
24+
const routeFullPath = routePath + hashAndQueries
25+
26+
// the route not found
27+
if (!routes.value[routePath]) {
28+
return {
29+
...routes.value['/404.html'],
30+
path: routeFullPath,
31+
notFound: true,
32+
} as ResolvedRoute<T>
2433
}
2534

2635
return {
27-
path: routePath,
36+
...routes.value[routePath],
37+
path: routeFullPath,
2838
notFound: false,
29-
...route,
3039
} as ResolvedRoute<T>
3140
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { resolveRoutePathInfo } from '@vuepress/shared'
2+
import { resolveRoutePath } from './resolveRoutePath.js'
3+
4+
/**
5+
* Resolve route full path with given raw path
6+
*/
7+
export const resolveRouteFullPath = (
8+
path: string,
9+
currentPath?: string,
10+
): string => {
11+
const [pathname, hashAndQueries] = resolveRoutePathInfo(path)
12+
13+
return resolveRoutePath(pathname, currentPath) + hashAndQueries
14+
}
Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
1-
import { normalizeRoutePath, resolveRouteFullPath } from '@vuepress/shared'
1+
import { normalizeRoutePath } from '@vuepress/shared'
22
import { redirects, routes } from '../internal/routes.js'
33

44
/**
55
* Resolve route path with given raw path
66
*/
77
export const resolveRoutePath = (
8-
path: string,
8+
pathname: string,
99
currentPath?: string,
1010
): string => {
1111
// normalized path
12-
const normalizedPath = normalizeRoutePath(path, currentPath)
13-
const [normalizedPathname, queryAndHash] =
14-
resolveRouteFullPath(normalizedPath)
12+
const normalizedRoutePath = normalizeRoutePath(pathname, currentPath)
1513

16-
if (routes.value[normalizedPathname]) return normalizedPath
14+
// check if the normalized path is in routes
15+
if (routes.value[normalizedRoutePath]) return normalizedRoutePath
1716

18-
// encoded path
19-
const encodedPathname = encodeURI(normalizedPathname)
20-
if (routes.value[encodedPathname]) return encodeURI(normalizedPath)
17+
// check encoded path
18+
const encodedRoutePath = encodeURI(normalizedRoutePath)
2119

22-
// redirected path
23-
const redirectPath =
24-
redirects.value[normalizedPathname] || redirects.value[encodedPathname]
25-
if (redirectPath) return redirectPath + queryAndHash
20+
if (routes.value[encodedRoutePath]) {
21+
return encodedRoutePath
22+
}
2623

27-
// fallback to the normalized path
28-
return normalizedPath
24+
// check redirected path with normalized path and encoded path
25+
const redirectedRoutePath =
26+
redirects.value[normalizedRoutePath] || redirects.value[encodedRoutePath]
27+
28+
if (redirectedRoutePath) {
29+
return redirectedRoutePath
30+
}
31+
32+
// default to normalized route path
33+
return normalizedRoutePath
2934
}

packages/shared/src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ export * from './removeLeadingSlash.js'
1515
export * from './resolveHeadIdentifier.js'
1616
export * from './resolveLocalePath.js'
1717
export * from './resolveRoutePathFromUrl.js'
18-
export * from './resolveRouteFullPath.js'
18+
export * from './resolveRoutePathInfo.js'
1919
export * from './typeGuards.js'
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import { inferRoutePath } from './inferRoutePath.js'
2-
import { resolveRouteFullPath } from './resolveRouteFullPath.js'
32

43
const FAKE_HOST = 'http://.'
54

65
/**
7-
* Normalize the given path to the final route path
6+
* Normalize the given pathname path to the final route path
87
*/
9-
export const normalizeRoutePath = (path: string, current?: string): string => {
10-
if (!path.startsWith('/') && current) {
8+
export const normalizeRoutePath = (
9+
pathname: string,
10+
current?: string,
11+
): string => {
12+
if (!pathname.startsWith('/') && current) {
1113
// the relative path should be resolved against the current path
1214
const loc = current.slice(0, current.lastIndexOf('/'))
1315

14-
const { pathname, search, hash } = new URL(`${loc}/${path}`, FAKE_HOST)
15-
16-
return inferRoutePath(pathname) + search + hash
16+
return inferRoutePath(new URL(`${loc}/${pathname}`, FAKE_HOST).pathname)
1717
}
1818

19-
const [pathname, queryAndHash] = resolveRouteFullPath(path)
20-
21-
return inferRoutePath(pathname) + queryAndHash
19+
return inferRoutePath(pathname)
2220
}

packages/shared/src/utils/resolveRouteFullPath.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const SPLIT_CHAR_REGEXP = /(#|\?)/
2+
3+
/**
4+
* Extract pathname / hash and queries from a full route path
5+
*/
6+
export const resolveRoutePathInfo = (
7+
path: string,
8+
): [pathname: string, hashAndQueries: string] => {
9+
const [pathname, ...hashAndQueries] = path.split(SPLIT_CHAR_REGEXP)
10+
11+
return [pathname, hashAndQueries.join('')]
12+
}

packages/shared/tests/normalizeRoutePath.spec.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -204,42 +204,3 @@ describe('should normalize clean paths correctly', () => {
204204
}),
205205
)
206206
})
207-
208-
describe('should normalize paths with query correctly', () => {
209-
testCases
210-
.map(([[path, current], expected]) => [
211-
[`${path}?foo=bar`, current],
212-
`${expected}?foo=bar`,
213-
])
214-
.forEach(([[path, current], expected]) =>
215-
it(`${current ? `"${current}"-` : ''}"${path}" -> "${expected}"`, () => {
216-
expect(normalizeRoutePath(path, current)).toBe(expected)
217-
}),
218-
)
219-
})
220-
221-
describe('should normalize paths with hash correctly', () => {
222-
testCases
223-
.map(([[path, current], expected]) => [
224-
[`${path}#foobar`, current],
225-
`${expected}#foobar`,
226-
])
227-
.map(([[path, current], expected]) =>
228-
it(`${current ? `"${current}"-` : ''}"${path}" -> "${expected}"`, () => {
229-
expect(normalizeRoutePath(path, current)).toBe(expected)
230-
}),
231-
)
232-
})
233-
234-
describe('should normalize paths with query and hash correctly', () => {
235-
testCases
236-
.map(([[path, current], expected]) => [
237-
[`${path}?foo=1&bar=2#foobar`, current],
238-
`${expected}?foo=1&bar=2#foobar`,
239-
])
240-
.map(([[path, current], expected]) =>
241-
it(`${current ? `"${current}"-` : ''}"${path}" -> "${expected}"`, () => {
242-
expect(normalizeRoutePath(path, current)).toBe(expected)
243-
}),
244-
)
245-
})

0 commit comments

Comments
 (0)