renderToNodeStream

Deprecated

์ด API๋Š” ํ–ฅํ›„ React์˜ ์ฃผ์š” ๋ฒ„์ „์—์„œ ์ œ๊ฑฐ๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ๋Œ€์‹  renderToPipeableStream์„ ์‚ฌ์šฉํ•˜์„ธ์š”.

renderToNodeStream์€ React ํŠธ๋ฆฌ๋ฅผ Node.js Readable Stream.์œผ๋กœ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.

const stream = renderToNodeStream(reactNode)

๋ž˜ํผ๋Ÿฐ์Šค

renderToNodeStream(reactNode)

์„œ๋ฒ„์—์„œ renderToNodeStream์„ ํ˜ธ์ถœํ•˜์—ฌ ์‘๋‹ต์œผ๋กœ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” Node.js Readable Stream์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

import { renderToNodeStream } from 'react-dom/server';

const stream = renderToNodeStream(<App />)
stream.pipe(response);

ํด๋ผ์ด์–ธํŠธ์—์„œ hydrateRoot๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์„œ๋ฒ„์—์„œ ์ƒ์„ฑ๋œ HTML์„ ์ƒํ˜ธ์ž‘์šฉ ๊ฐ€๋Šฅํ•˜๋„๋ก ๋งŒ๋“œ์„ธ์š”.

์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

๋งค๊ฐœ๋ณ€์ˆ˜

  • reactNode: HTML๋กœ ๋ Œ๋”๋งํ•˜๋ ค๋Š” React ๋…ธ๋“œ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, <App />๊ณผ ๊ฐ™์€ JSX ์—˜๋ฆฌ๋จผํŠธ์ž…๋‹ˆ๋‹ค.

๋ฐ˜ํ™˜

HTML ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋Š” Node.js Readable Stream๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์˜ ์‚ฌํ•ญ

  • ์ด ๋ฉ”์„œ๋“œ๋Š” ์ถœ๋ ฅ๋ฌผ์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์ „์— ๋ชจ๋“  Suspense boundaries)๊ฐ€ ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐํ•ฉ๋‹ˆ๋‹ค.

  • React 18๋ถ€ํ„ฐ ์ด ๋ฉ”์„œ๋“œ๋Š” ๋ชจ๋“  ์ถœ๋ ฅ๊ฐ’์„ ๋ฒ„ํผ๋งํ•˜๋ฏ€๋กœ ์‹ค์ œ๋กœ ์ŠคํŠธ๋ฆฌ๋ฐ ์ด์ ์„ ์ œ๊ณตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ renderToPipeableStream์œผ๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฐ˜ํ™˜๋œ ์ŠคํŠธ๋ฆผ์€ utf-8๋กœ ์ธ์ฝ”๋”ฉ๋œ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ธ์ฝ”๋”ฉ ์ŠคํŠธ๋ฆผ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ํ…์ŠคํŠธ ๋ณ€ํ™˜์„ ์œ„ํ•œ ๋ณ€ํ™˜ ์ŠคํŠธ๋ฆผ์„ ์ œ๊ณตํ•˜๋Š” iconv-lite์™€ ๊ฐ™์€ ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”.


์‚ฌ์šฉ๋ฒ•

React ํŠธ๋ฆฌ๋ฅผ HTML๋กœ Node.js Readable Stream์— ๋ Œ๋”๋งํ•˜๊ธฐ

renderToNodeStream ์„ ํ˜ธ์ถœํ•˜์—ฌ ์„œ๋ฒ„ ์‘๋‹ต์œผ๋กœ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” Node.js Readable Stream์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

import { renderToNodeStream } from 'react-dom/server';

// ๋ผ์šฐํŠธ ํ•ธ๋“ค๋Ÿฌ ๊ตฌ๋ฌธ์€ ๋ฐฑ์—”๋“œ ํ”„๋ ˆ์ž„์›Œํฌ์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
app.use('/', (request, response) => {
const stream = renderToNodeStream(<App />);
stream.pipe(response);
});

์ŠคํŠธ๋ฆผ์€ React ์ปดํฌ๋„ŒํŠธ์˜ ์ดˆ๊ธฐ์— ์ƒํ˜ธ์ž‘์šฉํ•˜์ง€ ์•Š๋Š” HTML ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„์—์„œ ์ƒ์„ฑ๋œ HTML์„ hydrate ์ฒ˜๋ฆฌํ•˜๊ณ  ์ƒํ˜ธ์ž‘์šฉํ•˜๋„๋ก ํ•˜๋ ค๋ฉด hydrateRoot๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.