Comments (13)
@NfNitLoop You are correct that the router's current behavior includes normalizing paths in a way that removes trailing /
.
@benwis You are also correct that there is a difference in behavior between Actix & Axum here. If you define a Route /foo/
:
- Axum (or
leptos_axum
) matches/foo
or/foo/
, for initial render - Actix (or
leptos_actix
) matches/foo
only, not/foo/
, for initial render - Client-side routing always navigates to
/foo
if, for example, you click a link to/foo/
or/foo
So the current behavior is perfectly consistent with itself, but not with all possible URL schemes (such as including trailing slashes).
I haven't thought that much about what it would require to support trailing slashes, but I'm sure it's possible. If you'd be interested in making a PR I'd be happy to see it.
from leptos.
@paul-hansen Try against the branch for PR #2217.
from leptos.
Unfortunately your proposal is not possible, what happens to routes is governed by actix/axum. Being not an Actix user, a quick doc skim suggests that they do consider /foo and /foo/ to be different routes. See their docs and middleware for one example of that: https://cseweb.ucsd.edu/classes/sp22/cse223B-a/tribbler/actix_web/middleware/struct.NormalizePath.html
Assuming actix is behaving how you want, it's possible the actix->leptos router conversion function doesn't handle that correctly. That can be found here:
leptos/integrations/actix/src/lib.rs
Line 941 in 16cf3c4
So assuming in the Actix Router you specify a path for /foo/*
or /foo/:file
that serves a file and in Leptos Router a path for /foo
I imagine it should just work
from leptos.
Unfortunately your proposal is not possible
It's possible, the implementation would just need to change.
I'll dig into actix integration and check what it's doing, but if:
they do consider /foo and /foo/ to be different routes
… and I specify <Route path="/foo/" trailingSlash="always" ...>
, then generate_route_list()
could just add two routes:
/foo
is a route that just redirects the browser to/foo/
/foo/
serves my Route view.
from leptos.
I just hit this issue as well, but for me it is enough to fix client-side routing to retain trailing slash if it's there. So I fiddled with leptos_router
a bit and it works now :) Just a bit ugly ATM. I changed matching::resolve_path
to use a different normalize
function that doesn't do .trim_end_matches('/')
. normalize
is also used by join_paths
which depends on trailing slash removal though, so that breaks quite spectacularly if I just change normalize
implementation.
Anyway, the version of normalize
that I use in resolve_path
looks like this:
fn normalize_for_resolve(path: &str, omit_slash: bool) -> Cow<'_, str> {
let s = path.trim_start_matches('/');
if s.is_empty() || omit_slash || begins_with_query_or_hash(s) {
s.into()
} else {
format!("/{s}").into()
}
}
from leptos.
@gbj do you see any issues with this approach? Otherwise, I could open a PR once I tidy this up and add a few tests.
from leptos.
@skirsdeda While it doesn't address the full scope of this issue (I still would like support for redirecting to URLs with/without slashes), I'd definitely be interested in seeing your fix. It may unblock me for maintaining URL compatibility w/ my existing project, and I can just put in /foo
→ /foo/
redirects manually for now.
Did you account for not adding a redundant /
when concatenating inner routes, now that they'er not being trimmed?
from leptos.
@NfNitLoop there's also a workaround for links with current implementation. Just add non-empty query string or hash fragment and trailing slash is retained.
from leptos.
Learning the Leptos codebase when testing @skirsdeda's changes in #2163.
That's a good place to branch off of w/ an experimental implementation of my feature request.
I think I'll offer a few options on <Router>
and <Route>
.
-
TrailingSlash::Drop
-- default/legacy behavior, trailing slashes get stripped from path patterns. Route paths with a trailing slash are not possible. -
TralingSlash::Redirect
-- If the client/server receive a request that is off by just the trailing slash, issue a redirect/navigate to the correct URL. (My preferred user-friendly & SEO-friendly option. Maybe we can make this the default in a future major version?) -
TrailingSlash::Exact
-- Keep the trailing slash (or lack thereof), and don't do any redirects to fix it up for users.
from leptos.
First pass at an implementation is up. Let me know what you think!
Should I spend some more time to get this ready to be merged? Or am I going off in the wrong direction?
from leptos.
I believe we can close this one now that #2172 is merged. Feel free to comment if that's not so
from leptos.
@benwis @gbj since #2172 was reverted in 0.5.7 and isn't in 0.6.2 or main is it no longer planned? Just curious if I need to re-implement the workaround I had for this in my app.
Basing my assumption that it isn't in 0.6.2 from it not showing up in the docs: https://docs.rs/leptos_router/0.6.2/leptos_router/?search=leptos_router%3A%3ATrailingSlash
from leptos.
Ah I missed it thanks!
from leptos.
Related Issues (20)
- Inconsistent behaviour for buttons with formmethod="dialog" rendered on the client HOT 1
- NodeRef on_load panics when ref is remounted HOT 2
- DX regression in view macro for rust-analyzer 2024-03-18 (v0.3.1885) and newer HOT 5
- Access environment variables in CSR HOT 1
- There is a bug in Safari browser where signals cannot be transmitted properly HOT 1
- DynChild is populated as a single Child in the components' children property HOT 4
- Breaking change in view macro between 0.6.10 and 0.6.11
- Allow custom derive for server_fns with custom encoding HOT 3
- Add Trigger::dispose
- leptos_macro 0.6.11 leptos::view! breaks with some comments
- Non-'static signals
- the format!() bug in <a href={format!()}> HOT 2
- Effects/Memos should't be able to "own" a signal HOT 4
- Nested Suspense ignores SSR Mode and breaks Hydration HOT 2
- Error when running server function inside spawn_local HOT 5
- Click events are registered twice when two leptos custom elements are on a page HOT 3
- about mobile support
- Recursive effects never run after recursing HOT 2
- Clippy warnings in some components with rust 1.78 HOT 1
- rkyv, avoid the copy to byte ? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leptos.