I'm trying to return a stream for the HTML response in a service worker but the browser does not seem to be able to parse it (I'm using chrome for this test).
So, this one works (in the fetch event):
event.respondWith(new Response("<h1>Yellow!</h1>", { headers: { "Content-Type": "text/html" }}))
But when I use a ReadableStream it no longer renders in the browser:
const stream = new ReadableStream({
start(controller) {
controller.enqueue("<h1>Yellow!</h1>")
controller.close()
}
})
event.respondWith(new Response(stream, { headers: { "Content-Type": "text/html" }}))
It seems like maybe the browser doesn't understand that I'm sending it a stream. But I don't know what headers I would need to use so the above works.
Resources that I have used:
https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#Examples
https://philipwalton.com/articles/smaller-html-payloads-with-service-workers/
UPDATE