diff --git a/backend/open_webui/env.py b/backend/open_webui/env.py index 7059780220..ecad336855 100644 --- a/backend/open_webui/env.py +++ b/backend/open_webui/env.py @@ -135,6 +135,9 @@ else: PACKAGE_DATA = {"version": "0.0.0"} VERSION = PACKAGE_DATA["version"] + + +DEPLOYMENT_ID = os.environ.get("DEPLOYMENT_ID", "") INSTANCE_ID = os.environ.get("INSTANCE_ID", str(uuid4())) diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index e21dd5d1ed..e0e23390af 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -454,6 +454,7 @@ from open_webui.env import ( SAFE_MODE, SRC_LOG_LEVELS, VERSION, + DEPLOYMENT_ID, INSTANCE_ID, WEBUI_BUILD_HASH, WEBUI_SECRET_KEY, @@ -1983,6 +1984,7 @@ async def update_webhook_url(form_data: UrlForm, user=Depends(get_admin_user)): async def get_app_version(): return { "version": VERSION, + "deployment_id": DEPLOYMENT_ID, } diff --git a/src/lib/apis/index.ts b/src/lib/apis/index.ts index 126c59ad2f..e865e9ba0e 100644 --- a/src/lib/apis/index.ts +++ b/src/lib/apis/index.ts @@ -1425,7 +1425,7 @@ export const getVersion = async (token: string) => { throw error; } - return res?.version ?? null; + return res; }; export const getVersionUpdates = async (token: string) => { diff --git a/src/lib/stores/index.ts b/src/lib/stores/index.ts index 5d8cc62513..c4c2dc10c9 100644 --- a/src/lib/stores/index.ts +++ b/src/lib/stores/index.ts @@ -8,7 +8,10 @@ import emojiShortCodes from '$lib/emoji-shortcodes.json'; // Backend export const WEBUI_NAME = writable(APP_NAME); + export const WEBUI_VERSION = writable(null); +export const WEBUI_DEPLOYMENT_ID = writable(null); + export const config: Writable = writable(undefined); export const user: Writable = writable(undefined); diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index a4376c4710..3f880b7cad 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -16,6 +16,7 @@ theme, WEBUI_NAME, WEBUI_VERSION, + WEBUI_DEPLOYMENT_ID, mobile, socket, chatId, @@ -54,9 +55,24 @@ import Spinner from '$lib/components/common/Spinner.svelte'; import { getUserSettings } from '$lib/apis/users'; + const unregisterServiceWorkers = async () => { + if ('serviceWorker' in navigator) { + try { + const registrations = await navigator.serviceWorker.getRegistrations(); + await Promise.all(registrations.map((r) => r.unregister())); + return true; + } catch (error) { + console.error('Error unregistering service workers:', error); + return false; + } + } + return false; + }; + // handle frontend updates (https://svelte.dev/docs/kit/configuration#version) - beforeNavigate(({ willUnload, to }) => { + beforeNavigate(async ({ willUnload, to }) => { if (updated.current && !willUnload && to?.url) { + await unregisterServiceWorkers(); location.href = to.url.href; } }); @@ -90,15 +106,30 @@ _socket.on('connect', async () => { console.log('connected', _socket.id); - const version = await getVersion(localStorage.token); - if (version !== null) { - if ($WEBUI_VERSION !== null && version !== $WEBUI_VERSION) { + const res = await getVersion(localStorage.token); + + const deploymentId = res?.deployment_id ?? null; + const version = res?.version ?? null; + + if (version !== null || deploymentId !== null) { + if ( + ($WEBUI_VERSION !== null && version !== $WEBUI_VERSION) || + ($WEBUI_DEPLOYMENT_ID !== null && deploymentId !== $WEBUI_DEPLOYMENT_ID) + ) { + await unregisterServiceWorkers(); location.href = location.href; - } else { - WEBUI_VERSION.set(version); + return; } } + if (deploymentId !== null) { + WEBUI_DEPLOYMENT_ID.set(deploymentId); + } + + if (version !== null) { + WEBUI_VERSION.set(version); + } + console.log('version', version); if (localStorage.getItem('token')) {