gzdaniel commited on
Commit
12adb43
·
1 Parent(s): 04f47f7

Refactor: Optimize static file caching for WebUI

Browse files

- Renamed `NoCacheStaticFiles` to `SmartStaticFiles`.
- Implemented long-term caching (1 year, immutable) for versioned assets in `/webui/assets/`.
- Ensured `index.html` remains un-cached.
- Set correct `Content-Type` for JS and CSS files.

Files changed (1) hide show
  1. lightrag/api/lightrag_server.py +14 -7
lightrag/api/lightrag_server.py CHANGED
@@ -478,18 +478,25 @@ def create_app(args):
478
  logger.error(f"Error getting health status: {str(e)}")
479
  raise HTTPException(status_code=500, detail=str(e))
480
 
481
- # Custom StaticFiles class to prevent caching of HTML files
482
- class NoCacheStaticFiles(StaticFiles):
483
  async def get_response(self, path: str, scope):
484
  response = await super().get_response(path, scope)
 
485
  if path.endswith(".html"):
486
- response.headers["Cache-Control"] = (
487
- "no-cache, no-store, must-revalidate"
488
- )
489
  response.headers["Pragma"] = "no-cache"
490
  response.headers["Expires"] = "0"
491
- elif path.endswith(".js"):
 
 
 
 
 
492
  response.headers["Content-Type"] = "application/javascript"
 
 
 
493
  return response
494
 
495
  # Webui mount webui/index.html
@@ -497,7 +504,7 @@ def create_app(args):
497
  static_dir.mkdir(exist_ok=True)
498
  app.mount(
499
  "/webui",
500
- NoCacheStaticFiles(directory=static_dir, html=True, check_dir=True),
501
  name="webui",
502
  )
503
 
 
478
  logger.error(f"Error getting health status: {str(e)}")
479
  raise HTTPException(status_code=500, detail=str(e))
480
 
481
+ # Custom StaticFiles class for smart caching
482
+ class SmartStaticFiles(StaticFiles): # Renamed from NoCacheStaticFiles
483
  async def get_response(self, path: str, scope):
484
  response = await super().get_response(path, scope)
485
+
486
  if path.endswith(".html"):
487
+ response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
 
 
488
  response.headers["Pragma"] = "no-cache"
489
  response.headers["Expires"] = "0"
490
+ elif "/assets/" in path: # Assets (JS, CSS, images, fonts) generated by Vite with hash in filename
491
+ response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
492
+ # Add other rules here if needed for non-HTML, non-asset files
493
+
494
+ # Ensure correct Content-Type
495
+ if path.endswith(".js"):
496
  response.headers["Content-Type"] = "application/javascript"
497
+ elif path.endswith(".css"):
498
+ response.headers["Content-Type"] = "text/css"
499
+
500
  return response
501
 
502
  # Webui mount webui/index.html
 
504
  static_dir.mkdir(exist_ok=True)
505
  app.mount(
506
  "/webui",
507
+ SmartStaticFiles(directory=static_dir, html=True, check_dir=True), # Use SmartStaticFiles
508
  name="webui",
509
  )
510