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

Register middlewares as late as possible #4155

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

rodja
Copy link
Member

@rodja rodja commented Dec 31, 2024

This PR moves the middleware registration from import time to the actual call of ui.run. Thereby the middlewares are also run before middlewares which are added by user code. See the the authentication example for an actual use-case where this is convinient:

https://github.com/zauberzeug/nicegui/blob/09f4c34049b37907174bfe3109aae4b834f80863/examples/authentication/main.py#L28C2-L33C40

Without this change, the RedirectResponse does not work properly behind a reverse proxy with subpath or NiceGUI On Air, because the rewriting of the path prefix which is done by our RedirectWithPrefixMiddleware. FastAPI / Starlette call the middlewares in reverse order of registration. Therefore, without this PR, the user middleware is the last one added and hence called first, leaving the path unmodified.

ToDos:

  • test with simple NiceGUI App which has no if __name__ in ('__mp_main__', '__main__') guard
  • implement and test ui.run_with

to make sure they are also applied to middlewares added by user code
@rodja rodja added the enhancement New feature or request label Dec 31, 2024
@rodja rodja marked this pull request as ready for review January 11, 2025 08:01
@rodja rodja requested a review from falkoschindler January 11, 2025 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant