Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: retire service workers other apps left on the Studio origin (#16107)

Daniel Han4b94a56fbb27
diff --git a/studio/backend/main.py b/studio/backend/main.py
index 852fac1a680..4d356260366 100644
--- a/studio/backend/main.py
+++ b/studio/backend/main.py
@@ -3244,6 +3244,14 @@ def _match_assets_before_api_routes(app: FastAPI) -> None:
routes.insert(position, mount)
+# Takes over at once, then unregisters: no fetch handler, so requests go straight to the network,
+# and no cache or storage is touched (another app's caches are not Studio's to clear).
+_RETIRING_SERVICE_WORKER = (
+ 'self.addEventListener("install", () => self.skipWaiting());\n'
+ 'self.addEventListener("activate", (event) => event.waitUntil(self.registration.unregister()));\n'
+)
+
+
def setup_frontend(
app: FastAPI,
build_path: Path,
@@ -3320,6 +3328,15 @@ def setup_frontend(
if is_engine_probe_path(full_path):
raise HTTPException(status_code = 404, detail = "API endpoint not found")
+ # Studio registers no service worker, so this is the browser updating one another app left on
+ # this origin. The app shell fails that update forever and the worker keeps every Studio request.
+ if request.headers.get("service-worker") == "script":
+ return Response(
+ content = _RETIRING_SERVICE_WORKER,
+ media_type = "text/javascript",
+ headers = {"Cache-Control": "no-store", "Service-Worker-Allowed": "/"},
+ )
+
# Serve index.html as bytes - avoids Content-Length mismatch
return _build_index_response(request)
diff --git a/studio/backend/tests/test_middleware.py b/studio/backend/tests/test_middleware.py
index 197aa18c4db..e8a00d09e9a 100644
--- a/studio/backend/tests/test_middleware.py
+++ b/studio/backend/tests/test_middleware.py
@@ -1070,6 +1070,30 @@ class TestFrontendAssets:
assert client.get("/assets/missing.js").status_code == 404
assert "<title>x</title>" in client.get("/some/spa/route").text
+ def test_a_service_worker_update_gets_a_worker_that_unregisters_itself(
+ self, tmp_path, main_module
+ ):
+ """Another app's worker left on this origin updates against /sw.js; the app shell would fail
+ that update forever, so the browser gets a worker that retires the registration."""
+ (tmp_path / "index.html").write_text("<!doctype html><title>x</title>")
+ (tmp_path / "real-sw.js").write_text("// shipped", encoding = "utf-8")
+ app = FastAPI()
+ assert main_module.setup_frontend(app, tmp_path)
+ client = TestClient(app, base_url = "http://127.0.0.1:8888", client = ("127.0.0.1", 40000))
+ worker = {"Service-Worker": "script"}
+
+ response = client.get("/sw.js", headers = worker)
+ assert response.status_code == 200
+ assert response.headers["content-type"].startswith("text/javascript")
+ assert response.headers["cache-control"] == "no-store"
+ assert response.headers["service-worker-allowed"] == "/"
+ assert "self.registration.unregister()" in response.text and "fetch" not in response.text
+ assert "unregister()" in client.get("/nested/service-worker.js", headers = worker).text
+ # Page loads and files that exist are untouched.
+ assert "<title>x</title>" in client.get("/sw.js").text
+ assert client.get("/real-sw.js", headers = worker).text == "// shipped"
+ assert client.get("/api/nope", headers = worker).status_code == 404
+
def test_a_second_setup_frontend_keeps_the_first_assets_mount_winning(
self, tmp_path, main_module
):