Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(web-studio): normalize service worker scope (#3963)

Onefly0fc2a2cb46b9
diff --git a/web-studio/src/lib/service-worker.ts b/web-studio/src/lib/service-worker.ts
new file mode 100644
index 000000000..4b9c7cc0a
--- /dev/null
+++ b/web-studio/src/lib/service-worker.ts
@@ -0,0 +1,33 @@
+export interface ServiceWorkerPaths {
+ scope: string
+ scriptUrl: string
+}
+
+export function resolveServiceWorkerPaths(
+ basePath: string,
+): ServiceWorkerPaths {
+ const normalizedBasePath = basePath.trim() || '/'
+ const scope =
+ normalizedBasePath === '/'
+ ? '/'
+ : `${normalizedBasePath.replace(/\/+$/, '')}/`
+
+ return {
+ scope,
+ scriptUrl: `${scope}service-worker.js`,
+ }
+}
+
+export async function registerServiceWorker(
+ serviceWorker: ServiceWorkerContainer,
+ basePath: string,
+): Promise<ServiceWorkerRegistration | undefined> {
+ const { scope, scriptUrl } = resolveServiceWorkerPaths(basePath)
+
+ try {
+ return await serviceWorker.register(scriptUrl, { scope })
+ } catch (error) {
+ console.warn('OpenViking Studio service worker registration failed', error)
+ return undefined
+ }
+}
diff --git a/web-studio/src/main.tsx b/web-studio/src/main.tsx
index 5e7bdce92..aeed95268 100644
--- a/web-studio/src/main.tsx
+++ b/web-studio/src/main.tsx
@@ -7,15 +7,15 @@ import { routeTree } from './routeTree.gen'
import { TooltipProvider } from './components/ui/tooltip'
import { queryClient } from './lib/query-client'
import { getRouterBasePath } from './lib/public-path'
+import { registerServiceWorker } from './lib/service-worker'
// PWA: register the service worker at the SPA's base path so the scope
// matches the manifest's start_url / scope. Production builds only — the
// vite dev server's HMR doesn't play nicely with a SW intercepting requests.
if ('serviceWorker' in navigator && import.meta.env.PROD) {
const basePath = getRouterBasePath() || '/'
- const swUrl = `${basePath}${basePath.endsWith('/') ? '' : '/'}service-worker.js`
window.addEventListener('load', () => {
- void navigator.serviceWorker.register(swUrl, { scope: basePath })
+ void registerServiceWorker(navigator.serviceWorker, basePath)
})
}