Skip to content

Commit ed013a9

Browse files
authored
Fix pattern matching for @ encoded routes (#6110)
changeset
1 parent 0467aea commit ed013a9

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

.changeset/rich-walls-doubt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Fix pattern matching for routes starting with an encoded `@` symbol

packages/kit/src/utils/routing.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ export function parse_route_id(id) {
1616
id === ''
1717
? /^\/$/
1818
: new RegExp(
19-
`^${decodeURIComponent(id)
19+
`^${id
2020
.split(/(?:@[a-zA-Z0-9_-]+)?(?:\/|$)/)
2121
.map((segment, i, segments) => {
22+
const decoded_segment = decodeURIComponent(segment);
2223
// special case — /[...rest]/ could contain zero segments
23-
const match = /^\[\.\.\.(\w+)(?:=(\w+))?\]$/.exec(segment);
24+
const match = /^\[\.\.\.(\w+)(?:=(\w+))?\]$/.exec(decoded_segment);
2425
if (match) {
2526
names.push(match[1]);
2627
types.push(match[2]);
@@ -30,9 +31,9 @@ export function parse_route_id(id) {
3031
const is_last = i === segments.length - 1;
3132
3233
return (
33-
segment &&
34+
decoded_segment &&
3435
'/' +
35-
segment
36+
decoded_segment
3637
.split(/\[(.+?)\]/)
3738
.map((content, i) => {
3839
if (i % 2) {

packages/kit/src/utils/routing.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ const tests = {
4242
pattern: /^\/matched\/([^/]+?)\/?$/,
4343
names: ['id'],
4444
types: ['uuid']
45+
},
46+
'%23hash-encoded': {
47+
pattern: /^\/%23hash-encoded\/?$/,
48+
names: [],
49+
types: []
50+
},
51+
'%40at-encoded/[id]': {
52+
pattern: /^\/@at-encoded\/([^/]+?)\/?$/,
53+
names: ['id'],
54+
types: [undefined]
4555
}
4656
};
4757

0 commit comments

Comments
 (0)