Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: successive optional route parameters can now be empty #9266

Merged
merged 2 commits into from
Mar 2, 2023
Merged

fix: successive optional route parameters can now be empty #9266

merged 2 commits into from
Mar 2, 2023

Conversation

glennsayers
Copy link
Contributor

@glennsayers glennsayers commented Mar 1, 2023

Closes #9261

In the scenario outlined in the linked issue, with 2 chained optional parameters, skipping the first optional parameter would result in a 404.
When the first optional parameter was detected as being missing, a buffer variable was incremented to allow the 2nd parameter to be correctly assigned. However this buffer was never reset (except in the case of a [...rest] parameter being present). This was triggering the if(buffered) return; check, resulting in a 404.

This change resets the buffer back to 0 in the appropriate circumstance.
The check verifies that the next parameter is not a [...rest] parameter. If it is, we don't reset the buffer in order to trigger the coalescing of remaining values into [...rest].
If it's not a rest parameter, and the next value is defined, then we should reset the buffer to prevent any data loss. This scenario is tested in the following existing test:

{
	route: '/[[slug1=doesntmatch]]/[[slug2=matches]]/[[slug3=doesntmatch]]/[...rest].json',
	path: '/foo/bar/baz.json',
	expected: { slug2: 'foo', rest: 'bar/baz' }
},

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • 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
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • 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 be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
@benmccann benmccann changed the title Optional chained parameters can be skipped - close #9261 fix: successive optional route parameters can now be empty Mar 1, 2023
@Rich-Harris Rich-Harris merged commit 1e2c826 into sveltejs:master Mar 2, 2023
@Rich-Harris
Copy link
Member

amazing, thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optional parameters chain - 404 error
3 participants