bug1047663_worker.sjs (971B)
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_1 = ` 7 "use strict"; 8 9 self.onmessage = function () { 10 postMessage("one"); 11 }; 12 `; 13 14 const WORKER_2 = ` 15 "use strict"; 16 17 self.onmessage = function () { 18 postMessage("two"); 19 }; 20 `; 21 22 function handleRequest(request, response) { 23 let count = getState("count"); 24 if (count === "") { 25 count = "1"; 26 } 27 28 // This header is necessary for the cache to trigger. 29 response.setHeader("Cache-control", "max-age=3600"); 30 response.setHeader("Content-Type", "text/javascript", false); 31 32 // If this is the first request, return the first source. 33 if (count === "1") { 34 response.write(WORKER_1); 35 setState("count", "2"); 36 } 37 // For all subsequent requests, return the second source. 38 else { 39 response.write(WORKER_2); 40 } 41 }