Skip to content

Commit edf6d66

Browse files
authored
Fix streaming demo (#99)
1 parent 223dbb4 commit edf6d66

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

examples/nav-with-service-worker.html

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,21 @@
1414
</main>
1515
</body>
1616
<script>
17+
18+
async function navigate(url) {
19+
const page = new URL(url).searchParams.get("page") || 1;
20+
const stream = document.body.streamAppendHTMLUnsafe();
21+
const response = await fetch("/content?page=" + page);
22+
await response.body
23+
.pipeThrough(new TextDecoderStream())
24+
.pipeTo(stream);
25+
}
1726
const loaded = new Promise((resolve) => {
1827
window.onload = async () => {
1928
navigator.serviceWorker.register("./sw.js");
2029
await navigator.serviceWorker.ready;
2130
console.log("Service worker ready");
31+
await navigate(location.href);
2232
resolve();
2333
};
2434
});
@@ -28,14 +38,8 @@
2838
event.intercept({
2939
async handler() {
3040
await loaded;
31-
const url = new URL(event.destination.url);
32-
const page = url.searchParams.get("page");
33-
const stream = document.body.streamAppendHTMLUnsafe();
34-
const response = await fetch("/content?page=" + page);
35-
await response.body
36-
.pipeThrough(new TextDecoderStream())
37-
.pipeTo(stream);
38-
},
41+
await navigate(event.destination.url);
42+
}
3943
});
4044
}
4145
});

0 commit comments

Comments
 (0)