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

static web page router question #723

Closed
Vincent-bin opened this issue Jul 30, 2018 · 7 comments
Closed

static web page router question #723

Vincent-bin opened this issue Jul 30, 2018 · 7 comments
Labels
question A question (converts to discussion)

Comments

@Vincent-bin
Copy link

Rocket version : 0.3.15

When I use react and react-router to build a web page and use rocket static file router,

I found the main page "/index.html" can work but router function can't work, get 404 back, and log shows my router "/answers" match "/<file..>", then 404,

I've tried to use redirect but not useful.

@mettke
Copy link
Contributor

mettke commented Jul 30, 2018

You may want to give us a small example. It is quite hard to understand your problem without any bit of code showing what exactly you want to archive

@Yarn
Copy link

Yarn commented Jul 31, 2018

I think the issue he's having is that requests to a folder don't open index.html. This is standard default behavior for servers serving static assets like nginx and apache and some async web frameworks have static file serving with that behavior available built in.

You can see the lack of this feature in the example code in the guide.

I'll probably have some code to share for this soon.

@jebrosen
Copy link
Collaborator

If react-router behaves like other client-side routing frameworks, it probably expects all paths that aren't static assets or APIs to serve the application, and react-router will handle everything from there. A react-router FAQ entry suggests doing exactly that: serve the index.html page for any route that the server doesn't handle otherwise.

@Yarn
Copy link

Yarn commented Jul 31, 2018

ah, i misunderstood the issue

then it would just be

#[get("/<_file..>")]
fn files(_file: PathBuf) -> Option<NamedFile> {
    NamedFile::open(Path::new("static/index.html")).ok()
}

Edit: except there are other static assets?

#[get("/<file..>", rank = 10)] // set the rank higher than other endpoints
fn files(file: PathBuf) -> Option<NamedFile> {
    NamedFile::open(Path::new("static/").join(file)).ok().or_else(|| NamedFile::open(Path::new("static/index.html")).ok())
}

this will be annoying if you put in a path wrong, not sure off the top of my head how check that
https://doc.rust-lang.org/std/path/struct.Path.html#method.extension might be close enough but obviously hacky

@SergioBenitez
Copy link
Member

I think this is #239?

@jebrosen
Copy link
Collaborator

@SergioBenitez: It's more than what appears planned with #239, but #239 could be extended to support it.

@SergioBenitez SergioBenitez added the question A question (converts to discussion) label Aug 1, 2018
@SergioBenitez
Copy link
Member

@jebrosen Indeed. That solution should support index.html files. Closing this in favor of #239.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A question (converts to discussion)
Projects
None yet
Development

No branches or pull requests

5 participants