#!/usr/bin/env python3 """Dev server for Molly Cool. Plain http.server caches ES modules hard enough that you edit a file, reload, and still run the old code — which costs more debugging time than it has any right to. This just turns caching off. """ import sys from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer import re VERSIONED = re.compile(r"^/v\d+/") class NoCache(SimpleHTTPRequestHandler): def translate_path(self, path): # /v1234567/src/main.js -> /src/main.js # # index.html imports the entry under a fresh /v/ prefix each # load, so every relative import inside the graph resolves to a URL the # browser has never seen. No-cache headers alone don't do it: browsers # cache the *parsed* module by URL, so an edited file can keep running # its old code until the URL itself changes. return super().translate_path(VERSIONED.sub("/", path)) def end_headers(self): self.send_header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0") self.send_header("Pragma", "no-cache") self.send_header("Expires", "0") super().end_headers() def log_message(self, fmt, *args): pass # quiet if __name__ == "__main__": port = int(sys.argv[1]) if len(sys.argv) > 1 else 5178 print(f"molly cool -> http://localhost:{port}") ThreadingHTTPServer(("", port), NoCache).serve_forever()