tide icon indicating copy to clipboard operation
tide copied to clipboard

Serving index path for single page application

Open abhijit-k33 opened this issue 5 years ago • 9 comments

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?

abhijit-k33 avatar Oct 26 '20 06:10 abhijit-k33

I think you can use a param for this? /:page?

Fishrock123 avatar Oct 28 '20 02:10 Fishrock123

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.

abhijit-k33 avatar Oct 28 '20 04:10 abhijit-k33

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/")?;

abhijit-k33 avatar Oct 28 '20 05:10 abhijit-k33

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 avatar Oct 28 '20 06:10 abhijit-k33

@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 avatar Oct 28 '20 11:10 yoshuawuyts

@yoshuawuyts with /* one level paths work "/home", "/about" but not beyond that like "/product/cat1/1002" If I am not missing something

abhijit-k33 avatar Oct 29 '20 05:10 abhijit-k33

@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

yoshuawuyts avatar Oct 29 '20 17:10 yoshuawuyts

I was expecting for /* or /** or /**/ it will match entire url with deep paths like /a/b/c/d/e but did not work.

abhijit-k33 avatar Oct 30 '20 09:10 abhijit-k33

a duplicate of #604 ?

bbigras avatar Dec 01 '20 21:12 bbigras