Daniel.y commited on
Commit
d262551
·
unverified ·
2 Parent(s): 833afae 8ee22d7

Merge pull request #1605 from HKUDS/fix-mime-for-windows

Browse files
Files changed (1) hide show
  1. lightrag/api/lightrag_server.py +20 -3
lightrag/api/lightrag_server.py CHANGED
@@ -478,16 +478,31 @@ 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
  return response
492
 
493
  # Webui mount webui/index.html
@@ -495,7 +510,9 @@ def create_app(args):
495
  static_dir.mkdir(exist_ok=True)
496
  app.mount(
497
  "/webui",
498
- NoCacheStaticFiles(directory=static_dir, html=True, check_dir=True),
 
 
499
  name="webui",
500
  )
501
 
 
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"] = (
488
  "no-cache, no-store, must-revalidate"
489
  )
490
  response.headers["Pragma"] = "no-cache"
491
  response.headers["Expires"] = "0"
492
+ elif (
493
+ "/assets/" in path
494
+ ): # Assets (JS, CSS, images, fonts) generated by Vite with hash in filename
495
+ response.headers["Cache-Control"] = (
496
+ "public, max-age=31536000, immutable"
497
+ )
498
+ # Add other rules here if needed for non-HTML, non-asset files
499
+
500
+ # Ensure correct Content-Type
501
+ if path.endswith(".js"):
502
+ response.headers["Content-Type"] = "application/javascript"
503
+ elif path.endswith(".css"):
504
+ response.headers["Content-Type"] = "text/css"
505
+
506
  return response
507
 
508
  # Webui mount webui/index.html
 
510
  static_dir.mkdir(exist_ok=True)
511
  app.mount(
512
  "/webui",
513
+ SmartStaticFiles(
514
+ directory=static_dir, html=True, check_dir=True
515
+ ), # Use SmartStaticFiles
516
  name="webui",
517
  )
518