axum icon indicating copy to clipboard operation
axum copied to clipboard

Unexpected behaviour in example `static-file-server`

Open tobx opened this issue 1 year ago • 2 comments

  • [X] I have looked for existing issues (including closed) about this

Bug Report

Version

0.7.5

Description

Unexpected behaviour in example static-file-server, function using_serve_dir_with_assets_fallback():

It serves assets (in this case script.js) from the url /assets/script.js, but unexpectedly also from /script.js, because the whole assets folder is used as fallback instead of just the index.html file.

I expected to see this happen:
url /script.js should fallback to index.html

Instead, this happened:
url /script.js responded with script.js

tobx avatar Jun 28 '24 12:06 tobx

Why did you expect this? I think the examples could certainly be better explained, but I'm not really sure about the original intent behind that example.

jplatte avatar Sep 28 '24 21:09 jplatte

Why did you expect this? I think the examples could certainly be better explained, but I'm not really sure about the original intent behind that example.

Yes, I think my description was not very clear. Let me try again:

Typically when you serve static files from a folder like assets, you do that in order to "namespace" URLs (e.g. /api/, /assets/ or /posts/). So, if you have an image like background.jpg in a static folder and serve it with /assets/background.jpg you do NOT want it to be also served at the root level with /background.jpg.

The issue is, that in the example the whole assets folder is set as a fallback service instead of just the index.html file. It is not a big deal and now it is easy for me to see, but when I started with axum, it took me quite a while to understand what is going on and why my files are served from both URLs (/ and /assets/).

tobx avatar Sep 30 '24 15:09 tobx

@tobx indeed, having fields served from both URLs is strange. Would you like to open a PR to fix this behavior?

yanns avatar Nov 09 '25 08:11 yanns