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

Better handling for trailing slashes. (#2154) #2172

Merged
merged 12 commits into from
Jan 11, 2024

Conversation

NfNitLoop
Copy link
Contributor

This adds a trailing_slash option to <Router> and <Route>.

By default, this option is backward compatible with current Leptos
behavior, but users can opt into two new modes for handling trailing
slashes.

skirsdeda and others added 3 commits January 5, 2024 23:57
This adds a trailing_slash option to <Router> and <Route>.

By default, this option is backward compatible with current Leptos
behavior, but users can opt into two new modes for handling trailing
slashes.
@NfNitLoop
Copy link
Contributor Author

NfNitLoop commented Jan 8, 2024

Things still WIP:

  • A few TODOs
  • Figure out a way to test redirects in unit tests. (They're working for me in Actix locally.)
  • Are there any tests I'm forgetting to run? cargo +nightly test doesn't work for me on main. So I've been running it in the leptos/, router/ and integrations/actix/ subdirectories.
  • Confirm: Is this going in the right direction? Should I spend more time on this?

@NfNitLoop NfNitLoop changed the title Better handling for trailing slashes. (#2154)Trailing slashes Better handling for trailing slashes. (#2154) Jan 8, 2024
@NfNitLoop
Copy link
Contributor Author

NfNitLoop commented Jan 8, 2024

Link to #2154 (the related feature request), please, GitHub! 😊

@rakshith-ravi
Copy link
Contributor

rakshith-ravi commented Jan 8, 2024

Quick question - is this the same as #2163?

@NfNitLoop
Copy link
Contributor Author

@rakshith-ravi #2163 isn't by me, it's by @skirsdeda. But we were discussing it as an incremental approach to this larger feature implementation in the feature request, so I provided some feedback over there and based these changes atop that work.

IMO we probably shouldn't merge #2163 in its current state. Without 10915f9 the front- and back-end routing are slightly different.

@rakshith-ravi
Copy link
Contributor

Yeah I got confused with the issue numbers for a second there. Like I said, super sleep deprived rn 😅

so I provided some feedback over there and based these changes atop that work.

Ahh, got it. Greg will have a better idea about this, but just my two cents - how about y'all put a PR on each other's branches and then have the complete slash handling as a single PR to main? That way, it'll be a feature complete PR.

On the flip side, it would mean a larger PR, so reviewing it might be difficult. Idk. Just putting both sides of the argument

@NfNitLoop
Copy link
Contributor Author

how about y'all put a PR on each other's branches and then have the complete slash handling as a single PR to main?

This is the full PR. Minus a couple TODOs and any suggestions I get from maintainers. 😊

@benwis
Copy link
Contributor

benwis commented Jan 8, 2024

how about y'all put a PR on each other's branches and then have the complete slash handling as a single PR to main?

This is the full PR. Minus a couple TODOs and any suggestions I get from maintainers. 😊

This looks pretty solid to me, I've started the checks. I think we should add some mention to the interplay between the Axum and Actix routers, and the Leptos one. Does Axum behave the same way?

@benwis
Copy link
Contributor

benwis commented Jan 8, 2024

Probably also want to run cargo clippy against your repo

@NfNitLoop
Copy link
Contributor Author

NfNitLoop commented Jan 9, 2024

  • Blocker: In testing, I found that these changes break route matching for "" (leptos) and "/" (web servers). I'm capturing that in some tests and will devise a fix but probably not until tomorrow night.

@NfNitLoop
Copy link
Contributor Author

Are all of these test failures in CI valid failures that I need to look into? Or are some just known to be flakey?

@benwis
Copy link
Contributor

benwis commented Jan 9, 2024

Are all of these test failures in CI valid failures that I need to look into? Or are some just known to be flakey?

@gbj is probably more familiar than I, but some of the examples test suites are failing now. Is this common?

@gbj
Copy link
Collaborator

gbj commented Jan 9, 2024

I'll have the CI rerun the failed tasks just in case. I would need to check out the branch and test out the examples myself to give a definitive answer, but I'm seeing failing e2e tests on several examples that do involve routing, so it's worth checking whether those examples actually work with the changes.

@NfNitLoop
Copy link
Contributor Author

NfNitLoop commented Jan 10, 2024

OK, thanks. I hope to have more time to look into these tonight. Hopefully it's just the known issue with "/" and "" routes breaking that I mentioned above.

maybe TMI but just to share:

I've already looked into that a bit and found it was because of the changes to path normalization. The problem is that path normalization works the same everywhere … except root paths. For both Actix and Axum, (and HTTP/HTML, AFAICT) root paths of "/" and "" are equivalent. So the error comes when Leptos specifies a root path of "", but the web server gives us a path of "/", which Leptos doesn't find a match for (in this branch, given its more precise path matching.) I've got a couple different fixes I'm testing out.

This path is the exception to the rule and *should* be treated
as equivalent regardless of its trailing slash.
@NfNitLoop
Copy link
Contributor Author

OK, I've done local testing with Actix and Axum and have fixed my blocking issue. I bet that was the breaking issue for those integration tests, given they couldn't even render the first page of the site.🤞

I started down the path of trying to run the tests for myself locally but it looks like they require manual installation of some test tools and it's getting late here. If one of y'all can kick these off tonight/tomorrow I'll come back and check results here. Thank you! 😊

@benwis
Copy link
Contributor

benwis commented Jan 11, 2024

OK, I've done local testing with Actix and Axum and have fixed my blocking issue. I bet that was the breaking issue for those integration tests, given they couldn't even render the first page of the site.🤞

I started down the path of trying to run the tests for myself locally but it looks like they require manual installation of some test tools and it's getting late here. If one of y'all can kick these off tonight/tomorrow I'll come back and check results here. Thank you! 😊

Looks promising, although it seems to have failed a cargo fmt check

@NfNitLoop
Copy link
Contributor Author

it seems to have failed a cargo fmt check

Weird, I would've sworn I ran cargo fmt before pushing my last commit lsat night. 🤷

Thankfully that's an easy one to fix! I assume/hope we'll squash merge this messy history away? 😅

@benwis benwis merged commit 1eaf886 into leptos-rs:main Jan 11, 2024
10 checks passed
@NfNitLoop
Copy link
Contributor Author

And merged? Just like that! Thank you so much! I'll definitely be using this in my own project(s)! 😊

@benwis
Copy link
Contributor

benwis commented Jan 11, 2024 via email

@gbj
Copy link
Collaborator

gbj commented Jan 19, 2024

@NfNitLoop @benwis Sorry to say this PR broke the router in a way that unfortunately was not caught by the existing CI setup.

Two examples:

  1. This valid approach for defining a param, and a fallback if the param was not provided, now panics at runtime when Axum tries to create duplicate routes
<Routes>
  <Route path="bar" view=|| view! { <Outlet/> }>
    <Route path="" view=|| view! { <p>"Empty"</p>}/>
    <Route path=":id" view=|| view! { <p>"ID"</p>}/>
  </Route>
</Routes>
  1. Similarly, it breaks matching when there is a param at the root level, as in the router example in the repo or in Incorrect route gets rendered #2201

Given the breakages in existing code I'm going to have to revert this and yank the last two releases, since I unfortunately didn't catch it before then. If you can find a way to implement this that does not cause these issues, I will be happy to review a new PR.

gbj added a commit that referenced this pull request Jan 19, 2024
skirsdeda added a commit to skirsdeda/leptos that referenced this pull request Jan 21, 2024
* retain trailing slashes in paths but leave matching trail-slash-insensitive

* fix: Allow trailing slashes to remain in leptos_path.

* Better handling for trailing slashes. (leptos-rs#2154)

This adds a trailing_slash option to <Router> and <Route>.

By default, this option is backward compatible with current Leptos
behavior, but users can opt into two new modes for handling trailing
slashes.

* cargo fmt

* Fix redirect routes for wildcard patterns.

* Clippy fixies

* (Re)Reduce the scope of PossibleBranchContext's internals.

* Test real code, not copied code.

* Test TrailingSlash redirects.

* Fixes and more tests for matching "" && "/".

This path is the exception to the rule and *should* be treated
as equivalent regardless of its trailing slash.

* cargo fmt

---------

Co-authored-by: Tadas Dailyda <tadas@dailyda.com>
skirsdeda added a commit to skirsdeda/leptos that referenced this pull request Jan 21, 2024
* retain trailing slashes in paths but leave matching trail-slash-insensitive

* fix: Allow trailing slashes to remain in leptos_path.

* Better handling for trailing slashes. (leptos-rs#2154)

This adds a trailing_slash option to <Router> and <Route>.

By default, this option is backward compatible with current Leptos
behavior, but users can opt into two new modes for handling trailing
slashes.

* cargo fmt

* Fix redirect routes for wildcard patterns.

* Clippy fixies

* (Re)Reduce the scope of PossibleBranchContext's internals.

* Test real code, not copied code.

* Test TrailingSlash redirects.

* Fixes and more tests for matching "" && "/".

This path is the exception to the rule and *should* be treated
as equivalent regardless of its trailing slash.

* cargo fmt

---------

Co-authored-by: Tadas Dailyda <tadas@dailyda.com>
skirsdeda added a commit to skirsdeda/leptos that referenced this pull request Feb 18, 2024
* retain trailing slashes in paths but leave matching trail-slash-insensitive

* fix: Allow trailing slashes to remain in leptos_path.

* Better handling for trailing slashes. (leptos-rs#2154)

This adds a trailing_slash option to <Router> and <Route>.

By default, this option is backward compatible with current Leptos
behavior, but users can opt into two new modes for handling trailing
slashes.

* cargo fmt

* Fix redirect routes for wildcard patterns.

* Clippy fixies

* (Re)Reduce the scope of PossibleBranchContext's internals.

* Test real code, not copied code.

* Test TrailingSlash redirects.

* Fixes and more tests for matching "" && "/".

This path is the exception to the rule and *should* be treated
as equivalent regardless of its trailing slash.

* cargo fmt

---------

Co-authored-by: Tadas Dailyda <tadas@dailyda.com>
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.

None yet

5 participants