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

Router/Routing can't handle enter url from address bar or pressing refresh button #771

Closed
thienpow opened this issue Dec 4, 2019 · 5 comments
Labels
A-examples Area: The examples

Comments

@thienpow
Copy link

thienpow commented Dec 4, 2019

tried running the Routing example, it will return error Cannot GET /a if you enter the URL http://localhost:8000/a manually.

how to handle this if user copied/bookmarked the url and trying to return to this page?

@jstarry jstarry added the A-examples Area: The examples label Dec 4, 2019
@jstarry
Copy link
Member

jstarry commented Dec 4, 2019

@hgzimmerman do you mind looking into this?

@hgzimmerman
Copy link
Member

This is expected behavior.

Either you need to prepend all routes with a #, which causes everything after it to not be sent to the server, or you need the server to return the index.html's content when you encounter URL for a resource what would otherwise be a 404 (eg, one of your routes).

Using # is generally a bad practice (or is at the very least dated), and cargo web doesn't support the 404 "redirect" trick.
Parcel does support the 404 trick out of the box, and I can't say for the WebPack template.

I think the router example can be removed from this project given that a dedicated crate exists for routing.
I think that the router repo should have a couple of example servers (warp, actix, rocket) showing how to write your server so it has this behavior.

@thienpow
Copy link
Author

thienpow commented Dec 7, 2019

thanks @hgzimmerman i finally got it working with #
i think i understand how to handle the /path/subpath/ at webserver if not using #

btw i was not able to get the tag response to the change of route by following the sample in example/router_compoent folder, so i am using this
RouterAgent::bridge method as shown in the image

Screenshot 2019-12-07 at 1 28 02 PM

@hgzimmerman
Copy link
Member

I think that this can be closed now, given that an example server exists in the router repo, and that better support for fragment routing is planned. The question author appears to have come to a suitable solution.

@fanux
Copy link

fanux commented Apr 30, 2020

I still 404 after click browser refresh ...

    #[to = "#/app/{appName}"]
    App(String),
    #[to = "#/a/{anything}"]
    A(String),
    #[to = "#/b/{anything}/{number}"]
    B { anything: String, number: u32 },
    #[to = "#/c"]
    C,
                    AppRoute::App(s) => format!("#/app/{}", s),
                    AppRoute::A(s) => format!("#/a/{}", s),
                    AppRoute::B { anything, number } => format!("#/b/{}/{}", anything, number),
                    AppRoute::C => "#/c".to_string(),
                   match AppRoute::switch(self.route.clone()) {
                        Some(AppRoute::App(thing)) => self.app_info(thing),
                        Some(AppRoute::A(thing)) => VNode::from(thing.as_str()),
                        Some(AppRoute::B{anything, number}) => html!{<div> {anything} {number} </div>},
                        Some(AppRoute::C) => self.apps(),
                        None => VNode::from("404")
                    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-examples Area: The examples
Projects
None yet
Development

No branches or pull requests

4 participants