prerender – React
prerender – React
v19
prerender
prerender renders a React tree to a static HTML string using a
Web Stream.
Reference
prerender(reactNode, options?)
Usage
Troubleshooting
Note
Reference
https://react.dev/reference/react-dom/static/prerender 1/9
20/02/2025, 19:18 prerender – React
prerender(reactNode, options?)
Parameters
https://react.dev/reference/react-dom/static/prerender 2/9
20/02/2025, 19:18 prerender – React
optional namespaceURI : A string with the root namespace URI for the
stream. Defaults to regular HTML. Pass
'http://www.w3.org/2000/svg' for SVG or
'http://www.w3.org/1998/Math/MathML' for MathML.
Returns
prelude : a Web Stream of HTML. You can use this stream to send a
response in chunks, or you can read the entire stream into a string.
If rendering fails, the Promise will be rejected. Use this to output a fallback
shell.
Note
https://react.dev/reference/react-dom/static/prerender 3/9
20/02/2025, 19:18 prerender – React
Usage
Call prerender to render your React tree to static HTML into a Readable
Web Stream::
Along with the root component , you need to provide a list of bootstrap
<script> paths . Your root component should return the entire document
React will inject the doctype and your bootstrap <script> tags into the
resulting HTML stream:
<!DOCTYPE html>
<html>
<!-- ... HTML from your components ... -->
</html>
<script src=" /main.js " async=""></script>
On the client, your bootstrap script should hydrate the entire document with
a call to hydrateRoot :
This will attach event listeners to the static server-generated HTML and
make it interactive.
DEEP DIVE
Show Details
This will produce the initial non-interactive HTML output of your React
components. On the client, you will need to call hydrateRoot to hydrate that
server-generated HTML and make it interactive.
prerender waits for all data to load before finishing the static HTML
generation and resolving. For example, consider a profile page that shows a
cover, a sidebar with friends and photos, and a list of posts:
https://react.dev/reference/react-dom/static/prerender 6/9
20/02/2025, 19:18 prerender – React
function ProfilePage() {
return (
<ProfileLayout>
<ProfileCover />
<Sidebar>
<Friends />
<Photos />
</Sidebar>
<Suspense fallback={<PostsGlimmer />}>
<Posts />
</Suspense>
</ProfileLayout>
);
}
Imagine that <Posts /> needs to load some data, which takes some time.
Ideally, you’d want wait for the posts to finish so it’s included in the HTML. To
do this, you can use Suspense to suspend on the data, and prerender will
wait for the suspended content to finish before resolving to the static HTML.
Note
The exact way you would load data in the Posts component above
depends on your framework. If you use a Suspense-enabled
framework, you’ll find the details in its data fetching documentation.
https://react.dev/reference/react-dom/static/prerender 7/9
20/02/2025, 19:18 prerender – React
Troubleshooting
The prerender response waits for the entire app to finish rendering,
including waiting for all Suspense boundaries to resolve, before resolving. It
is designed for static site generation (SSG) ahead of time and does not
support streaming more content as it loads.
PREVIOUS
Static APIs
NEXT
prerenderToNodeStream
https://react.dev/reference/react-dom/static/prerender 8/9
20/02/2025, 19:18 prerender – React
uwu?
Describing the UI
Adding Interactivity
Managing State
Escape Hatches
Community More
Acknowledgements Terms
https://react.dev/reference/react-dom/static/prerender 9/9