Skip to content

Commit

Permalink
register middlewares as late as possible
Browse files Browse the repository at this point in the history
to make sure they are also applied to middlewares added by user code
  • Loading branch information
rodja committed Dec 31, 2024
1 parent 09f4c34 commit 32668df
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 0 additions & 4 deletions nicegui/nicegui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import socketio
from fastapi import HTTPException, Request
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.responses import FileResponse, Response

from . import air, background_tasks, binding, core, favicon, helpers, json, run, welcome
Expand All @@ -17,7 +16,6 @@
from .error import error_content
from .json import NiceGUIJSONResponse
from .logging import log
from .middlewares import RedirectWithPrefixMiddleware
from .page import page
from .slot import Slot
from .staticfiles import CacheControlledStaticFiles
Expand Down Expand Up @@ -54,8 +52,6 @@ async def __call__(self, scope, receive, send):
mimetypes.add_type('text/javascript', '.js')
mimetypes.add_type('text/css', '.css')

app.add_middleware(GZipMiddleware)
app.add_middleware(RedirectWithPrefixMiddleware)
static_files = CacheControlledStaticFiles(
directory=(Path(__file__).parent / 'static').resolve(),
follow_symlink=True,
Expand Down
4 changes: 4 additions & 0 deletions nicegui/ui_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path
from typing import Any, List, Literal, Optional, Tuple, TypedDict, Union

from fastapi.middleware.gzip import GZipMiddleware
from starlette.routing import Route
from uvicorn.main import STARTUP_FAILURE
from uvicorn.supervisors import ChangeReload, Multiprocess
Expand All @@ -16,6 +17,7 @@
from .client import Client
from .language import Language
from .logging import log
from .middlewares import RedirectWithPrefixMiddleware
from .server import CustomServerConfig, Server

APP_IMPORT_STRING = 'nicegui:app'
Expand Down Expand Up @@ -122,6 +124,8 @@ def run(*,
show_welcome_message=show_welcome_message,
)
core.app.config.endpoint_documentation = endpoint_documentation
core.app.add_middleware(GZipMiddleware)
core.app.add_middleware(RedirectWithPrefixMiddleware)

for route in core.app.routes:
if not isinstance(route, Route):
Expand Down

0 comments on commit 32668df

Please sign in to comment.