Spaces:
Running
Running
Send cache headers so updates reach visitors on a normal reload
#3
by lewtun HF Staff - opened
- Dockerfile +3 -3
- README.md +3 -1
- 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", "
|
|
|
|
| 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
|
|
|
|
|
|
|
| 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()
|