What is Streaming?
Streaming HTML means sending parts of the page to the browser before the entire page is ready. This allows:- Faster initial render - Show content immediately
- Better perceived performance - Users see progress
- Improved UX - No blank page while waiting
- Efficient resource usage - Start rendering before all data loads
Streaming is an experimental feature. The API may change in future versions.
Basic Streaming
UserenderToReadableStream() to stream JSX content:
Suspense Component
TheSuspense component allows you to show a fallback while async content loads:
How Suspense Works
When you useSuspense:
- The fallback is rendered immediately and sent to the client
- The async component starts loading in the background
- Once loaded, the actual content is sent to the client
- Client-side JavaScript replaces the fallback with the real content
Multiple Suspense Boundaries
You can have multipleSuspense boundaries for independent loading states:
Nested Suspense
Suspense boundaries can be nested:StreamingContext
UseStreamingContext to configure streaming behavior, such as adding a nonce for CSP:
Error Handling
Handle errors in streaming with a custom error handler:Async Components
Any component can be async when used with Suspense:Promise Resolution
Suspense automatically handles promises in the component tree:Streaming with the Stream Helper
Combine with Hono’s streaming helper for more control:Performance Considerations
Use Suspense for slow operations
Use Suspense for slow operations
Wrap components that fetch data or perform expensive operations in Suspense boundaries.
Show meaningful loading states
Show meaningful loading states
Provide informative fallbacks that match the content shape.
Stream above-the-fold content first
Stream above-the-fold content first
Structure your page so critical content loads first.
Consider network conditions
Consider network conditions
Streaming is most beneficial on slower networks. On fast connections, the difference may be negligible.
Real-World Example: Product Page
Browser Compatibility
Streaming works in all modern browsers. The generated JavaScript for replacing fallbacks is minimal and works without any external dependencies.Limitations
Next Steps
Server Rendering
Learn about standard server-side rendering
DOM Rendering
Build interactive UIs with client-side rendering
JSX Overview
Back to JSX overview
Streaming Helper
Learn about the streaming helper