Skip to content

Commit 8b12b17

Browse files
authored
Fix writing strings to the writable stream writer (vercel#32637)
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`
1 parent 3e83205 commit 8b12b17

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

packages/next/server/render.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ function checkRedirectValues(
285285
function createRSCHook() {
286286
const rscCache = new Map()
287287
const decoder = new TextDecoder()
288+
const encoder = new TextEncoder()
288289

289290
return (
290291
writable: WritableStream,
@@ -306,21 +307,22 @@ function createRSCHook() {
306307
if (bootstrap && !bootstrapped) {
307308
bootstrapped = true
308309
writer.write(
309-
`<script>(self.__next_s=self.__next_s||[]).push(${JSON.stringify([
310-
0,
311-
id,
312-
])})</script>`
310+
encoder.encode(
311+
`<script>(self.__next_s=self.__next_s||[]).push(${JSON.stringify(
312+
[0, id]
313+
)})</script>`
314+
)
313315
)
314316
}
315317
if (done) {
316318
writer.close()
317319
} else {
318320
writer.write(
319-
`<script>(self.__next_s=self.__next_s||[]).push(${JSON.stringify([
320-
1,
321-
id,
322-
decoder.decode(value),
323-
])})</script>`
321+
encoder.encode(
322+
`<script>(self.__next_s=self.__next_s||[]).push(${JSON.stringify(
323+
[1, id, decoder.decode(value)]
324+
)})</script>`
325+
)
324326
)
325327
process()
326328
}

0 commit comments

Comments
 (0)