xxyyy123 commited on
Commit
0d43891
·
verified ·
1 Parent(s): 94459f0

Reduce example and document preparation latency

Browse files
Files changed (3) hide show
  1. app.py +18 -3
  2. dist/assets/index-DKf1_8wi.js +0 -0
  3. dist/index.html +1 -1
app.py CHANGED
@@ -98,6 +98,18 @@ BBOX_IMAGE_PATTERN = re.compile(
98
  r'<img\s+src=["\']images/bbox_(\d+)_(\d+)_(\d+)_(\d+)\.jpg["\']\s*/?>',
99
  flags=re.IGNORECASE,
100
  )
 
 
 
 
 
 
 
 
 
 
 
 
101
  UNMATERIALIZED_BBOX_IMAGE_PATTERN = re.compile(
102
  r'<img\b[^>]*\bsrc=["\']images/bbox_[^"\']+["\'][^>]*>',
103
  flags=re.IGNORECASE,
@@ -549,7 +561,7 @@ if DIST_DIR.is_dir():
549
  ("/vendor", DIST_DIR / "vendor"),
550
  ):
551
  if directory.is_dir():
552
- app.mount(route, StaticFiles(directory=directory), name=route.strip("/").replace("/", "-"))
553
 
554
 
555
  @app.get("/")
@@ -557,12 +569,15 @@ def homepage() -> FileResponse:
557
  index_path = DIST_DIR / "index.html"
558
  if not index_path.is_file():
559
  raise RuntimeError("Frontend build missing. Run `npm run build` before launching app.py.")
560
- return FileResponse(index_path)
561
 
562
 
563
  @app.get("/favicon.ico")
564
  def favicon() -> FileResponse:
565
- return FileResponse(DIST_DIR / "favicon.ico")
 
 
 
566
 
567
 
568
  if __name__ == "__main__":
 
98
  r'<img\s+src=["\']images/bbox_(\d+)_(\d+)_(\d+)_(\d+)\.jpg["\']\s*/?>',
99
  flags=re.IGNORECASE,
100
  )
101
+
102
+
103
+ class CachedStaticFiles(StaticFiles):
104
+ """Serve immutable production assets from the browser cache after first load."""
105
+
106
+ async def get_response(self, path: str, scope: dict[str, Any]) -> Any:
107
+ response = await super().get_response(path, scope)
108
+ if response.status_code == 200:
109
+ response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
110
+ return response
111
+
112
+
113
  UNMATERIALIZED_BBOX_IMAGE_PATTERN = re.compile(
114
  r'<img\b[^>]*\bsrc=["\']images/bbox_[^"\']+["\'][^>]*>',
115
  flags=re.IGNORECASE,
 
561
  ("/vendor", DIST_DIR / "vendor"),
562
  ):
563
  if directory.is_dir():
564
+ app.mount(route, CachedStaticFiles(directory=directory), name=route.strip("/").replace("/", "-"))
565
 
566
 
567
  @app.get("/")
 
569
  index_path = DIST_DIR / "index.html"
570
  if not index_path.is_file():
571
  raise RuntimeError("Frontend build missing. Run `npm run build` before launching app.py.")
572
+ return FileResponse(index_path, headers={"Cache-Control": "no-cache"})
573
 
574
 
575
  @app.get("/favicon.ico")
576
  def favicon() -> FileResponse:
577
+ return FileResponse(
578
+ DIST_DIR / "favicon.ico",
579
+ headers={"Cache-Control": "public, max-age=31536000, immutable"},
580
+ )
581
 
582
 
583
  if __name__ == "__main__":
dist/assets/index-DKf1_8wi.js ADDED
The diff for this file is too large to render. See raw diff
 
dist/index.html CHANGED
@@ -22,7 +22,7 @@
22
  };
23
  </script>
24
  <script defer src="./vendor/mathjax/tex-chtml-full.js"></script>
25
- <script type="module" crossorigin src="./assets/index-5Ju-rOmV.js"></script>
26
  <link rel="stylesheet" crossorigin href="./assets/index-CCa7-Cez.css">
27
  </head>
28
  <body>
 
22
  };
23
  </script>
24
  <script defer src="./vendor/mathjax/tex-chtml-full.js"></script>
25
+ <script type="module" crossorigin src="./assets/index-DKf1_8wi.js"></script>
26
  <link rel="stylesheet" crossorigin href="./assets/index-CCa7-Cez.css">
27
  </head>
28
  <body>