Git Product home page Git Product logo

Comments (13)

gbj avatar gbj commented on May 20, 2024 2

@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.

gbj avatar gbj commented on May 20, 2024 1

@paul-hansen Try against the branch for PR #2217.

from leptos.

benwis avatar benwis commented on May 20, 2024

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:

pub fn generate_route_list_with_exclusions_and_ssg_and_context<IV>(

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.

NfNitLoop avatar NfNitLoop commented on May 20, 2024

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.

skirsdeda avatar skirsdeda commented on May 20, 2024

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.

skirsdeda avatar skirsdeda commented on May 20, 2024

@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.

NfNitLoop avatar NfNitLoop commented on May 20, 2024

@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.

skirsdeda avatar skirsdeda commented on May 20, 2024

@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.

NfNitLoop avatar NfNitLoop commented on May 20, 2024

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.

NfNitLoop avatar NfNitLoop commented on May 20, 2024

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.

benwis avatar benwis commented on May 20, 2024

I believe we can close this one now that #2172 is merged. Feel free to comment if that's not so

from leptos.

paul-hansen avatar paul-hansen commented on May 20, 2024

@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.

paul-hansen avatar paul-hansen commented on May 20, 2024

Ah I missed it thanks!

from leptos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.