update-recovery-worker.py (1084B)
1 def main(request, response): 2 # Set mode to 'init' for initial fetch. 3 mode = b'init' 4 if b'update-recovery-mode' in request.cookies: 5 mode = request.cookies[b'update-recovery-mode'].value 6 7 # no-cache itself to ensure the user agent finds a new version for each update. 8 headers = [(b'Cache-Control', b'no-cache, must-revalidate'), 9 (b'Pragma', b'no-cache')] 10 11 extra_body = b'' 12 13 if mode == b'init': 14 # Install a bad service worker that will break the controlled 15 # document navigation. 16 response.set_cookie(b'update-recovery-mode', b'bad') 17 extra_body = b"addEventListener('fetch', function(e) { e.respondWith(Promise.reject()); });" 18 elif mode == b'bad': 19 # When the update tries to pull the script again, update to 20 # a worker service worker that does not break document 21 # navigation. Serve the same script from then on. 22 response.delete_cookie(b'update-recovery-mode') 23 24 headers.append((b'Content-Type', b'application/javascript')) 25 return headers, b'%s' % (extra_body)