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

Single page application (SPA) example #183

Closed
frederikhors opened this issue Aug 23, 2022 · 3 comments · Fixed by #184
Closed

Single page application (SPA) example #183

frederikhors opened this issue Aug 23, 2022 · 3 comments · Fixed by #184
Labels

Comments

@frederikhors
Copy link
Contributor

I'm having a bit of difficulty using your example (with axum in this case) to host a single page application (SPA).

I have no trouble without rust-embed using a single line of code with axum (from here):

app.fallback(get_service(ServeDir::new("./app/static")).handle_error(error_handler))

But I don't understand how to do with rust-embed.

The need is that every path typed by the user (and that is not an existent file such as .js or .css which obviously must be downloaded by the browser) leads to the "index.html" file in the root of my static dir.

If I use your example code I can see the route:

.route("/dist/*file", static_handler.into_service())

which has /dist/*file and I don't need that /dist becasue the index.html calls many files with custom paths, such as /_works, menu, images.

If I remove the dist part I get this error:

thread 'main' panicked at 'Invalid route: insertion failed due to conflict with previously registered route: /index.html'

Can you help me understand?

If I fix this I can create a PR for future people to come!

I hope my problem is clear. Sorry if is not.

@AzureMarker
Copy link
Collaborator

AzureMarker commented Aug 24, 2022

The need is that every path typed by the user (and that is not an existent file such as .js or .css which obviously must be downloaded by the browser) leads to the "index.html" file in the root of my static dir.

You can call RustEmbed::get and if the result is None then fall back to returning the index.html file.

I did this a while ago with an old project: https://github.com/AzureMarker/pihole-api/blob/bc7819bb4186e4cbd04e881920a925619537bae5/src/routes/web.rs#L46

@frederikhors
Copy link
Contributor Author

@AzureMarker thank you very much!

I'm starting with a small example here: https://github.com/frederikhors/rust-embed-spa.

I'm having trouble refactoring. I'll write in a few what I think is wrong.

@frederikhors
Copy link
Contributor Author

I opened #184.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants