leptos icon indicating copy to clipboard operation
leptos copied to clipboard

Setting trailing_slash=Strict does not work with nested Routes

Open metatoaster opened this issue 8 months ago • 3 comments

Describe the bug

It appears the trailing slash improvements provided by #2154 and #2217 still is not quite working as one might expect.

Leptos Dependencies

I've created a fork and built the reproducible code as an example called router_trailing_slash_axum, so the dependency is based directly on the current main branch (or more directly, its Cargo.toml).

To Reproduce

My goal is to have the example app provide an /item/ route that would provide a listing of all items, and individual items can be accessed using a route like /item/1/. I had tried the following definition to take advantage of the nested routes as per documentation.

#[component(transparent)]
pub fn ItemRoutes() -> impl IntoView {
    view! {
        <Route path="/item" view=ItemTop>
            <Route path="/:id/" view=ItemView trailing_slash=TrailingSlash::Exact/>
            <Route path="/" view=ItemListing trailing_slash=TrailingSlash::Exact/>
        </Route>
    }
}

What ended up happening was the route was generated as /item, without the trailing slash, made apparent by the dbg! output generated by the example:

[src/main.rs:13:5] &routes = [
    RouteListing {
        path: "/item/:id/",
        leptos_path: "/item/:id/",
        ...
    },
    RouteListing {
        path: "/item",
        leptos_path: "/item",
        ...
    },
    ...
]

As opposed to path: "/item/", and that only the http://localhost:3000/item route was usable, instead of http://localhost:3000/item/.

I've also tried <Route path="/item/" view=ItemTop trailing_slash=TrailingSlash::Exact> and have the inner route be <Route path="" ...> instead, and that actually made it worse as the /item route no longer functioned. There are a number of other combinations I've tried but none achieved what I really wanted.

Note that the A anchors do work as expected in this example.

Expected behavior

I would have expected the route /item/ be generated. Moreover, I wanted to use Redirect instead, but...

Additional context

This might be related to the axum integration, if Exact is replaced with with Redirect, duplicate routes will be generated for axum. While this is the desired behavior I do plan to have other layers be provided to axum which might be able to mitigate the redirect manually, but this failure to deal with exact trailing slashes is a complete showstopper for what I want to do (as the real application I am building has these specific routes I need working, for example, /path/target.zip should be able to download the zip file, /path/target.zip/ should hit the application to generate a listing of the archive, /path/target.zip/dir/ might list a directory of the archive (or automatically expand the tree at that location), /path/target.zip/dir/file might allow the download of that individual file within the archive.

metatoaster avatar Jun 11 '24 13:06 metatoaster