Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ReadableStream.pipeTo() being unimplemented in the web runtime #32602

Merged
merged 2 commits into from Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -101,6 +101,10 @@ export function getRender({
renderOpts
)
} catch (err: any) {
console.error(
'An error occurred while rendering the initial result:',
err
)
const errorRes = { statusCode: 500, err }
try {
result = await renderToHTML(
Expand Down
39 changes: 23 additions & 16 deletions packages/next/server/render.tsx
Expand Up @@ -298,28 +298,35 @@ function createRSCHook() {
entry = createFromReadableStream(renderStream)
rscCache.set(id, entry)

const transfomer = new TransformStream({
start(controller) {
if (bootstrap) {
controller.enqueue(
let bootstrapped = false
shuding marked this conversation as resolved.
Show resolved Hide resolved
const forwardReader = forwardStream.getReader()
const writer = writable.getWriter()
function process() {
forwardReader.read().then(({ done, value }) => {
if (bootstrap && !bootstrapped) {
bootstrapped = true
writer.write(
`<script>(self.__next_s=self.__next_s||[]).push(${JSON.stringify([
0,
id,
])})</script>`
)
}
},
transform(chunk, controller) {
controller.enqueue(
`<script>(self.__next_s=self.__next_s||[]).push(${JSON.stringify([
1,
id,
decoder.decode(chunk),
])})</script>`
)
},
})
forwardStream.pipeThrough(transfomer).pipeTo(writable)
if (done) {
writer.close()
} else {
writer.write(
`<script>(self.__next_s=self.__next_s||[]).push(${JSON.stringify([
1,
id,
decoder.decode(value),
])})</script>`
)
process()
}
})
}
process()
}
return entry
}
Expand Down