Skip to content

Commit

Permalink
fix: successive optional route parameters can now be empty (#9266)
Browse files Browse the repository at this point in the history
* Optional chained parameters can be skipped - close #9261

* Update changeset description

Co-authored-by: Ben McCann <[email protected]>

---------

Co-authored-by: Ben McCann <[email protected]>
  • Loading branch information
glennsayers and benmccann authored Mar 2, 2023
1 parent 99c970f commit 1e2c826
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-guests-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: successive optional route parameters can now be empty
8 changes: 8 additions & 0 deletions packages/kit/src/utils/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ export function exec(match, params, matchers) {

if (!param.matcher || matchers[param.matcher](value)) {
result[param.name] = value;

// Now that the params match, reset the buffer if the next param isn't the [...rest]
// and the next value is defined, otherwise the buffer will cause us to skip values
const next_param = params[i + 1];
const next_value = values[i + 1];
if (next_param && !next_param.rest && next_value) {
buffered = 0;
}
continue;
}

Expand Down
10 changes: 10 additions & 0 deletions packages/kit/src/utils/routing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ const exec_tests = [
route: '/[[a=doesntmatch]]/[[b=matches]]/c',
path: '/a/b/c',
expected: undefined
},
{
route: '/[[slug1=matches]]/[[slug2=matches]]/constant/[[slug3=matches]]',
path: '/a/b/constant/c',
expected: { slug1: 'a', slug2: 'b', slug3: 'c' }
},
{
route: '/[[slug1=doesntmatch]]/[[slug2=matches]]/constant/[[slug3=matches]]',
path: '/b/constant/c',
expected: { slug2: 'b', slug3: 'c' }
}
];

Expand Down

0 comments on commit 1e2c826

Please sign in to comment.