3v324v23 commited on
Commit
71ff4fa
·
1 Parent(s): ca7d09e

Add endpoint for retired service worker to clear cache and update app

Browse files
Files changed (1) hide show
  1. app/main.py +15 -0
app/main.py CHANGED
@@ -97,6 +97,21 @@ def create_app(auth: SupabaseAuth | None = None) -> FastAPI:
97
  return HTMLResponse((STATIC / "phone.html").read_text(encoding="utf-8"),
98
  headers={"Cache-Control": "no-store", "Referrer-Policy": "no-referrer"})
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  @app.get("/manifest.webmanifest", include_in_schema=False)
101
  async def manifest():
102
  return Response((STATIC / "manifest.webmanifest").read_text(encoding="utf-8"),
 
97
  return HTMLResponse((STATIC / "phone.html").read_text(encoding="utf-8"),
98
  headers={"Cache-Control": "no-store", "Referrer-Policy": "no-referrer"})
99
 
100
+ @app.get("/service-worker.js", include_in_schema=False)
101
+ async def retired_service_worker():
102
+ # An older version installed a service worker that served /static/ from its own cache forever.
103
+ # Browsers that still have it fetch this file to update it: the new one clears that cache,
104
+ # removes itself and reloads the open pages, so they get the current app.
105
+ script = """
106
+ self.addEventListener("install", () => self.skipWaiting());
107
+ self.addEventListener("activate", event => event.waitUntil((async () => {
108
+ await Promise.all((await caches.keys()).map(key => caches.delete(key)));
109
+ await self.registration.unregister();
110
+ for (const client of await self.clients.matchAll({type: "window"})) client.navigate(client.url);
111
+ })()));
112
+ """
113
+ return Response(script, media_type="text/javascript", headers={"Cache-Control": "no-store"})
114
+
115
  @app.get("/manifest.webmanifest", include_in_schema=False)
116
  async def manifest():
117
  return Response((STATIC / "manifest.webmanifest").read_text(encoding="utf-8"),