bep40 commited on
Commit
19ff1bf
·
1 Parent(s): 9d99f80

Fix stale-cache blank page: immutable asset cache + no-cache index.html (v11)

Browse files

Root cause: HF Spaces serves the built static app with NO Cache-Control
headers. After every rebuild the vite bundle hash changes but browsers keep
serving a cached index.html referencing the OLD hash -> 404 on
/assets/index-*.js -> blank/hanging page (seen in logs:
'GET /assets/index-UH2b2uDK.js 404').

Fix in patch_models.py Step 3: patch backend/main.py to mount a
_CachedStaticFiles subclass that sets:
- public, max-age=31536000, immutable on hashed assets (assets/*.js etc)
- no-cache on index.html so it is always revalidated after a rebuild

The 3 new OpenRouter models (nex-agi nex-n2.5-mini/pro, inclusionai
ling-3.0-flash-vl) from v10 are unchanged and verified live (25 models
served by /api/config/model).

Files changed (2) hide show
  1. Dockerfile +1 -1
  2. patch_models.py +59 -0
Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- # Rebuild v10: Add 3 new OpenRouter models - Nex-N2.5-Mini, Nex-N2.5-Pro, Ling 3.0 Flash VL
2
 
3
  # Stage 1: Clone upstream + patch frontend ChatInput + build
4
  FROM node:20-alpine AS patcher
 
1
+ # Rebuild v11: Fix stale-cache blank page (immutable asset cache + no-cache index.html)
2
 
3
  # Stage 1: Clone upstream + patch frontend ChatInput + build
4
  FROM node:20-alpine AS patcher
patch_models.py CHANGED
@@ -313,3 +313,62 @@ with open(LLM_PARAMS_FILE, "w") as f:
313
  f.write(llm_content)
314
 
315
  print("OK: Backend patched - 25 models, deepseek/ + z-ai/[:free stripped] + stripped openai/ -> OR")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
  f.write(llm_content)
314
 
315
  print("OK: Backend patched - 25 models, deepseek/ + z-ai/[:free stripped] + stripped openai/ -> OR")
316
+
317
+ # === Step 3: Patch backend/main.py — safe static cache headers ===
318
+ # FIX: Stale-cache blank page. HF Spaces serves the static build with NO
319
+ # Cache-Control headers. After every rebuild the hashed asset filename
320
+ # changes but the browser keeps caching index.html which then references a
321
+ # bundle hash that no longer exists -> 404 on /assets/index-*.js -> blank
322
+ # page. Fix: 1yr immutable cache for hashed assets, no-cache for index.html.
323
+ MAIN_FILE = "/app/backend/main.py"
324
+ with open(MAIN_FILE) as f:
325
+ main_content = f.read()
326
+
327
+ static_import_old = "from fastapi.staticfiles import StaticFiles"
328
+ static_import_new = """from fastapi.staticfiles import StaticFiles
329
+
330
+
331
+ class _CachedStaticFiles(StaticFiles):
332
+ \"\"\"StaticFiles with safe cache headers.
333
+
334
+ - Hashed build assets (vite emits index-<hash>.js) get a 1-year
335
+ immutable Cache-Control so repeat visits skip the download.
336
+ - index.html and everything else get no-cache so the browser always
337
+ revalidates and never pins a stale HTML that references a bundle
338
+ hash that no longer exists after a rebuild (blank-page 404 bug).
339
+ \"\"\"
340
+
341
+ async def get_response(self, path: str, scope):
342
+ resp = await super().get_response(path, scope)
343
+ if scope["method"] == "GET" and resp.status_code == 200:
344
+ if path.startswith("assets/") or path.endswith(
345
+ (".js", ".css", ".woff2", ".png", ".webp", ".svg", ".ico", ".jpg", ".jpeg", ".ttf")
346
+ ):
347
+ resp.headers["Cache-Control"] = "public, max-age=31536000, immutable"
348
+ else:
349
+ resp.headers["Cache-Control"] = "no-cache"
350
+ return resp"""
351
+
352
+ if static_import_old in main_content:
353
+ main_content = main_content.replace(static_import_old, static_import_new)
354
+ else:
355
+ print("WARN: StaticFiles import not found for cache patch, may already be applied")
356
+
357
+ static_mount_old = 'app.mount("/", StaticFiles(directory=str(static_path), html=True), name="static")'
358
+ static_mount_new = 'app.mount("/", _CachedStaticFiles(directory=str(static_path), html=True), name="static")'
359
+ if static_mount_old in main_content:
360
+ main_content = main_content.replace(static_mount_old, static_mount_new)
361
+ else:
362
+ print("WARN: StaticFiles mount not found for cache patch, may already be applied")
363
+
364
+ try:
365
+ ast.parse(main_content)
366
+ print("OK: main.py syntax OK")
367
+ except SyntaxError as e:
368
+ print(f"FAIL: Syntax error in main.py: {e}")
369
+ raise
370
+
371
+ with open(MAIN_FILE, "w") as f:
372
+ f.write(main_content)
373
+
374
+ print("OK: main.py patched - immutable asset cache + no-cache index.html (fixes stale-hash blank page)")