update_shell.py (1114B)
1 # This serves a different response to each request, to test service worker 2 # updates. If |filename| is provided, it writes that file into the body. 3 # 4 # Usage: 5 # navigator.serviceWorker.register('update_shell.py?filename=worker.js') 6 # 7 # This registers worker.js as a service worker, and every update check 8 # will return a new response. 9 import os 10 import random 11 import time 12 13 from wptserve.utils import isomorphic_encode 14 15 def main(request, response): 16 # Set no-cache to ensure the user agent finds a new version for each update. 17 headers = [(b'Cache-Control', b'no-cache, must-revalidate'), 18 (b'Pragma', b'no-cache'), 19 (b'Content-Type', b'application/javascript')] 20 21 # Return a different script for each access. 22 timestamp = u'// %s %s' % (time.time(), random.random()) 23 body = isomorphic_encode(timestamp) + b'\n' 24 25 # Inject the file into the response. 26 if b'filename' in request.GET: 27 path = os.path.join(os.path.dirname(isomorphic_encode(__file__)), 28 request.GET[b'filename']) 29 with open(path, 'rb') as f: 30 body += f.read() 31 32 return headers, body