Streaming Server-Side Rendering
Streaming rendering is an advanced rendering method that progressively returns content during the page rendering process, significantly improving user experience.
In traditional SSR rendering, the page is rendered all at once, requiring all data to be loaded before returning the complete HTML. In streaming rendering, the page is rendered progressively, allowing content to be returned as it renders, so users can see initial content faster.
Streaming SSR is the default rendering mode for Modern.js SSR. When you enable SSR, streaming rendering is available without additional configuration.
If you need to switch to traditional SSR mode (waiting for all data to load before returning at once), you can configure server.ssr.mode to 'string'. For detailed information, refer to the Server-Side Rendering (SSR) documentation.
Compared to traditional SSR rendering:
- Faster Perceived Speed: Streaming rendering can progressively display content, quickly rendering the home page.
- Enhanced User Experience: Users can see page content faster and interact without waiting for the entire page to render.
- Better Performance Control: Developers can better control the loading priority and order, optimizing performance and user experience.
- Better Adaptability: Streaming rendering adapts better to various network speeds and device performance, ensuring good performance across different environments.
Enabling Streaming Rendering
Modern.js supports React 18+ streaming rendering. When you enable SSR, streaming rendering is enabled by default:
If you need to explicitly specify streaming rendering mode, you can configure:
Modern.js streaming rendering is based on React Router and involves several key APIs:
Await: Used to render the asynchronous data returned by the Data Loader.useAsyncValue: Used to fetch data from the nearest parentAwaitcomponent.
Fetching Data
Here, user is a Promise object representing asynchronously fetched data, processed using defer. Notice that defer must receive an object parameter; a direct Promise cannot be passed.
Additionally, defer can receive both asynchronous and synchronous data. In the example below, short-duration requests are returned using object data, while longer-duration requests are returned using a Promise:
This way, the application can prioritize displaying partially available content without waiting for the most time-consuming data requests.
Rendering Data
To render the asynchronous data returned by the Data Loader, use the Await component. For example:
The Await component needs to be wrapped inside a Suspense component. The resolve prop of Await should be the asynchronously fetched data from the Data Loader. When the data is fetched, it will be rendered using the Render Props pattern. During data fetching, the content set by the fallback prop of Suspense is displayed.
When importing types from the page.data.ts file, use import type to ensure only type information is imported, preventing Data Loader code from being bundled into the frontend.
In the component, you can also fetch asynchronous data returned by the Data Loader using useAsyncValue. For example:
Error Handling
The errorElement prop of the Await component handles errors in Data Loader or sub-component rendering. For example, intentionally throwing an error in the Data Loader function:
Then, fetch the error using useAsyncError and set a component to render the error message for the errorElement prop of the Await component:
Controlling When to Wait for Full HTML
Streaming improves perceived speed, but in some cases (SEO crawlers, A/B buckets, compliance pages) you may want to wait for all content before sending the response.
Modern.js decides the streaming mode with this priority:
- Request header
x-should-stream-all(set per-request in middleware). - Env
MODERN_JS_STREAM_TO_STRING(forces full HTML). - isbot check on
user-agent(bots get full HTML). - Default: stream shell first.
Set the header in your middleware to choose the behavior dynamically:
Related Documentation
- Rendering Mode Overview
- Server-Side Rendering (SSR)
- Rendering Cache
- React Server Components (RSC) - Use with Streaming SSR
- New Suspense SSR Architecture in React 18 - React 18 Architecture Overview