#!/usr/bin/env python3 """Reproducer server for crbug.com/505481952. PWA/TWA does not respect the HTTP cache of a `start_url` document on first open. This server reproduces the reporter's exact setup: 1. `/` (the PWA start_url) is served with Cache-Control: private, max-age=1209600 and its inline """ % reload_js REAL = """ Cache Repro

VERSION 2 (real document)

If you see VERSION 1 after closing and reopening the installed app, the HTTP cache was not respected on relaunch.

""" class Handler(http.server.BaseHTTPRequestHandler): def _send(self, body, ctype, cache=None): data = body if isinstance(body, bytes) else body.encode("utf-8") self.send_response(200) self.send_header("Content-Type", ctype) self.send_header("Content-Length", str(len(data))) if cache: self.send_header("Cache-Control", cache) self.end_headers() self.wfile.write(data) def do_GET(self): path = self.path.split("?", 1)[0] if path == "/manifest.webmanifest": self._send(MANIFEST, "application/manifest+json") return if path == "/icon.svg": self._send(ICON, "image/svg+xml") return if path == "/icon-192.png": self._send(ICON_192, "image/png", cache="public, max-age=86400") return if path == "/icon-512.png": self._send(ICON_512, "image/png", cache="public, max-age=86400") return if path == "/": with _lock: n = next(_counter) body = bootstrap_html(RELOAD_DELAY_MS) if n == 1 else REAL # Exactly the header from the bug report. self._send(body, "text/html", cache="private, max-age=1209600") self.log_message("served start_url request #%d -> %s", n, "VERSION 1" if n == 1 else "VERSION 2") return self.send_error(404) def main(): global RELOAD_DELAY_MS ap = argparse.ArgumentParser() ap.add_argument("--port", type=int, default=8000) ap.add_argument("--reload-delay", type=int, default=0, help="ms the stale V1 bootstrap waits before self-reloading " "(use >0 to make a stale relaunch visible on screen)") args = ap.parse_args() RELOAD_DELAY_MS = args.reload_delay httpd = http.server.ThreadingHTTPServer(("0.0.0.0", args.port), Handler) print("Reproducer for crbug.com/505481952 on port %d" % args.port) print("Serve over HTTPS (or adb reverse to localhost) to install as PWA.") print("Reload delay: %d ms" % RELOAD_DELAY_MS) print("Real signal on relaunch: 1 network GET / AND " "sessionStorage.reloaded == 1 => stale (bug).") httpd.serve_forever() if __name__ == "__main__": main()