From 43b8ad5256cf64bac3c6d777a16efbf2db5cb115 Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:41:44 +0100 Subject: [PATCH] perf: load the config and the session check at the same time on page load (#33914) Every page load fetched the app config, then checked the login session, then fetched the config a second time to pick up the logged-in settings, three requests in a row before the sidebar and chat could start loading. Now the first config request sends the login token, so it already returns the logged-in settings and the second fetch is gone, and the session check runs alongside it. A deleted or deactivated user whose old login was still saved in the browser landed on the "backend not detected" error page on every reload; they now go to the sign-in page. | Local run, 150 ms added to every request | dev branch | this change | |---|---|---| | Requests before the sidebar loads | 3 in a row | 2 in parallel | | Sidebar requests start at | 1240 ms | 875 ms | --- backend/open_webui/main.py | 10 ++++++++-- src/lib/apis/index.ts | 5 +++-- src/routes/+layout.svelte | 14 ++++++-------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index 1caf156bfa..b080af7964 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -2347,8 +2347,14 @@ async def get_app_config(request: Request): status_code=status.HTTP_401_UNAUTHORIZED, detail='Invalid token', ) - if data is not None and 'id' in data and await is_valid_token(data, request.app.state.redis): - user = await Users.get_user_by_id(data['id']) + if data is not None and 'id' in data: + try: + if await is_valid_token(data, request.app.state.redis): + user = await Users.get_user_by_id(data['id']) + except HTTPException as e: + # A deleted or deactivated account gets the public config + if e.status_code != status.HTTP_401_UNAUTHORIZED: + raise group_defaults = None if user is not None and user.role in ('admin', 'user'): diff --git a/src/lib/apis/index.ts b/src/lib/apis/index.ts index 698f5f27e6..bb3e6c15e6 100644 --- a/src/lib/apis/index.ts +++ b/src/lib/apis/index.ts @@ -1502,14 +1502,15 @@ export const getUsage = async (token: string = '') => { return res; }; -export const getBackendConfig = async () => { +export const getBackendConfig = async (token: string = '') => { let error = null; const res = await fetch(`${WEBUI_BASE_URL}/api/config`, { method: 'GET', credentials: 'include', headers: { - 'Content-Type': 'application/json' + 'Content-Type': 'application/json', + ...(token && { authorization: `Bearer ${token}` }) } }) .then(async (res) => { diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 138feff65c..2806712e50 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1340,9 +1340,12 @@ }; window.addEventListener('beforeunload', beforeUnloadHandler); + const sessionUserRequest = localStorage.token ? getSessionUser(localStorage.token) : null; + sessionUserRequest?.catch(() => {}); + let backendConfig = null; try { - backendConfig = await getBackendConfig(); + backendConfig = await getBackendConfig(localStorage.token); console.log('Backend config:', backendConfig); } catch (error) { if (error?.authRedirect) { @@ -1380,20 +1383,15 @@ if ($config) { await setupSocket($config.features?.enable_websocket ?? true); - if (localStorage.token) { + if (sessionUserRequest) { // Get Session User Info - const sessionUser = await getSessionUser(localStorage.token).catch((error) => { + const sessionUser = await sessionUserRequest.catch((error) => { toast.error(`${error}`); return null; }); if (sessionUser) { await user.set(sessionUser); - try { - await config.set(await getBackendConfig()); - } catch (error) { - console.error('Error refreshing backend config:', error); - } // Keep user timezone in sync on every app load/refresh const timezone = getUserTimezone();