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 streaming SSR with multi-byte characters #35724

Merged
merged 1 commit into from Mar 30, 2022
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
10 changes: 7 additions & 3 deletions packages/next/server/node-web-streams-helper.ts
Expand Up @@ -111,8 +111,10 @@ export function encodeText(input: string) {
return new TextEncoder().encode(input)
}

export function decodeText(input?: Uint8Array) {
return new TextDecoder().decode(input)
export function decodeText(input?: Uint8Array, textDecoder?: TextDecoder) {
return textDecoder
? textDecoder.decode(input, { stream: true })
: new TextDecoder().decode(input)
}

export function createTransformStream<Input, Output>({
Expand Down Expand Up @@ -207,9 +209,11 @@ export function createBufferedTransformStream(): TransformStream<
return pendingFlush
}

const textDecoder = new TextDecoder()

return createTransformStream({
transform(chunk, controller) {
bufferedString += decodeText(chunk)
bufferedString += decodeText(chunk, textDecoder)
flushBuffer(controller)
},

Expand Down
14 changes: 14 additions & 0 deletions test/production/react-18-streaming-ssr/index.test.ts
Expand Up @@ -61,6 +61,15 @@ describe('react 18 streaming SSR with custom next configs', () => {
return <p>hello nextjs</p>
}
`,
'pages/multi-byte.js': `
export default function Page() {
return (
<div>
<p>{"マルチバイト".repeat(28)}</p>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

マルチバイト is multi-byte in Japanese. Repeating this word exactly 28 times results in corrupted text in the response.

</div>
);
}
`,
},
nextConfig: {
trailingSlash: true,
Expand Down Expand Up @@ -98,4 +107,9 @@ describe('react 18 streaming SSR with custom next configs', () => {
expect(res.status).toBe(200)
expect(html).toContain('hello nextjs')
})

it('should render multi-byte characters correctly in streaming', async () => {
const html = await renderViaHTTP(next.url, '/multi-byte')
expect(html).toContain('マルチバイト'.repeat(28))
})
})