Skip to content

Commit

Permalink
fix(server-renderer): pipeToWebWritable CF worker compat
Browse files Browse the repository at this point in the history
fix #4287
  • Loading branch information
yyx990803 committed Aug 10, 2021
1 parent e04680b commit 2224610
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
3 changes: 1 addition & 2 deletions packages/server-renderer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
39 changes: 22 additions & 17 deletions packages/server-renderer/src/renderToStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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()
}
})
}

0 comments on commit 2224610

Please sign in to comment.