sw_always_updating_inter_sw_postmessage.sjs (1492B)
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 // This file is derived from `self_update_worker.sjs`. 5 "use strict"; 6 7 // We import the entirety of the normal, non-infinitely-updating SW rather than 8 // having meaningful script logic here. This indirection doesn't really change 9 // anything. 10 const WORKER_BODY = ` 11 console.log("Version", version, "importing helper"); 12 importScripts("sw_inter_sw_postmessage.js"); 13 console.log("Version", version, "imported helper"); 14 `; 15 16 function handleRequest(request, response) { 17 let count = getState("count"); 18 dump(`SJS: existing count is ${count}\n`); 19 if (count === "") { 20 count = 1; 21 } else { 22 count = parseInt(count); 23 // test-verify mode unfortunately doesn't do anything on its own to reset 24 // SJS state, which is unfortunate. Our test only goes up to 5, so when we 25 // hit 6 wrap back to 1. 26 if (count === 6) { 27 count = 1; 28 } 29 } 30 dump(`SJS: using count of ${count}\n`); 31 32 let worker = "var version = " + count + ";\n"; 33 worker = worker + WORKER_BODY; 34 35 dump(`SJS BODY::::\n\n${worker}\n\n`); 36 37 // This header is necessary for making this script able to be loaded. 38 response.setHeader("Content-Type", "application/javascript"); 39 40 // If this is the first request, return the first source. 41 response.write(worker); 42 setState("count", "" + (count + 1)); 43 }