Skip to content

Commit

Permalink
Fix writing strings to the writable stream writer (#32637)
Browse files Browse the repository at this point in the history
Only chunks are allowed to write to writable. This fixes the following error in the web runtime:

```
Uncaught (in promise) TypeError: This TransformStream is being used as a byte stream, but received a string on its
writable side. If you wish to write a string, you'll probably want to explicitly UTF-8-encode it with TextEncoder.
```

It doesn't fail in the Node.js sandbox since we polyfilled this case (which should be reverted back later).

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
  • Loading branch information
shuding committed Dec 18, 2021
1 parent 3e83205 commit 8b12b17
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/next/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ function checkRedirectValues(
function createRSCHook() {
const rscCache = new Map()
const decoder = new TextDecoder()
const encoder = new TextEncoder()

return (
writable: WritableStream,
Expand All @@ -306,21 +307,22 @@ function createRSCHook() {
if (bootstrap && !bootstrapped) {
bootstrapped = true
writer.write(
`<script>(self.__next_s=self.__next_s||[]).push(${JSON.stringify([
0,
id,
])})</script>`
encoder.encode(
`<script>(self.__next_s=self.__next_s||[]).push(${JSON.stringify(
[0, id]
)})</script>`
)
)
}
if (done) {
writer.close()
} else {
writer.write(
`<script>(self.__next_s=self.__next_s||[]).push(${JSON.stringify([
1,
id,
decoder.decode(value),
])})</script>`
encoder.encode(
`<script>(self.__next_s=self.__next_s||[]).push(${JSON.stringify(
[1, id, decoder.decode(value)]
)})</script>`
)
)
process()
}
Expand Down

0 comments on commit 8b12b17

Please sign in to comment.