refac/enh: unregisterServiceWorkers on update

This commit is contained in:
Timothy Jaeryang Baek 2025-11-20 15:34:15 -05:00
parent 4c28f19bdd
commit ff7a54653a
5 changed files with 46 additions and 7 deletions

View file

@ -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()))

View file

@ -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,
}

View file

@ -1425,7 +1425,7 @@ export const getVersion = async (token: string) => {
throw error;
}
return res?.version ?? null;
return res;
};
export const getVersionUpdates = async (token: string) => {

View file

@ -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<Config | undefined> = writable(undefined);
export const user: Writable<SessionUser | undefined> = writable(undefined);

View file

@ -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')) {