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

404 error page not rendered on static file routes #2570

Closed
falkoschindler opened this issue Feb 19, 2024 · 1 comment · Fixed by #2746
Closed

404 error page not rendered on static file routes #2570

falkoschindler opened this issue Feb 19, 2024 · 1 comment · Fixed by #2746
Labels
bug Something isn't working
Milestone

Comments

@falkoschindler
Copy link
Contributor

Description

As noticed by @Anindya088 (#2568), the 404 error page isn't rendered correctly when accessed via a (sub-)path of a static route. Take this code for example:

app.add_static_files('/static', '.')
ui.link('Static', '/static')

When visiting /static or /static/subpage, the HTML page is displayed, but without any CSS and JS assets. They are requested from a wrong path and, thus, not used for rendering the page.

@rodja
Copy link
Member

rodja commented Mar 22, 2024

Thanks for the PR, @Lucas-lyh. It made me dig a bit deeper into the issue. I think the problem was introduced with #848 which sets the path prefix to scope['root_path'] -- which is correct in my opinion:

prefix = request.headers.get('X-Forwarded-Prefix', request.scope.get('root_path', ''))

But, app.add_static_files uses app.mount internally:

self.mount(url_path, StaticFiles(directory=str(local_directory), follow_symlink=follow_symlink))

This is defined in the ASGI standard to alter the scope['root_path'] -- which is not desired in this case. Because there is no way to tell the FastAPI/Starlette (which implements ASGI) to not alter scope['root_path'], we may be forced to implement app.add_static_files in some other way :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants