fix(server-renderer): pipeToWebWritable CF worker compat

fix #4287
This commit is contained in:
Evan You 2021-08-10 11:46:44 -04:00
parent e04680b0aa
commit 2224610b0b
2 changed files with 23 additions and 19 deletions

View File

@ -92,8 +92,7 @@ Renders input as a [Web ReadableStream](https://developer.mozilla.org/en-US/docs
```ts
function renderToWebStream(
input: App | VNode,
context?: SSRContext,
Ctor?: { new (): ReadableStream }
context?: SSRContext
): ReadableStream
```

View File

@ -74,9 +74,7 @@ export function renderToSimpleStream<T extends SimpleReadable>(
Promise.resolve(renderComponentVNode(vnode))
.then(buffer => unrollBuffer(buffer, stream))
.then(() => {
stream.push(null)
})
.then(() => stream.push(null))
.catch(error => {
stream.destroy(error)
})
@ -180,20 +178,27 @@ export function pipeToWebWritable(
const writer = writable.getWriter()
const encoder = new TextEncoder()
writer.ready.then(() => {
renderToSimpleStream(input, context, {
push(content) {
if (content != null) {
writer.write(encoder.encode(content))
} else {
writer.close()
}
},
destroy(err) {
// TODO better error handling?
console.log(err)
writer.close()
// #4287 CloudFlare workers do not implement `ready` property
let hasReady = false
try {
hasReady = isPromise(writer.ready)
} catch (e) {}
renderToSimpleStream(input, context, {
async push(content) {
if (hasReady) {
await writer.ready
}
})
if (content != null) {
return writer.write(encoder.encode(content))
} else {
return writer.close()
}
},
destroy(err) {
// TODO better error handling?
console.log(err)
writer.close()
}
})
}