{"componentChunkName":"component---src-templates-docs-js","path":"/docs/react-dom-server.html","result":{"data":{"markdownRemark":{"html":"<div class=\"scary\">\n<blockquote>\n<p>These docs are old and won’t be updated. Go to <a href=\"https://react.dev/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">react.dev</a> for the new React docs.</p>\n<p>These new documentation pages teach modern React:</p>\n<ul>\n<li><a href=\"https://react.dev/reference/react-dom/server\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">react-dom</code>: Server APIs</a></li>\n</ul>\n</blockquote>\n</div>\n<p><code class=\"gatsby-code-text\">ReactDOMServer</code> object 使你能將 component render 至靜態標記。它通常是用在 Node 伺服器上：</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token comment\">// ES modules</span>\n<span class=\"token keyword\">import</span> <span class=\"token operator\">*</span> <span class=\"token keyword\">as</span> ReactDOMServer <span class=\"token keyword\">from</span> <span class=\"token string\">'react-dom/server'</span><span class=\"token punctuation\">;</span>\n<span class=\"token comment\">// CommonJS</span>\n<span class=\"token keyword\">var</span> ReactDOMServer <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'react-dom/server'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<h2 id=\"overview\"><a href=\"#overview\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>概觀 </h2>\n<p>These methods are only available in the <strong>environments with <a href=\"https://nodejs.org/api/stream.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Node.js Streams</a>:</strong></p>\n<ul>\n<li><a href=\"#rendertopipeablestream\"><code class=\"gatsby-code-text\">renderToPipeableStream()</code></a></li>\n<li><a href=\"#rendertonodestream\"><code class=\"gatsby-code-text\">renderToNodeStream()</code></a> (Deprecated)</li>\n<li><a href=\"#rendertostaticnodestream\"><code class=\"gatsby-code-text\">renderToStaticNodeStream()</code></a></li>\n</ul>\n<p>These methods are only available in the <strong>environments with <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Streams_API\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Web Streams</a></strong> (this includes browsers, Deno, and some modern edge runtimes):</p>\n<ul>\n<li><a href=\"#rendertoreadablestream\"><code class=\"gatsby-code-text\">renderToReadableStream()</code></a></li>\n</ul>\n<p>The following methods can be used in the environments that don’t support streams:</p>\n<ul>\n<li><a href=\"#rendertostring\"><code class=\"gatsby-code-text\">renderToString()</code></a></li>\n<li><a href=\"#rendertostaticmarkup\"><code class=\"gatsby-code-text\">renderToStaticMarkup()</code></a></li>\n</ul>\n<h2 id=\"reference\"><a href=\"#reference\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reference </h2>\n<h3 id=\"rendertopipeablestream\"><a href=\"#rendertopipeablestream\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">renderToPipeableStream()</code> </h3>\n<div class=\"scary\">\n<blockquote>\n<p>This content is out of date.</p>\n<p>Read the new React documentation for <a href=\"https://react.dev/reference/react-dom/server/renderToPipeableStream\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">renderToPipeableStream</code></a>.</p>\n</blockquote>\n</div>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOMServer<span class=\"token punctuation\">.</span><span class=\"token function\">renderToPipeableStream</span><span class=\"token punctuation\">(</span>element<span class=\"token punctuation\">,</span> options<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Render a React element to its initial HTML. Returns a stream with a <code class=\"gatsby-code-text\">pipe(res)</code> method to pipe the output and <code class=\"gatsby-code-text\">abort()</code> to abort the request. Fully supports Suspense and streaming of HTML with “delayed” content blocks “popping in” via inline <code class=\"gatsby-code-text\">&lt;script></code> tags later. <a href=\"https://github.com/reactwg/react-18/discussions/37\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Read more</a></p>\n<p>If you call <a href=\"/docs/react-dom-client.html#hydrateroot\"><code class=\"gatsby-code-text\">ReactDOM.hydrateRoot()</code></a> on a node that already has this server-rendered markup, React will preserve it and only attach event handlers, allowing you to have a very performant first-load experience.</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">let</span> didError <span class=\"token operator\">=</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">const</span> stream <span class=\"token operator\">=</span> <span class=\"token function\">renderToPipeableStream</span><span class=\"token punctuation\">(</span>\n  <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span><span class=\"token class-name\">App</span></span> <span class=\"token punctuation\">/></span></span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">{</span>\n    <span class=\"token function\">onShellReady</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token comment\">// The content above all Suspense boundaries is ready.</span>\n      <span class=\"token comment\">// If something errored before we started streaming, we set the error code appropriately.</span>\n      res<span class=\"token punctuation\">.</span>statusCode <span class=\"token operator\">=</span> didError <span class=\"token operator\">?</span> <span class=\"token number\">500</span> <span class=\"token operator\">:</span> <span class=\"token number\">200</span><span class=\"token punctuation\">;</span>\n      res<span class=\"token punctuation\">.</span><span class=\"token function\">setHeader</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Content-type'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'text/html'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n      stream<span class=\"token punctuation\">.</span><span class=\"token function\">pipe</span><span class=\"token punctuation\">(</span>res<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token function\">onShellError</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">error</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token comment\">// Something errored before we could complete the shell so we emit an alternative shell.</span>\n      res<span class=\"token punctuation\">.</span>statusCode <span class=\"token operator\">=</span> <span class=\"token number\">500</span><span class=\"token punctuation\">;</span>\n      res<span class=\"token punctuation\">.</span><span class=\"token function\">send</span><span class=\"token punctuation\">(</span>\n        <span class=\"token string\">'&lt;!doctype html>&lt;p>Loading...&lt;/p>&lt;script src=\"clientrender.js\">&lt;/script>'</span>\n      <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token function\">onAllReady</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token comment\">// If you don't want streaming, use this instead of onShellReady.</span>\n      <span class=\"token comment\">// This will fire after the entire page content is ready.</span>\n      <span class=\"token comment\">// You can use this for crawlers or static generation.</span>\n\n      <span class=\"token comment\">// res.statusCode = didError ? 500 : 200;</span>\n      <span class=\"token comment\">// res.setHeader('Content-type', 'text/html');</span>\n      <span class=\"token comment\">// stream.pipe(res);</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token function\">onError</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">err</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      didError <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span>\n      console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>See the <a href=\"https://github.com/facebook/react/blob/14c2be8dac2d5482fda8a0906a31d239df8551fc/packages/react-dom/src/server/ReactDOMFizzServerNode.js#L36-L46\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">full list of options</a>.</p>\n<blockquote>\n<p>Note:</p>\n<p>This is a Node.js-specific API. Environments with <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Streams_API\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Web Streams</a>, like Deno and modern edge runtimes, should use <a href=\"#rendertoreadablestream\"><code class=\"gatsby-code-text\">renderToReadableStream</code></a> instead.</p>\n</blockquote>\n<hr>\n<h3 id=\"rendertoreadablestream\"><a href=\"#rendertoreadablestream\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">renderToReadableStream()</code> </h3>\n<div class=\"scary\">\n<blockquote>\n<p>This content is out of date.</p>\n<p>Read the new React documentation for <a href=\"https://react.dev/reference/react-dom/server/renderToReadableStream\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">renderToReadableStream</code></a>.</p>\n</blockquote>\n</div>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOMServer<span class=\"token punctuation\">.</span><span class=\"token function\">renderToReadableStream</span><span class=\"token punctuation\">(</span>element<span class=\"token punctuation\">,</span> options<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Streams a React element to its initial HTML. Returns a Promise that resolves to a <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Readable Stream</a>. Fully supports Suspense and streaming of HTML. <a href=\"https://github.com/reactwg/react-18/discussions/127\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Read more</a></p>\n<p>If you call <a href=\"/docs/react-dom-client.html#hydrateroot\"><code class=\"gatsby-code-text\">ReactDOM.hydrateRoot()</code></a> on a node that already has this server-rendered markup, React will preserve it and only attach event handlers, allowing you to have a very performant first-load experience.</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token keyword\">let</span> controller <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">AbortController</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">let</span> didError <span class=\"token operator\">=</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">try</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">let</span> stream <span class=\"token operator\">=</span> <span class=\"token keyword\">await</span> <span class=\"token function\">renderToReadableStream</span><span class=\"token punctuation\">(</span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>html</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n      </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>body</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">Success</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>body</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">\n    </span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>html</span><span class=\"token punctuation\">></span></span><span class=\"token punctuation\">,</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token literal-property property\">signal</span><span class=\"token operator\">:</span> controller<span class=\"token punctuation\">.</span>signal<span class=\"token punctuation\">,</span>\n      <span class=\"token function\">onError</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">error</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        didError <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span>\n        console<span class=\"token punctuation\">.</span><span class=\"token function\">error</span><span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n      <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n  <span class=\"token comment\">// This is to wait for all Suspense boundaries to be ready. You can uncomment</span>\n  <span class=\"token comment\">// this line if you want to buffer the entire HTML instead of streaming it.</span>\n  <span class=\"token comment\">// You can use this for crawlers or static generation:</span>\n\n  <span class=\"token comment\">// await stream.allReady;</span>\n\n  <span class=\"token keyword\">return</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Response</span><span class=\"token punctuation\">(</span>stream<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token literal-property property\">status</span><span class=\"token operator\">:</span> didError <span class=\"token operator\">?</span> <span class=\"token number\">500</span> <span class=\"token operator\">:</span> <span class=\"token number\">200</span><span class=\"token punctuation\">,</span>\n    <span class=\"token literal-property property\">headers</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span><span class=\"token string-property property\">'Content-Type'</span><span class=\"token operator\">:</span> <span class=\"token string\">'text/html'</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span> <span class=\"token keyword\">catch</span> <span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Response</span><span class=\"token punctuation\">(</span>\n    <span class=\"token string\">'&lt;!doctype html>&lt;p>Loading...&lt;/p>&lt;script src=\"clientrender.js\">&lt;/script>'</span><span class=\"token punctuation\">,</span>\n    <span class=\"token punctuation\">{</span>\n      <span class=\"token literal-property property\">status</span><span class=\"token operator\">:</span> <span class=\"token number\">500</span><span class=\"token punctuation\">,</span>\n      <span class=\"token literal-property property\">headers</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span><span class=\"token string-property property\">'Content-Type'</span><span class=\"token operator\">:</span> <span class=\"token string\">'text/html'</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>See the <a href=\"https://github.com/facebook/react/blob/14c2be8dac2d5482fda8a0906a31d239df8551fc/packages/react-dom/src/server/ReactDOMFizzServerBrowser.js#L27-L35\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">full list of options</a>.</p>\n<blockquote>\n<p>Note:</p>\n<p>This API depends on <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Streams_API\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Web Streams</a>. For Node.js, use <a href=\"#rendertopipeablestream\"><code class=\"gatsby-code-text\">renderToPipeableStream</code></a> instead.</p>\n</blockquote>\n<hr>\n<h3 id=\"rendertonodestream\"><a href=\"#rendertonodestream\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">renderToNodeStream()</code>  (Deprecated) </h3>\n<div class=\"scary\">\n<blockquote>\n<p>This content is out of date.</p>\n<p>Read the new React documentation for <a href=\"https://react.dev/reference/react-dom/server/renderToNodeStream\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">renderToNodeStream</code></a>.</p>\n</blockquote>\n</div>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOMServer<span class=\"token punctuation\">.</span><span class=\"token function\">renderToNodeStream</span><span class=\"token punctuation\">(</span>element<span class=\"token punctuation\">)</span></code></pre></div>\n<p>這個方法會將一個 React element render 至其初始的 HTML。它會回傳一個 <a href=\"https://nodejs.org/api/stream.html#stream_readable_streams\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Node.js Readable stream</a>並輸出為一個 HTML string。通過 stream 輸出的 HTML 和 <a href=\"#rendertostring\"><code class=\"gatsby-code-text\">ReactDOMServer.renderToString</code></a> 回傳的 HTML 完全相同。你可以使用這個方法在伺服器端產生 HTML，並在初次請求時傳遞 markup ，以加快頁面載入速度，並讓搜尋引擎爬取你的頁面以達到 SEO 最佳化的效果。</p>\n<p>如果你在一個已經有伺服器端 render markup 的 node 上呼叫 <a href=\"/docs/react-dom-client.html#hydrateroot\"><code class=\"gatsby-code-text\">ReactDOM.hydrateRoot()</code></a>，React 將會保留這個 node 並只附上事件處理，這使你能有一個高效能的初次載入體驗。</p>\n<blockquote>\n<p>注意：</p>\n<p>這個 API 只在伺服器端有用。你無法在瀏覽器中使用此 API。</p>\n<p>這個方法回傳的 stream 將會回傳一個由 utf-8 編碼的 byte stream。 如果你需要另一種編碼的 stream，請參考像是 <a href=\"https://www.npmjs.com/package/iconv-lite\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">iconv-lite</a> 這種為轉換文本提供轉換 stream 的專案。</p>\n</blockquote>\n<hr>\n<h3 id=\"rendertostaticnodestream\"><a href=\"#rendertostaticnodestream\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">renderToStaticNodeStream()</code> </h3>\n<div class=\"scary\">\n<blockquote>\n<p>This content is out of date.</p>\n<p>Read the new React documentation for <a href=\"https://react.dev/reference/react-dom/server/renderToStaticNodeStream\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">renderToStaticNodeStream</code></a>.</p>\n</blockquote>\n</div>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOMServer<span class=\"token punctuation\">.</span><span class=\"token function\">renderToStaticNodeStream</span><span class=\"token punctuation\">(</span>element<span class=\"token punctuation\">)</span></code></pre></div>\n<p>這個方法和 <a href=\"#rendertonodestream\"><code class=\"gatsby-code-text\">renderToNodeStream</code></a> 很相似，不過這個方法不會建立那些額外 React 內部使用的 DOM attribute，像是 <code class=\"gatsby-code-text\">data-reactroot</code>。這個方法在你想要用 React 作為一個簡單的靜態頁面生成器時很有用，因為去除一些額外的 attribute 可以省去一些位元組。</p>\n<p>通過這個 stream 輸出的 HTML 和 <a href=\"#rendertostaticmarkup\"><code class=\"gatsby-code-text\">ReactDOMServer.renderToStaticMarkup</code></a> 回傳的 HTML 完全相同。</p>\n<p>如果你打算在前端使用 React 以使得 markup 有互動性的話，請不要使用這個方法。請在伺服器端使用 <a href=\"#rendertonodestream\"><code class=\"gatsby-code-text\">renderToNodeStream</code></a> 並在前端使用 <a href=\"/docs/react-dom-client.html#hydrateroot\"><code class=\"gatsby-code-text\">ReactDOM.hydrateRoot()</code></a>。</p>\n<blockquote>\n<p>注意：</p>\n<p>這個 API 只在伺服器端使用。你無法在瀏覽器中使用此 API。</p>\n<p>這個方法回傳的 stream 將會回傳一個由 utf-8 編碼的 byte stream。 如果你需要另一種編碼的 stream，請參考像是 <a href=\"https://www.npmjs.com/package/iconv-lite\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">iconv-lite</a> 這種為轉換文本提供轉換 stream 的專案。</p>\n</blockquote>\n<hr>\n<h3 id=\"rendertostring\"><a href=\"#rendertostring\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">renderToString()</code> </h3>\n<div class=\"scary\">\n<blockquote>\n<p>This content is out of date.</p>\n<p>Read the new React documentation for <a href=\"https://react.dev/reference/react-dom/server/renderToString\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">renderToString</code></a>.</p>\n</blockquote>\n</div>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOMServer<span class=\"token punctuation\">.</span><span class=\"token function\">renderToString</span><span class=\"token punctuation\">(</span>element<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Render a React element to its initial HTML. React will return an HTML string. You can use this method to generate HTML on the server and send the markup down on the initial request for faster page loads and to allow search engines to crawl your pages for SEO purposes.</p>\n<p>If you call <a href=\"/docs/react-dom-client.html#hydrateroot\"><code class=\"gatsby-code-text\">ReactDOM.hydrateRoot()</code></a> on a node that already has this server-rendered markup, React will preserve it and only attach event handlers, allowing you to have a very performant first-load experience.</p>\n<blockquote>\n<p>Note</p>\n<p>This API has limited Suspense support and does not support streaming.</p>\n<p>On the server, it is recommended to use either <a href=\"#rendertopipeablestream\"><code class=\"gatsby-code-text\">renderToPipeableStream</code></a> (for Node.js) or <a href=\"#rendertoreadablestream\"><code class=\"gatsby-code-text\">renderToReadableStream</code></a> (for Web Streams) instead.</p>\n</blockquote>\n<hr>\n<h3 id=\"rendertostaticmarkup\"><a href=\"#rendertostaticmarkup\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code class=\"gatsby-code-text\">renderToStaticMarkup()</code> </h3>\n<div class=\"scary\">\n<blockquote>\n<p>This content is out of date.</p>\n<p>Read the new React documentation for <a href=\"https://react.dev/reference/react-dom/server/renderToStaticMarkup\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><code class=\"gatsby-code-text\">renderToStaticMarkup</code></a>.</p>\n</blockquote>\n</div>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\">ReactDOMServer<span class=\"token punctuation\">.</span><span class=\"token function\">renderToStaticMarkup</span><span class=\"token punctuation\">(</span>element<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Similar to <a href=\"#rendertostring\"><code class=\"gatsby-code-text\">renderToString</code></a>, except this doesn’t create extra DOM attributes that React uses internally, such as <code class=\"gatsby-code-text\">data-reactroot</code>. This is useful if you want to use React as a simple static page generator, as stripping away the extra attributes can save some bytes.</p>\n<p>If you plan to use React on the client to make the markup interactive, do not use this method. Instead, use <a href=\"#rendertostring\"><code class=\"gatsby-code-text\">renderToString</code></a> on the server and <a href=\"/docs/react-dom-client.html#hydrateroot\"><code class=\"gatsby-code-text\">ReactDOM.hydrateRoot()</code></a> on the client.</p>","frontmatter":{"title":"ReactDOMServer","next":null,"prev":null},"fields":{"path":"content/docs/reference-react-dom-server.md","slug":"docs/react-dom-server.html"}}},"pageContext":{"slug":"docs/react-dom-server.html"}},"staticQueryHashes":[]}