Skip to content

Commit 7648075

Browse files
committed
fix: permalink redirect error
1 parent 6a9c40f commit 7648075

File tree

8 files changed

+49
-42
lines changed

8 files changed

+49
-42
lines changed

packages/client/src/router/resolveRoute.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolveRoutePathname } from '@vuepress/shared'
1+
import { resolveRouteFullPath } 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'
@@ -17,7 +17,7 @@ export const resolveRoute = <T extends RouteMeta = RouteMeta>(
1717
currentPath?: string,
1818
): ResolvedRoute<T> => {
1919
const routePath = resolveRoutePath(path, currentPath)
20-
const pathname = resolveRoutePathname(routePath)
20+
const [pathname] = resolveRouteFullPath(routePath)
2121
const route = routes.value[pathname] ?? {
2222
...routes.value['/404.html'],
2323
notFound: true,
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { normalizeRoutePath, resolveRoutePathname } from '@vuepress/shared'
1+
import { normalizeRoutePath, resolveRouteFullPath } from '@vuepress/shared'
22
import { redirects, routes } from '../internal/routes.js'
33

44
/**
@@ -10,16 +10,20 @@ export const resolveRoutePath = (
1010
): string => {
1111
// normalized path
1212
const normalizedPath = normalizeRoutePath(path, currentPath)
13-
const normalizedPathname = resolveRoutePathname(normalizedPath)
13+
const [normalizedPathname, queryAndHash] =
14+
resolveRouteFullPath(normalizedPath)
15+
1416
if (routes.value[normalizedPathname]) return normalizedPath
17+
1518
// encoded path
1619
const encodedPathname = encodeURI(normalizedPathname)
1720
if (routes.value[encodedPathname]) return encodeURI(normalizedPath)
1821

19-
// redirected path or fallback to the normalized path
20-
return (
21-
redirects.value[normalizedPathname] ||
22-
redirects.value[encodedPathname] ||
23-
normalizedPath
24-
)
22+
// redirected path
23+
const redirectPath =
24+
redirects.value[normalizedPathname] || redirects.value[encodedPathname]
25+
if (redirectPath) return redirectPath + queryAndHash
26+
27+
// fallback to the normalized path
28+
return normalizedPath
2529
}

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 './resolveRoutePathname.js'
18+
export * from './resolveRouteFullPath.js'
1919
export * from './typeGuards.js'

packages/shared/src/utils/normalizeRoutePath.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { inferRoutePath } from './inferRoutePath.js'
2+
import { resolveRouteFullPath } from './resolveRouteFullPath.js'
23

34
const FAKE_HOST = 'http://.'
45

@@ -15,7 +16,7 @@ export const normalizeRoutePath = (path: string, current?: string): string => {
1516
return inferRoutePath(pathname) + search + hash
1617
}
1718

18-
const [pathname, ...queryAndHash] = path.split(/(\?|#)/)
19+
const [pathname, queryAndHash] = resolveRouteFullPath(path)
1920

20-
return inferRoutePath(pathname) + queryAndHash.join('')
21+
return inferRoutePath(pathname) + queryAndHash
2122
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const SPLIT_CHAR_RE = /(#|\?)/
2+
3+
/**
4+
* Resolve the route.fullPath to [pathname, queryAndHash]
5+
*/
6+
export const resolveRouteFullPath = (path: string): [string, string] => {
7+
const [pathname, ...queryAndHash] = path.split(SPLIT_CHAR_RE)
8+
9+
return [pathname, queryAndHash.join('')]
10+
}

packages/shared/src/utils/resolveRoutePathname.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.
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 { resolveRouteFullPath } from '../src/index.js'
3+
4+
const testCases: [string, [string, string]][] = [
5+
['/a/b/c/', ['/a/b/c/', '']],
6+
['/a/b/c/?a=1', ['/a/b/c/', '?a=1']],
7+
['/a/b/c/#b', ['/a/b/c/', '#b']],
8+
['/a/b/c/?a=1#b', ['/a/b/c/', '?a=1#b']],
9+
['a/index.html', ['a/index.html', '']],
10+
['/a/index.html?a=1', ['/a/index.html', '?a=1']],
11+
['/a/index.html#a', ['/a/index.html', '#a']],
12+
['/a/index.html?a=1#b', ['/a/index.html', '?a=1#b']],
13+
]
14+
15+
describe('should resolve route pathname correctly', () => {
16+
testCases.forEach(([source, expected]) => {
17+
it(`${source} -> ${expected}`, () => {
18+
expect(resolveRouteFullPath(source)).toEqual(expected)
19+
})
20+
})
21+
})

packages/shared/tests/resolveRoutePathname.spec.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)