-
-
Save jakearchibald/18562306e6cbbf975009 to your computer and use it in GitHub Desktop.
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
self.onfetch = event => { | |
const bodyStream = new TransformStream(); | |
event.respondWith(new Response(bodyStream, { | |
headers: {'Content-Type': 'text/html'} | |
})); | |
[ | |
caches.match('/header'), | |
fetch('/body'), | |
caches.match('/footer') | |
].reduce((chain, responsePromise) => { | |
return chain | |
.then(_ => responsePromise) | |
.then(r => r.body.pipeTo(bodyStream, { preventClose: true })) | |
}, Promise.resolve()).then(_ => bodyStream.close()); | |
}; |
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
self.onfetch = async event => { | |
const bodyStream = new TransformStream(); | |
event.respondWith(new Response(bodyStream, { | |
headers: {'Content-Type': 'text/html'} | |
})); | |
const responsePromises = [ | |
caches.match('/header'), | |
fetch('/body'), | |
caches.match('/footer') | |
]; | |
for (let responsePromise of responsePromises) { | |
const body = (await responsePromise).body; | |
await body.pipeTo(bodyStream, { preventClose: true }); | |
} | |
bodyStream.close(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment