Skip to content

Commit 7e8027b

Browse files
committed
fix: encode path
1 parent 3c805be commit 7e8027b

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/codegen/__snapshots__/generateRouteRecords.spec.ts.snap

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,36 @@ exports[`generateRouteRecord > correctly names index files 1`] = `
6161
]"
6262
`;
6363

64+
exports[`generateRouteRecord > encodes special characters in path segments 1`] = `
65+
"[
66+
{
67+
path: '/caf%C3%A9',
68+
name: '/café',
69+
component: () => import('café.vue'),
70+
/* no children */
71+
},
72+
{
73+
path: '/my%20page',
74+
name: '/my page',
75+
component: () => import('my page.vue'),
76+
/* no children */
77+
},
78+
{
79+
path: '/users',
80+
/* internal name: '/users' */
81+
/* no component */
82+
children: [
83+
{
84+
path: 'hello%20world',
85+
name: '/users/hello world',
86+
component: () => import('users/hello world.vue'),
87+
/* no children */
88+
}
89+
],
90+
}
91+
]"
92+
`;
93+
6494
exports[`generateRouteRecord > generate custom imports 1`] = `
6595
"[
6696
{

src/codegen/generateRouteRecords.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ describe('generateRouteRecord', () => {
105105
expect(generateRouteRecordSimple(tree)).toMatchSnapshot()
106106
})
107107

108+
it('encodes special characters in path segments', () => {
109+
const tree = new PrefixTree(DEFAULT_OPTIONS)
110+
tree.insert('my page', 'my page.vue')
111+
tree.insert('users/hello world', 'users/hello world.vue')
112+
tree.insert('café', 'café.vue')
113+
expect(generateRouteRecordSimple(tree)).toMatchSnapshot()
114+
})
115+
108116
it('generate static imports', () => {
109117
const options = resolveOptions({
110118
...DEFAULT_OPTIONS,

src/core/treeNodeValue.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,13 @@ function parseFileSegment(
603603

604604
function consumeBuffer() {
605605
if (state === ParseFileSegmentState.static) {
606-
// add the buffer to the path segment as is
607-
pathSegment += buffer
608-
subSegments.push(buffer)
606+
// Encode static segments for URL safety, but preserve slashes from dotNesting
607+
const encodedBuffer = buffer
608+
.split('/')
609+
.map((part) => encodeURIComponent(part))
610+
.join('/')
611+
pathSegment += encodedBuffer
612+
subSegments.push(encodedBuffer)
609613
} else if (state === ParseFileSegmentState.modifier) {
610614
currentTreeRouteParam.paramName = buffer
611615
currentTreeRouteParam.parser = paramParserBuffer || null

0 commit comments

Comments
 (0)