-
-
Save Pauan/087dd273b8422a54066db23ee9406403 to your computer and use it in GitHub Desktop.
Service worker synchronous loading test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello world! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Service Worker</title> | |
</head> | |
<body> | |
<script> | |
if ('serviceWorker' in navigator) { | |
var counter = 0; | |
setInterval(() => { | |
var id = ++counter; | |
console.log("FETCHING", id); | |
fetch("./foo").then((x) => { | |
return x.text(); | |
}).then((x) => { | |
console.log("FETCH SUCCESSFUL", id); | |
}).catch((e) => { | |
console.error("FETCH FAILED", id, e); | |
}); | |
}, 1000); | |
navigator.serviceWorker.register("./worker.js") | |
.then((reg) => { | |
console.log("Registration succeeded. Scope is " + reg.scope); | |
}).catch((error) => { | |
console.log("Registration failed", error); | |
}); | |
} | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log("INITIALIZING WORKER"); | |
// Simulates synchronous loading | |
function loadSync() { | |
const end = Date.now() + 60000; | |
while (Date.now() < end) {} | |
} | |
loadSync(); | |
// Simulates asynchronous loading | |
var loadingWasm = new Promise((resolve) => { | |
setTimeout(() => { | |
resolve(); | |
}, 60000); | |
}); | |
self.addEventListener("fetch", (req) => { | |
console.log("RESPONDING"); | |
req.respondWith(loadingWasm.then(() => { | |
return fetch(req.request); | |
})); | |
}); | |
console.log("WORKER INITIALIZED"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment