-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Allow falling back to index page in SpaRouter
when serving on /
#933
Comments
SpaRouter
when serving on/
SpaRouter
when serving on /
Are you saying you're serving assets from the root path? So like |
Exactly that. In our case, we compile a project to wasm, including other web-related files. Then we output everything in one single folder and serve that one locally. So the assets and the I already had a look at the PR you prepared so quickly, still have to test this out. While looking at the code, I realized that my initial statement is a bit wrong. We don't just need to serve from the root, but technically need this “always fall back to index.html” behavior pretty much everywhere. So if the root changes through a user flag, it changes the whole root of where the wasm app is served. But the problem remains, that we have the index.html and all assets together and the same root where the actual app is served. Therefore, we technically move the whole root. I hope it isn't asked too much, to turn this feature into a flag, that can be passed to the |
Isn't that what my change in the PR does? Router::new()
.route("/foo", get(|| async { "GET /foo" }))
.merge(SpaRouter::new("/", "dist"));
If not then can you provide some examples of how you'd like things routed. |
Sorry, let me give you an example to make it a bit more clear. So we have basically an SPA router + eventually proxies (forwarding a specific route to some other server/service). The base version would be this: Router::new()
.fallback(Router::from(SpaRouter::new(public_route, &state.dist_dir))
.route("/_trunk/ws", get(...)); The Additional proxies might be added to the router later, if configured by the user. They just forward a configured URL to a backend server, which allows for local test setup where an external service is expected by the frontend at a given route. By default, the So the default turns into In that case, we'd still want to have the same logic as if the route is Therefore, it would be great to have a flag in the |
Ah I see! That makes sense 😃 I'll try and implement that in my PR. |
Sure, thank you so much 👍. Will have to wait until tomorrow though, already quite late for me here. |
Hm, I guess we almost have it. For my test, I set up the SpaRouter::new("/app", "dist")
.serve_index_file_on_missing_asset(true) This works great now for getting the index file from any location, but it seems to work a bit too good 😅. So for example I think that is the right behavior for the I tried to fix this myself with: Router::new()
.nest(
public_route,
Router::from(
SpaRouter::new(public_route, &state.dist_dir)
.serve_index_file_on_missing_asset(true),
),
) But that seems not allowed, as the SPA router uses a fallback and that doesn't work with At this point, I wonder whether I should maybe write my own router for this. Slowly get the feeling that our case is a bit to specialized for the average user of the |
For reference, here is the PR on our side, trying out to move to |
I have a few ideas how to make the whole Was thinking whether it would be better to let the user decide how to build the SPA logic and make the actual Will experiment a bit around with this idea and come back to you. |
What is Router::new()
.nest(
public_route,
Router::from(
SpaRouter::new(public_route, &state.dist_dir)
.serve_index_file_on_missing_asset(true),
),
) Doesn't work for serving assets at the root (ie when Also, the intended usecase is |
@dnaka91 have you tested my PR? And seen what I wrote above? |
Hey sorry, had quite a packed weekend. I'll get to it tomorrow. Only missing piece that I would need is to not have the fallback enabled... I think. |
I think we should leave |
I totally agree ❤️ At first, I thought I'd be great to add these features to Thank you for pointing out the new fallback support in Thanks again for all the effort you put into this issue. Axum is a great framework and I really appreciate your support in this (and other issues/questions I had in the past) 🚀 |
Feature Request
Motivation
Wanted to replace our current solution in Trunk for SPA apps with the new
SpaRouter
fromaxum-extra
. This would allow us to drop our current implementation, done withhyper-staticfile
.Unfortunately, I noticed that when serving the
SpaRouter
at the root/
, it doesn't fall back toindex.html
anymore.In Trunk the usual setup is to have the index page and additional assets all in the same folder. So without custom config it would always result in
SpaRouter::new("/", "dist")
. That makes the router consider everything to be a 404 as it is considered inside the assets folder.Instead, we need the router to fall back to the
index.html
, same as when using the router on anything else but the root.Proposal
The
SpaRouter
is technically correct in its implementation, but I see the root/
rather as a special case. Therefore, I see 2 solutions.Check that the serve route is
/
and enable the fallback toindex.html
on all missing files.Add a new option that allows to override the 404 logic, always returning the
index.html
on missing files, no matter what.Alternatives
The text was updated successfully, but these errors were encountered: