Skip to content

Commit 6a9c40f

Browse files
committed
chore: tweak
1 parent 1995208 commit 6a9c40f

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

packages/client/src/router/resolveRoute.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { resolveRoutePathname } from '@vuepress/shared'
12
import { routes } from '../internal/routes.js'
23
import type { Route, RouteMeta } from '../types/index.js'
34
import { resolveRoutePath } from './resolveRoutePath.js'
@@ -16,7 +17,7 @@ export const resolveRoute = <T extends RouteMeta = RouteMeta>(
1617
currentPath?: string,
1718
): ResolvedRoute<T> => {
1819
const routePath = resolveRoutePath(path, currentPath)
19-
const pathname = routePath.split(/#|\?/)[0]
20+
const pathname = resolveRoutePathname(routePath)
2021
const route = routes.value[pathname] ?? {
2122
...routes.value['/404.html'],
2223
notFound: true,
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { normalizeRoutePath } from '@vuepress/shared'
1+
import { normalizeRoutePath, resolveRoutePathname } from '@vuepress/shared'
22
import { redirects, routes } from '../internal/routes.js'
33

44
/**
@@ -10,16 +10,16 @@ export const resolveRoutePath = (
1010
): string => {
1111
// normalized path
1212
const normalizedPath = normalizeRoutePath(path, currentPath)
13-
if (routes.value[normalizedPath]) return normalizedPath
14-
13+
const normalizedPathname = resolveRoutePathname(normalizedPath)
14+
if (routes.value[normalizedPathname]) return normalizedPath
1515
// encoded path
16-
const encodedPath = encodeURI(normalizedPath)
17-
if (routes.value[encodedPath]) return encodedPath
16+
const encodedPathname = encodeURI(normalizedPathname)
17+
if (routes.value[encodedPathname]) return encodeURI(normalizedPath)
1818

1919
// redirected path or fallback to the normalized path
2020
return (
21-
redirects.value[normalizedPath] ||
22-
redirects.value[encodedPath] ||
21+
redirects.value[normalizedPathname] ||
22+
redirects.value[encodedPathname] ||
2323
normalizedPath
2424
)
2525
}

packages/shared/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ export * from './removeLeadingSlash.js'
1515
export * from './resolveHeadIdentifier.js'
1616
export * from './resolveLocalePath.js'
1717
export * from './resolveRoutePathFromUrl.js'
18+
export * from './resolveRoutePathname.js'
1819
export * from './typeGuards.js'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const SPLIT_CHAR_RE = /#|\?/
2+
3+
/**
4+
* Resolve the route pathname
5+
*/
6+
export const resolveRoutePathname = (path: string): string => {
7+
return path.split(SPLIT_CHAR_RE)[0]
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { resolveRoutePathname } from '../src/index.js'
3+
4+
const testCases = [
5+
['/a/b/c/', '/a/b/c/'],
6+
['/a/b/c/?a=1', '/a/b/c/'],
7+
['/a/b/c/#b', '/a/b/c/'],
8+
['/a/b/c/?a=1#b', '/a/b/c/'],
9+
['a/index.html', 'a/index.html'],
10+
['/a/index.html?a=1', '/a/index.html'],
11+
['/a/index.html#a', '/a/index.html'],
12+
['/a/index.html?a=1#b', '/a/index.html'],
13+
]
14+
15+
describe('should resolve route pathname correctly', () => {
16+
testCases.forEach(([source, expected]) => {
17+
it(`${source} -> ${expected}`, () => {
18+
expect(resolveRoutePathname(source)).toBe(expected)
19+
})
20+
})
21+
})

0 commit comments

Comments
 (0)