Skip to content

Instantly share code, notes, and snippets.

@Pauan

Pauan/foo Secret

Created January 26, 2020 01:21
Show Gist options
  • Save Pauan/087dd273b8422a54066db23ee9406403 to your computer and use it in GitHub Desktop.
Save Pauan/087dd273b8422a54066db23ee9406403 to your computer and use it in GitHub Desktop.
Service worker synchronous loading test
Hello world!
<!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>
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