Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spotty-bears-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: only match rest params with matchers when the matcher matches
8 changes: 6 additions & 2 deletions packages/kit/src/utils/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,12 @@ export function exec(match, params, matchers) {

// if `value` is undefined, it means this is an optional or rest parameter
if (value === undefined) {
if (param.rest) result[param.name] = '';
continue;
if (param.rest) {
// We need to allow the matcher to run so that it can decide if this optional rest param should be allowed to match
value = '';
} else {
continue;
}
}

if (!param.matcher || matchers[param.matcher](value)) {
Expand Down
20 changes: 20 additions & 0 deletions packages/kit/src/utils/routing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,26 @@ describe('exec', () => {
path: '/a/b/c',
expected: undefined
},
{
route: '/[...a=doesntmatch]/[b]',
path: '/foo',
expected: undefined
},
{
route: '/[...a=matches]/[b]',
path: '/foo',
expected: { a: '', b: 'foo' }
},
{
route: '/[...a=doesntmatch]/[b]/[c]',
path: '/foo/bar',
expected: undefined
},
{
route: '/[...a=matches]/[b]/[c]',
path: '/foo/bar',
expected: { a: '', b: 'foo', c: 'bar' }
},
{
route: '/[...catchall]',
path: '/\n',
Expand Down
Loading