lewtun HF Staff commited on
Commit
445c84c
·
verified ·
1 Parent(s): c327578

Send cache headers so updates reach visitors on a normal reload

Browse files

## What
Cache headers for the demo's files. `server.py` replaces `python -m http.server`: the page, scripts and data (`index.html`, `worker.js`, `pybot_web.py`, `tree.json`) get `Cache-Control: no-cache`; the bot bundle gets `immutable`, the fonts one day.

## Why
After #2 was merged, Lewis's browser kept the old `index.html` and paired it with the new `tree.json`, so file links had the new commit but no line numbers. `http.server` sends no `Cache-Control`, so browsers may reuse a page heuristically after an update.

## How
- `no-cache` still lets browsers keep a copy, but they check it on every load. An unchanged file costs a 304 (`http.server` already handles `If-Modified-Since`).
- The bundle can be cached for good because its name changes with every build (`pybot-<sha12>-<hash>.zip`).
- `Dockerfile` runs `server.py` (port 7860 as before; `HOST`/`PORT` can be overridden for local tests). README updated.

## Testing
Served the live files locally with `server.py`: the headers are as above, and an unchanged `index.html` revalidates with 304. In headless Chromium the demo loads and answers, and "Hi, I'm Sam" links to `chat.py#L14163-L14285`, with no page errors.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Files changed (3) hide show
  1. Dockerfile +3 -3
  2. README.md +3 -1
  3. server.py +24 -0
Dockerfile CHANGED
@@ -1,11 +1,11 @@
1
- # A plain static file server: PyBot itself runs in the visitor's browser (Pyodide, see worker.js).
2
  # Docker rather than sdk: static because a static Space redirects large (LFS/Xet) files to a CDN without CORS
3
  # headers, which blocks the worker's fetch of the bot bundle and the @font-face fonts.
4
  FROM python:3.12-slim
5
  RUN useradd -m -u 1000 user
6
  USER user
7
  WORKDIR /home/user/www
8
- COPY --chown=user index.html worker.js pybot_web.py tree.json pybot-*.zip ./
9
  COPY --chown=user fonts ./fonts
10
  EXPOSE 7860
11
- CMD ["python", "-m", "http.server", "7860", "--bind", "0.0.0.0"]
 
1
+ # A plain static file server (server.py: http.server plus cache headers): PyBot itself runs in the visitor's browser (Pyodide, see worker.js).
2
  # Docker rather than sdk: static because a static Space redirects large (LFS/Xet) files to a CDN without CORS
3
  # headers, which blocks the worker's fetch of the bot bundle and the @font-face fonts.
4
  FROM python:3.12-slim
5
  RUN useradd -m -u 1000 user
6
  USER user
7
  WORKDIR /home/user/www
8
+ COPY --chown=user server.py index.html worker.js pybot_web.py tree.json pybot-*.zip ./
9
  COPY --chown=user fonts ./fonts
10
  EXPOSE 7860
11
+ CMD ["python", "server.py"]
README.md CHANGED
@@ -29,7 +29,9 @@ only serves files. Each reply shows which of PyBot's modules and files produced
29
  - `tree.json`: PyBot's files and line counts for the treemap, and the GitHub repo and commit its links open. Links go
30
  to exact lines (the reply's text, the winner's `respond()`, the first function the winner ran in a file), so `ref` is
31
  a commit, not `main`: line numbers would drift as soon as `main` changes.
32
- - `Dockerfile`: only serves the files above (`python -m http.server`). A static Space would redirect the large files to a
 
 
33
  CDN without CORS headers, which blocks the worker's fetch of the bundle and the fonts.
34
 
35
  ## Rebuild from a newer bot
 
29
  - `tree.json`: PyBot's files and line counts for the treemap, and the GitHub repo and commit its links open. Links go
30
  to exact lines (the reply's text, the winner's `respond()`, the first function the winner ran in a file), so `ref` is
31
  a commit, not `main`: line numbers would drift as soon as `main` changes.
32
+ - `Dockerfile` and `server.py`: only serve the files above (`http.server` plus cache headers: the page, scripts and
33
+ data are revalidated on every load so updates show up on a normal reload; the bundle, renamed on every build, is
34
+ cached for good). A static Space would redirect the large files to a
35
  CDN without CORS headers, which blocks the worker's fetch of the bundle and the fonts.
36
 
37
  ## Rebuild from a newer bot
server.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Serve the demo's files, with cache headers so a Space update reaches visitors on a normal reload.
2
+
3
+ python -m http.server sends no Cache-Control, so browsers may keep an old index.html heuristically and pair it with
4
+ a newer worker.js or tree.json. Here the page, scripts and data are revalidated on every load (a cheap 304 when
5
+ unchanged), and only files whose names change with their content (the bot bundle) are cached for good.
6
+ """
7
+ import os
8
+ from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
9
+
10
+
11
+ class Handler(SimpleHTTPRequestHandler):
12
+ def end_headers(self):
13
+ path = self.path.split("?", 1)[0]
14
+ if path.endswith(".zip"): # pybot-<sha12>-<hash>.zip: renamed on every rebuild
15
+ self.send_header("Cache-Control", "public, max-age=31536000, immutable")
16
+ elif path.startswith("/fonts/"):
17
+ self.send_header("Cache-Control", "public, max-age=86400")
18
+ else: # index.html, worker.js, pybot_web.py, tree.json
19
+ self.send_header("Cache-Control", "no-cache")
20
+ super().end_headers()
21
+
22
+
23
+ if __name__ == "__main__":
24
+ ThreadingHTTPServer((os.environ.get("HOST", "0.0.0.0"), int(os.environ.get("PORT", "7860"))), Handler).serve_forever()