from browser import window


def load_and_eval_legacy_script(path: str) -> None:
    # Append a runtime cache-busting token and send no-cache headers so the
    # browser never replays a stale copy of the legacy script from its
    # heuristic HTTP cache (which cannot be fixed retroactively by server
    # headers once a response has already been cached).
    separator = "&" if "?" in path else "?"
    cache_busted = f"{path}{separator}_={window.Date.now()}"
    xhr = window.XMLHttpRequest.new()
    xhr.open("GET", cache_busted, False)
    xhr.setRequestHeader("Cache-Control", "no-cache")
    xhr.setRequestHeader("Pragma", "no-cache")
    xhr.send()
    if xhr.status != 200:
        raise RuntimeError(f"Failed to load legacy script: {path} (status {xhr.status})")
    window.eval(xhr.responseText)


load_and_eval_legacy_script("/app.js")
