From ba0ba54d93b25faf1e149f4ccedd3e9d84798563 Mon Sep 17 00:00:00 2001 From: Emanuil Rusev Date: Tue, 29 Sep 2026 18:48:17 +0300 Subject: [PATCH] server : remove the built-in UI's service worker when the UI is not served (#29565) With --path or --no-ui, /sw.js returned 404, and a 404 does not remove a service worker, so browsers kept showing the cached built-in UI. Serve a worker that unregisters itself, clears its caches and reloads open tabs. A sw.js in the --path folder is still served first. Assisted-by: Claude Opus 5.5 --- tools/server/server-http.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tools/server/server-http.cpp b/tools/server/server-http.cpp index e46a1f6f10..33cbb156c5 100644 --- a/tools/server/server-http.cpp +++ b/tools/server/server-http.cpp @@ -470,6 +470,23 @@ bool server_http_context::init_listener(const common_params & params) { #endif } } + + // a browser that used the built-in UI keeps its service worker, so it shows the old UI even after the UI is replaced or disabled + // answer the worker's update check with a worker that removes itself; a sw.js in public_path is served first + if (!params.ui || !params.public_path.empty()) { + srv->Get(params.api_prefix + "/sw.js", [](const httplib::Request &, httplib::Response & res) { + static constexpr const char * sw_remove_js = R"( +self.addEventListener('install', () => self.skipWaiting()); +self.addEventListener('activate', (e) => e.waitUntil((async () => { + await self.registration.unregister(); + for (const key of await caches.keys()) await caches.delete(key); + for (const c of await self.clients.matchAll({ type: 'window' })) c.navigate(c.url); +})())); +)"; + res.set_header("Cache-Control", "no-cache"); + res.set_content(sw_remove_js, "application/javascript"); + }); + } return true; }