Serving index path for single page application
Able to serve default index file like below:
app.at("/").get(|_| async { Ok(Body::from_file("public/index.html").await?) }).serve_dir("public/")?;
However for a single page application (spa) following routes should also serve index.html
- http://host:port/
- http://host:port/home
- http://host:port/about
- http://host:port/contact
In a spa the routes can change the url and user can share those urls to others (deep linking). Need such ability so that on can directly type in any of the above urls and it still renders index.html which handles the routes.
Is this possible?
I think you can use a param for this? /:page?
I think you can use a param for this?
/:page?
@Fishrock123 this is not about server side routes. They worked fine for me. What I mean is if you put url "http://host:port/home" or http://host:port/about/ in BROWSER it should still load index.html. And index.html will care of it as it will be a "reactjs" or "Angular" application.
Ok, I did try this. Seems to work. Didn't realize that before :)
app.at("/").get(|_| async { Ok(Body::from_file("public/index.html").await?) }).serve_dir("public/")?;
app.at("/:page").get(|_| async { Ok(Body::from_file("public/index.html").await?) }).serve_dir("public/")?;
For much deeper paths, like "customer/1002" or "/product/cat1/subcat2/1002" ; will I have to use multiple mappings (routes) to index.html like "/" & "/:page" & "/:page/:page" & "/:page/:page/:page" ? Is there a wildcard for this?
@abhijit-k33 you can use /* to get a wildcard mapping. But the flow you're describing is a rather common one, and I think we should at the very least do a better job documenting it, if not looking to how we can improve this experience.
@yoshuawuyts with /* one level paths work "/home", "/about" but not beyond that like "/product/cat1/1002"
If I am not missing something
@abhijit-k33 that's exactly how it should work. Partials are for parts; globs are to match entire urls. If it doesn't work that way that's a bug
I was expecting for /* or /** or /**/ it will match entire url with deep paths like /a/b/c/d/e but did not work.
a duplicate of #604 ?