Trying to send an early response (in JSON) with some follow on body, but can’t seem to get the first JSON chunk to show up.
Below there’s an artificial delay, but the JSON is not written to the client until writer.close()
Is there something I’m missing about streamed responses? Is CF only sending streams after a fixed number of bytes? i.e. are they buffered?
// start writing response
const writer = writable.getWriter()
const encoder = new TextEncoder()
writer.write(encoder.encode(JSON.stringify({
success: true
})))
// write some newline characters (doesnt affect JSON parsing)
const uint8 = new Uint8Array(1000)
uint8.fill(10)
writer.write(uint8)
// release lock
writer.releaseLock()
await new Promise((resolve) => setTimeout(resolve, 2000))
writer.close()