Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions examples/nav-with-service-worker.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@
</main>
</body>
<script>

async function navigate(url) {
const page = new URL(url).searchParams.get("page") || 1;
const stream = document.body.streamAppendHTMLUnsafe();
const response = await fetch("/content?page=" + page);
await response.body
.pipeThrough(new TextDecoderStream())
.pipeTo(stream);
}
const loaded = new Promise((resolve) => {
window.onload = async () => {
navigator.serviceWorker.register("./sw.js");
await navigator.serviceWorker.ready;
console.log("Service worker ready");
await navigate(location.href);
resolve();
};
});
Expand All @@ -28,14 +38,8 @@
event.intercept({
async handler() {
await loaded;
const url = new URL(event.destination.url);
const page = url.searchParams.get("page");
const stream = document.body.streamAppendHTMLUnsafe();
const response = await fetch("/content?page=" + page);
await response.body
.pipeThrough(new TextDecoderStream())
.pipeTo(stream);
},
await navigate(event.destination.url);
}
});
}
});
Expand Down
Loading