Skip to content

Commit

Permalink
fix: allow optional param in middle of route (#10736)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldiekmeier authored Sep 19, 2023
1 parent 3b573dd commit 7ca6f01
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-geckos-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: allow optional param in middle of route
10 changes: 10 additions & 0 deletions packages/kit/src/utils/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export function exec(match, params, matchers) {
const result = {};

const values = match.slice(1);
const values_needing_match = values.filter((value) => value !== undefined);

let buffered = 0;

Expand Down Expand Up @@ -170,6 +171,15 @@ export function exec(match, params, matchers) {
if (next_param && !next_param.rest && next_param.optional && next_value && param.chained) {
buffered = 0;
}

// There are no more params and no more values, but all non-empty values have been matched
if (
!next_param &&
!next_value &&
Object.keys(result).length === values_needing_match.length
) {
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 @@ -197,6 +197,16 @@ const exec_tests = [
path: '/b/constant/c',
expected: { slug2: 'b', slug3: 'c' }
},
{
route: '/[[slug1=doesntmatch]]/[[slug2=matches]]/[[slug3=matches]]',
path: '/b/c',
expected: { slug2: 'b', slug3: 'c' }
},
{
route: '/[slug1]/[[lang=doesntmatch]]/[[page=matches]]',
path: '/a/2',
expected: { slug1: 'a', lang: undefined, page: '2' }
},
{
route: '/[[slug1=doesntmatch]]/[slug2=matches]/[slug3]',
path: '/a/b/c',
Expand Down

0 comments on commit 7ca6f01

Please sign in to comment.