Skip to content

Commit 7ca6f01

Browse files
fix: allow optional param in middle of route (#10736)
1 parent 3b573dd commit 7ca6f01

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

.changeset/tricky-geckos-poke.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: allow optional param in middle of route

packages/kit/src/utils/routing.js

+10
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export function exec(match, params, matchers) {
136136
const result = {};
137137

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

140141
let buffered = 0;
141142

@@ -170,6 +171,15 @@ export function exec(match, params, matchers) {
170171
if (next_param && !next_param.rest && next_param.optional && next_value && param.chained) {
171172
buffered = 0;
172173
}
174+
175+
// There are no more params and no more values, but all non-empty values have been matched
176+
if (
177+
!next_param &&
178+
!next_value &&
179+
Object.keys(result).length === values_needing_match.length
180+
) {
181+
buffered = 0;
182+
}
173183
continue;
174184
}
175185

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

+10
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ const exec_tests = [
197197
path: '/b/constant/c',
198198
expected: { slug2: 'b', slug3: 'c' }
199199
},
200+
{
201+
route: '/[[slug1=doesntmatch]]/[[slug2=matches]]/[[slug3=matches]]',
202+
path: '/b/c',
203+
expected: { slug2: 'b', slug3: 'c' }
204+
},
205+
{
206+
route: '/[slug1]/[[lang=doesntmatch]]/[[page=matches]]',
207+
path: '/a/2',
208+
expected: { slug1: 'a', lang: undefined, page: '2' }
209+
},
200210
{
201211
route: '/[[slug1=doesntmatch]]/[slug2=matches]/[slug3]',
202212
path: '/a/b/c',

0 commit comments

Comments
 (0)