self_update_worker.sjs (1186B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 "use strict"; 5 6 const WORKER_BODY = ` 7 onactivate = function(event) { 8 let promise = clients.matchAll({includeUncontrolled: true}).then(function(clients) { 9 for (i = 0; i < clients.length; i++) { 10 clients[i].postMessage({version: version}); 11 } 12 }).then(function() { 13 return self.registration.update(); 14 }); 15 event.waitUntil(promise); 16 }; 17 `; 18 19 function handleRequest(request, response) { 20 if (request.queryString == "clearcounter") { 21 setState("count", "1"); 22 response.write("ok"); 23 return; 24 } 25 26 let count = getState("count"); 27 if (count === "") { 28 count = 1; 29 } else { 30 count = parseInt(count); 31 } 32 33 let worker = "var version = " + count + ";\n"; 34 worker = worker + WORKER_BODY; 35 36 // This header is necessary for making this script able to be loaded. 37 response.setHeader("Content-Type", "application/javascript"); 38 39 // If this is the first request, return the first source. 40 response.write(worker); 41 setState("count", "" + (count + 1)); 42 }