kit
kit copied to clipboard
fix: ensure optional routes with matchers and catch-all match correctly
This is a continuation of #9979. Fixes #9974.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- [x] This message body should clearly illustrate what problems it solves.
- [x] Ideally, include a test that fails without this PR but passes with it.
Tests
- [x] Run the tests with
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
- [x] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.
🦋 Changeset detected
Latest commit: 0148cf84a2e1e6bfed18881ad04d49046bf3f406
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
Name | Type |
---|---|
@sveltejs/kit | Patch |
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR
GitHub's UI sucks, so I can't actually make this suggestion in a useful way, but we should replace the added tests with the following. Little DRY-er. You may have to fix prettier errors when pasting. For some reason I can't open a PR targeting this branch, either.
const simple_tests = [
{
route: '/blog/[[slug]]/sub[[param]]',
path: '/blog/sub',
expected: {}
},
{
route: '/blog/[[slug]]/sub[[param]]',
path: '/blog/slug/sub',
expected: { slug: 'slug' }
},
{
route: '/blog/[[slug]]/sub[[param]]',
path: '/blog/slug/subparam',
expected: { slug: 'slug', param: 'param' }
},
{
route: '/blog/[[slug]]/sub[[param]]',
path: '/blog/subparam',
expected: { param: 'param' }
},
{
route: '/[[slug]]/[...rest]',
path: '/slug/rest/sub',
expected: { slug: 'slug', rest: 'rest/sub' }
},
{
route: '/[[slug]]/[...rest]',
path: '/slug/rest',
expected: { slug: 'slug', rest: 'rest' }
},
{
route: '/[[slug]]/[...rest]',
path: '/slug',
expected: { slug: 'slug', rest: '' }
},
{
route: '/[[slug]]/[...rest]',
path: '/',
expected: { rest: '' }
},
{
route: '/[...rest]/path',
path: '/rest/path',
expected: { rest: 'rest' }
},
{
route: '/[[slug1]]/[[slug2]]',
path: '/slug1/slug2',
expected: { slug1: 'slug1', slug2: 'slug2' }
},
{
route: '/[[slug1]]/[[slug2]]',
path: '/slug1',
expected: { slug1: 'slug1' }
},
{
route: '/[[slug1=matches]]',
path: '/',
expected: {}
},
{
route: '/[[slug1=doesntmatch]]',
path: '/',
expected: {}
},
{
route: '/[[slug1=matches]]/[[slug2=doesntmatch]]',
path: '/foo',
expected: { slug1: 'foo' }
},
{
route: '/[[slug1=doesntmatch]]/[[slug2=doesntmatch]]',
path: '/foo',
expected: undefined
},
{
route: '/[...slug1=matches]',
path: '/',
expected: { slug1: '' }
},
{
route: '/[...slug1=doesntmatch]',
path: '/',
expected: undefined
},
{
route: '/[[slug=doesntmatch]]/[...rest]',
path: '/foo',
expected: { rest: 'foo' }
},
{
route: '/[[slug1=doesntmatch]]/[slug2]/[...rest]',
path: '/foo/bar/baz',
expected: { slug2: 'foo', rest: 'bar/baz' }
},
{
route: '/[[slug1=doesntmatch]]/[slug2]/[...rest]/baz',
path: '/foo/bar/baz',
expected: { slug2: 'foo', rest: 'bar' }
},
{
route: '/[[a=doesntmatch]]/[[b=doesntmatch]]/[[c=doesntmatch]]/[...d]',
path: '/a/b/c/d',
expected: { d: 'a/b/c/d' }
},
{
route: '/[[a=doesntmatch]]/[b]/[...c]/[d]/e',
path: '/foo/bar/baz/qux/e',
expected: { b: 'foo', c: 'bar/baz', d: 'qux' }
},
{
route: '/[[slug1=doesntmatch]]/[[slug2=doesntmatch]]/[...rest]',
path: '/foo/bar/baz',
expected: { rest: 'foo/bar/baz' }
},
{
route: '/[[slug1=doesntmatch]]/[[slug2=matches]]/[[slug3=doesntmatch]]/[...rest].json',
path: '/foo/bar/baz.json',
expected: { slug2: 'foo', rest: 'bar/baz' }
},
{
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' }
},
{
route: '/[[slug1=doesntmatch]]/[slug2=matches]/[slug3]',
path: '/a/b/c',
expected: undefined
},
{
route: '/[[lang=doesntmatch]]/[asset=matches]/[[categoryType]]/[...categories]',
path: '/music',
expected: { asset: 'music', categories: '' }
},
{
route: '/[[lang=doesntmatch]]/[asset=matches]/[[categoryType]]/[...categories]',
path: '/music/genre',
expected: { asset: 'music', categoryType: 'genre', categories: '' }
},
{
route: '/[[lang=doesntmatch]]/[asset=matches]/[[categoryType]]/[...categories]',
path: '/music/genre/rock',
expected: { asset: 'music', categoryType: 'genre', categories: 'rock' }
},
{
route: '/[[lang=doesntmatch]]/[asset=matches]/[[categoryType]]/[...categories]',
path: '/sfx/category/car/crash',
expected: { asset: 'sfx', categoryType: 'category', categories: 'car/crash' }
},
{
route: '/[[lang=matches]]/[asset=matches]/[[categoryType]]/[...categories]',
path: '/es/sfx/category/car/crash',
expected: { lang: 'es', asset: 'sfx', categoryType: 'category', categories: 'car/crash' }
}
];
for (const { path, route, expected } of simple_tests) {
test(`exec extracts params correctly for ${path} from ${route}`, () => {
const { pattern, params } = parse_route_id(route);
const match = pattern.exec(path);
if (!match) throw new Error(`Failed to match ${path}`);
const actual = exec(match, params, {
matches: () => true,
doesntmatch: () => false
});
expect(actual).toEqual(expected);
});
}
@tcc-sejohnson Tests updated! Thank you
For some reason I can't open a PR targeting this branch, either
Not sure if this is what you tried, but you'd have to do it in the https://github.com/Vextil/kit repo probably since that's where the code lives, so I expect you couldn't do it in this repository
Not sure if this is what you tried, but you'd have to do it in the https://github.com/Vextil/kit repo probably since that's where the code lives, so I expect you couldn't do it in this repository
Yeah, tried that -- but it didn't show up as a valid base in the PR interface on GH, so I'm guessing it was either a permissions issue or some eventual consistency lag.
Yeah, tried that -- but it didn't show up as a valid base in the PR interface on GH, so I'm guessing it was either a permissions issue or some eventual consistency lag.
The way I do it is that I clone https://github.com/Vextil/kit. You can push directly to it or open a PR there. I've never tried to open a PR to something like https://github.com/Vextil/kit from the UI on https://github.com/sveltejs/kit
I'm going to merge this cause @tcc-sejohnson told me it was good. Everyone blame him if it breaks :stuck_out_tongue_closed_eyes: Only kidding...
I honestly haven't sat down to understand this section of code, but this PR looks pretty well tested, so as long as we keep adding good tests like this we should be reducing edge cases