Files
Linus Torvalds dada49fccd WebMIDI: keep the service worker out of local development
Editing app.js and then testing the old one is not a good use of anyone's
afternoon, and that is exactly what happens right now.

Two things conspire.  The dev server sends no cache headers at all, so
the browser falls back to heuristic caching and makes up its own mind
about how long to hold onto things.  And the service worker caches
app.js by name, which would be fine except that unregistering a service
worker does not actually stop it: the active worker keeps controlling
already-controlled clients until every one of them goes away, and
reloading the page navigates straight back through the worker you just
tried to get rid of.  So the "Update App" button reliably fails to
update the app, which is a special kind of unhelpful.

The symptom is nasty because it is silent.  A fresh index.html with a
stale app.js gives you the new UI with none of the handlers attached, so
buttons are simply inert - no error, nothing in the console, just
nothing happening.  I spent a debugging round assuming the new code was
wrong when it had been right all along.

So don't register the worker at all on localhost, and unregister any
that a previous visit left behind.  The PWA behaviour only matters for
the deployed copy on github.io, and that is untouched.

Also make server.py send 'Cache-Control: no-store'.  That end_headers()
override was an empty stub with a comment about adding headers "if
needed in future".

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2026-07-27 14:02:41 -07:00

29 lines
864 B
Python
Executable File

#!/usr/bin/env python3
import http.server
import socketserver
import sys
PORT = 8080
class Handler(http.server.SimpleHTTPRequestHandler):
# Never let the browser cache anything from the development server.
# Without this it applies heuristic caching (there are no cache headers
# to go by), and you end up editing app.js and testing the old one.
def end_headers(self):
self.send_header("Cache-Control", "no-store")
super().end_headers()
if len(sys.argv) > 1:
try:
PORT = int(sys.argv[1])
except ValueError:
pass
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"Serving at http://localhost:{PORT}")
print("Use this URL in your Web-MIDI capable browser (like Chrome).")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down server.")