from functools import partial from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer from urllib.parse import urlsplit class CORSRequestHandler(SimpleHTTPRequestHandler): def end_headers(self): # Remote JS files are imported by apps from a stable URL. Disable HTTP # caching so a full page refresh can pick up a newly deployed version. # Keep hashed demo assets cacheable and leave non-JS responses unchanged. path = urlsplit(self.path).path is_remote_js = path.endswith(".js") and ( path.count("/") == 1 or path.startswith("/chunks/") ) if is_remote_js: self.send_header("Cache-Control", "no-store, max-age=0") self.send_header("Pragma", "no-cache") self.send_header("Expires", "0") self.send_header("Access-Control-Allow-Origin", "*") self.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS") self.send_header( "Access-Control-Allow-Headers", "Content-Type, Authorization", ) super().end_headers() def do_OPTIONS(self): self.send_response(204) self.end_headers() HOST = "0.0.0.0" PORT = 8000 DIRECTORY = "./dist/remote" handler = partial( CORSRequestHandler, directory=DIRECTORY, ) server = ThreadingHTTPServer( (HOST, PORT), handler, ) print(f"Serving {DIRECTORY} on http://{HOST}:{PORT}") server.serve_forever()