Add live reload support for local development

Replace static HTTP server with livereload-based dev server that watches
posts/, layout/, and static/ directories for changes, rebuilds the site,
and auto-refreshes the browser.
This commit is contained in:
Yax 2026-01-18 17:41:33 +01:00
parent 433d3b4a2a
commit 633c6e0b91
3 changed files with 30 additions and 4 deletions

View file

@ -3,14 +3,14 @@
# declare phony targets # declare phony targets
.PHONY: build test typecheck .PHONY: build test typecheck
# run locally (uses --local-stacosys if stacosys is running on port 8100) # run locally with live reload (uses --local-stacosys if stacosys is running on port 8100)
# watches posts/ for changes, regenerates site, and auto-refreshes browser
site: site:
@if curl -s --connect-timeout 1 http://127.0.0.1:8100/api > /dev/null 2>&1; then \ @if curl -s --connect-timeout 1 http://127.0.0.1:8100/api > /dev/null 2>&1; then \
uv run python makesite.py --local-stacosys; \ uv run python devserver.py --local-stacosys; \
else \ else \
uv run python makesite.py --local; \ uv run python devserver.py --local; \
fi fi
cd _site && python -m SimpleHTTPServer 2> /dev/null || python3 -m http.server
# run type checks # run type checks
typecheck: typecheck:

25
devserver.py Normal file
View file

@ -0,0 +1,25 @@
#!/usr/bin/env python3
"""Development server with live reload."""
import subprocess
import sys
from livereload import Server
def rebuild(flag):
subprocess.run(["uv", "run", "python", "makesite.py", flag])
def main():
flag = sys.argv[1] if len(sys.argv) > 1 else "--local"
rebuild(flag)
server = Server()
server.watch("posts/**/*.md", lambda: rebuild(flag))
server.watch("layout/*.html", lambda: rebuild(flag))
server.watch("static/**/*", lambda: rebuild(flag))
server.serve(root="_site", port=8000, open_url_delay=1)
if __name__ == "__main__":
main()

View file

@ -17,6 +17,7 @@ dependencies = [
[dependency-groups] [dependency-groups]
dev = [ dev = [
"black>=24.10.0", "black>=24.10.0",
"livereload>=2.7.1",
"mypy>=1.19.1", "mypy>=1.19.1",
"types-pygments>=2.19.0.20251121", "types-pygments>=2.19.0.20251121",
"types-requests>=2.32.4.20260107", "types-requests>=2.32.4.20260107",