-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Rocket + Rust + React + Routing + SPA? #1183
Comments
To make SPAs work, you need to respond with the index.html file on all GET requests that are not a part of your API. To get/send data to the server, one could set up endpoints like |
The best documentation I could find for this in the past is https://create-react-app.dev/docs/deployment#serving-apps-with-client-side-routing. Notably, I didn't see anything like it mentioned in the tutorial you linked. It would probably be worth making an issue/PR for that tutorial if there isn't already, because I expect it to be a common source of confusion. This has been asnwered before (#723 (comment)) so I'll close this as a duplicate - but feel free to ask further questions! |
Hello, I think I've found a way to solve problems with SPA and client-side routing. In the additionally, all assets including css, js, service workers, and manifest files should be added to the #[macro_use]
extern crate rocket;
use rocket::fs::{FileServer, relative, NamedFile};
use rocket::response::{content, status};
use std::path::{PathBuf, Path};
mod apps;
#[get("/<_..>", rank = 2)]
pub(crate) fn fallback_url() -> &'static str {
"Hey, this is the fallback url"
}
#[get("/<file..>", rank=1)]
async fn files(file: PathBuf) -> Option<NamedFile> {
NamedFile::open(Path::new("static/").join(file)).await.ok()
}
#[launch]
fn rocket() -> rocket::Rocket<rocket::Build> {
rocket::build()
.attach(apps::device::stage())
// .mount("/static", FileServer::from(relative!("/static")))
.mount("/static", routes![files,])
.mount("/", routes![fallback_url,])
} |
@ghimiresdp This is just |
Thanks, @SergioBenitez. I was unaware of this. Worked perfectly!! 🚀 🚀. |
Hey @ghimiresdp, your solution worked out amazingly for me, thanks for posting that! |
I am experimenting with Rust, React and Rocket. This is eventually what I want:
For the glue I have chosen Rocket. One thing that my brain is missing, is how I can deploy Rust, React and Rocket together, so not one by one (I made a post on Reddit because I didn't understand how it can/should be done: https://www.reddit.com/r/rust/comments/e7zg3g/rust_as_server_react_as_frontend_how_to_deploy/).
I searched some time on google and came across some tutorials which quality isn't that great. But currently I have this project: https://github.com/Jasperav/rustreactrocket which can do the trick. It deploys React, Rust and Rocket all in once (after npm install & build)!
However, the routing is done in Rust/Rocket. I don't understand how I can make it work through react routing and components. I want to use the react routing dom dependency (https://reacttraining.com/react-router/web/guides/quick-start). This enables SPA and adds a suffix to the URL when creating a route.
That is where my problem is. When the suffix will be inserted inside the URL, Rocket tells me it doesn't know how to handle it. I can use a wildcard matcher in the get macro (
#[get("/<file..>")]
), but what do I need to return from that function? React is doing my routes already.Ofcourse I can let Rocket do my routing, but is it still SPA? From what I am seeing is that it returns a whole html file (or some strings). That's not what I want. I have looked quickly in the docs but I haven't found out one with React.
The text was updated successfully, but these errors were encountered: