mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-28 08:09:27 -05:00
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 |
This commit is contained in:
1 parent
87d148e9cb
commit
43b8ad5256
3 files changed
+17
-12
No files matched your search
@@ -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'):
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user