Skip to content

Commit 78404df

Browse files
authored
fix: correctly match route groups preceding optional parameters (#13099)
fixes #13095 This PR changes the RegExp to ensure that routes such as /[[optional]]/(group) are treated the same as [[optional]]. Previously, /[[optional]]/(group) was being treated the same as / which is incorrect since it has an optional segment. It wasn't recognised as /[[optional]] because the RegExp didn't consider the route group preceding it (it only looked for the end of the string).
1 parent 38d65e3 commit 78404df

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

.changeset/fifty-cars-lie.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: correctly match route groups preceding optional parameters

packages/kit/src/core/sync/create_manifest_data/index.spec.js

+42
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,48 @@ test('nested optionals', () => {
412412
]);
413413
});
414414

415+
test('group preceding optional parameters', () => {
416+
const { nodes, routes } = create('samples/optional-group');
417+
418+
expect(
419+
nodes
420+
.map(simplify_node)
421+
// for some reason linux and windows have a different order, which is why
422+
// we need sort the nodes using a sort function (doesn't work either without),
423+
// resulting in the following expected node order
424+
.sort((a, b) => a.component?.localeCompare(b.component ?? '') ?? 1)
425+
).toEqual([
426+
default_error,
427+
default_layout,
428+
{
429+
component: 'samples/optional-group/[[optional]]/(group)/+page.svelte'
430+
}
431+
]);
432+
433+
expect(routes.map(simplify_route)).toEqual([
434+
{
435+
id: '/',
436+
pattern: '/^/$/'
437+
},
438+
{
439+
id: '/[[optional]]/(group)',
440+
pattern: '/^(?:/([^/]+))?/?$/',
441+
page: {
442+
layouts: [0],
443+
errors: [1],
444+
// see above, linux/windows difference -> find the index dynamically
445+
leaf: nodes.findIndex((node) =>
446+
node.component?.includes('optional-group/[[optional]]/(group)')
447+
)
448+
}
449+
},
450+
{
451+
id: '/[[optional]]',
452+
pattern: '/^(?:/([^/]+))?/?$/'
453+
}
454+
]);
455+
});
456+
415457
test('ignores files and directories with leading underscores', () => {
416458
const { routes } = create('samples/hidden-underscore');
417459

packages/kit/src/core/sync/create_manifest_data/sort.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ function split_route_id(id) {
136136
return get_route_segments(
137137
id
138138
// remove all [[optional]] parts unless they're at the very end
139-
.replace(/\[\[[^\]]+\]\](?!$)/g, '')
139+
// or it ends with a route group
140+
.replace(/\[\[[^\]]+\]\](?!(?:\/\([^/]+\))*$)/g, '')
140141
).filter(Boolean);
141142
}
142143

packages/kit/src/core/sync/create_manifest_data/test/samples/optional-group/[[optional]]/(group)/+page.svelte

Whitespace-only changes.

0 commit comments

Comments
 (0)