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

Error on conflicting routes #7304

Merged
merged 16 commits into from
Oct 18, 2022
Merged

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Oct 18, 2022

Yet another follow-up to #7051, which can be considered an alternative to #7278. It reverts to the simpler algorithm initially present in that PR, but adds a check for routes that are in conflict with each other (for example x/[[y]] and [[y]]/x, which can't meaningfully coexist) so that the algorithm doesn't need to consider the additional cases added later in the PR.

The one case it doesn't handle is x/[...rest] vs [...rest]/x (or x/[[optional]] vs [...rest]/x), which is the exception to the rule that a route ending with a rest/optional parameters comes after an otherwise-equivalent route. I'm hopeful that we can solve that case without introducing a lot more complexity, but I haven't nailed it yet.

Another test that fails (both in this PR and in #7278) is foo/bar[...rest] vs foo/[...rest]bar.

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. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Oct 18, 2022

⚠️ No Changeset found

Latest commit: 304b493

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dummdidumm
Copy link
Member

foo/bar[...rest] vs foo/[...rest]bar -> is that even allowed? Can rest params appear as part of a segment?

(if they can't, there's still foo/bar[[optional]] vs foo/[[optional]]bar, which .. should probably be a route conflict?)

@Rich-Harris
Copy link
Member Author

is that even allowed? Can rest params appear as part of a segment?

It is, yeah — it's come up in the past for things like [...path].json#4656. Less likely that you'd need a route like that since the big loading revamp a few months back, but still a legitimate use case.

foo/bar[[optional]] and foo/[[optional]]bar would be treated as a conflict. The way this PR detects conflicts is it normalizes routes by replacing [param] with <*> and [param=x] with <x> and [[param]] with <?*> and so on...

  1. foo/bar<?*>
  2. foo/<?*>bar

...then figures out all the permutations of those routes:

  1. foo/bar
  2. foo/bar<*>
  3. foo/<*>bar
  4. foo/bar

Since 3 (derived from 1) and 6 (derived from 2) are identical, those routes are identified as being incompatible.

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

The only thing I'm not sure about - but relaxing this is always possible later in a non-breaking way - is the very strict conflict check.

Imagine you have a/[[b]]/c/[[d]] and a/c as routes. Under this algorithm, this is a conflict, but the user could say "yes I know that that's a conflict in one of these cases, but I don't want to duplicate routes into a/[b]/c/[d], a/[b]/c/ and a/c/[d] (because a/[b]/c/[[d]] and a/[[b]]/c/[d] would also be a conflict in one case).

I don't know how a algorithm that relaxes this and at the same time sorts these things correctly without bringing the complexity of that other PR back would look like (probably something like "if this optional route only has one nn-conflicting permutation left it's an error), so I'm giving my approval regardless of this wrinkle. You can decide if this overly strict check is reason enough to use more complex sorting logic after all like in the other PR or not. I haven't decided yet.

@Rich-Harris
Copy link
Member Author

Ah, that's an interesting case. It seems like a fairly unlikely one, but if it did arise then it would be possible to work around, or we could relax the conflict detection as you say. Aside from that edge case I think having the conflict detection is useful regardless (e.g. it found a conflicting route in the test app that wasn't doing any work)

@Rich-Harris Rich-Harris merged commit d4a79af into optional-route-params Oct 18, 2022
@Rich-Harris Rich-Harris deleted the optional-route-params-3 branch October 18, 2022 16:22
@Rich-Harris Rich-Harris mentioned this pull request Oct 18, 2022
5 tasks
Rich-Harris added a commit that referenced this pull request Oct 19, 2022
* [feat] implement optional route params

Closes #5072

* docs

* param fixes, tidy up, more tests

* more tests, move some validations, sorting

* better regex

* sort to fix test

* next try

* fix validation

* next try

* Update documentation/docs/04-advanced-routing.md

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>

* simplify

* disallow ambiguity in both directions

* Error on conflicting routes (#7304)

* more sorting logic into one place

* better sorting algorithm

* update tests

* tidy up

* use an array instead of a map

* remove unused test files

* relax constraint prohibiting [[optional]]/[...rest]

* update docs

* docs

* fix disambiguation between x/y and x/[...rest]/y

* more intelligent conflict detection

* more accurate error message

* fix regex

* remove conflicting route from test app

* handle edge case

* oops

* introcuce randomness to make test fail

* fix conflict detection

* make input valid

* remove obsolete note

* more tests

* i think this is it? fingers crossed

* Update documentation/docs/04-advanced-routing.md

* changeset

* Update documentation/docs/04-advanced-routing.md

Co-authored-by: Conduitry <git@chor.date>

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Co-authored-by: Rich Harris <hello@rich-harris.dev>
Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
Co-authored-by: Conduitry <git@chor.date>
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.

2 participants