Skip to content

Instantly share code, notes, and snippets.

@jakearchibald
Created October 15, 2015 11:25
Show Gist options
  • Save jakearchibald/18562306e6cbbf975009 to your computer and use it in GitHub Desktop.
Save jakearchibald/18562306e6cbbf975009 to your computer and use it in GitHub Desktop.
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());
};
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