Skip to content

Commit

Permalink
Fix pattern matching for @ encoded routes (#6110)
Browse files Browse the repository at this point in the history
changeset
  • Loading branch information
trmcnvn committed Aug 20, 2022
1 parent 0467aea commit ed013a9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-walls-doubt.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Fix pattern matching for routes starting with an encoded `@` symbol
9 changes: 5 additions & 4 deletions packages/kit/src/utils/routing.js
Expand Up @@ -16,11 +16,12 @@ export function parse_route_id(id) {
id === ''
? /^\/$/
: new RegExp(
`^${decodeURIComponent(id)
`^${id
.split(/(?:@[a-zA-Z0-9_-]+)?(?:\/|$)/)
.map((segment, i, segments) => {
const decoded_segment = decodeURIComponent(segment);
// special case — /[...rest]/ could contain zero segments
const match = /^\[\.\.\.(\w+)(?:=(\w+))?\]$/.exec(segment);
const match = /^\[\.\.\.(\w+)(?:=(\w+))?\]$/.exec(decoded_segment);
if (match) {
names.push(match[1]);
types.push(match[2]);
Expand All @@ -30,9 +31,9 @@ export function parse_route_id(id) {
const is_last = i === segments.length - 1;
return (
segment &&
decoded_segment &&
'/' +
segment
decoded_segment
.split(/\[(.+?)\]/)
.map((content, i) => {
if (i % 2) {
Expand Down
10 changes: 10 additions & 0 deletions packages/kit/src/utils/routing.spec.js
Expand Up @@ -42,6 +42,16 @@ const tests = {
pattern: /^\/matched\/([^/]+?)\/?$/,
names: ['id'],
types: ['uuid']
},
'%23hash-encoded': {
pattern: /^\/%23hash-encoded\/?$/,
names: [],
types: []
},
'%40at-encoded/[id]': {
pattern: /^\/@at-encoded\/([^/]+?)\/?$/,
names: ['id'],
types: [undefined]
}
};

Expand Down

0 comments on commit ed013a9

Please sign in to comment.