Skip to content

Commit 867be3e

Browse files
authored
Merge pull request #448 from preactjs/document-streaming
Add streaming to readme
2 parents e5dbe50 + 5e1b20b commit 867be3e

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,104 @@ main().catch((error) => {
144144

145145
---
146146

147+
### Streaming
148+
149+
> [!NOTE]
150+
> This is an early version of our streaming implementation.
151+
152+
Preact supports streaming HTML to the client incrementally, flushing `<Suspense>` fallbacks immediately and replacing them with the resolved content as data arrives. This reduces Time to First Byte and allows the browser to start parsing earlier.
153+
154+
#### `renderToReadableStream` — Web Streams
155+
156+
```jsx
157+
import { renderToReadableStream } from 'preact-render-to-string/stream';
158+
import { Suspense, lazy } from 'preact/compat';
159+
160+
const Profile = lazy(() => import('./Profile'));
161+
162+
const App = () => (
163+
<html>
164+
<head><title>My App</title></head>
165+
<body>
166+
<Suspense fallback={<p>Loading profile…</p>}>
167+
<Profile />
168+
</Suspense>
169+
</body>
170+
</html>
171+
);
172+
173+
// Works in any Web Streams environment (Deno, Bun, Cloudflare Workers, …)
174+
export default {
175+
fetch() {
176+
const stream = renderToReadableStream(<App />);
177+
178+
// stream.allReady resolves once all suspended content has been flushed
179+
return new Response(stream, {
180+
headers: { 'Content-Type': 'text/html' }
181+
});
182+
}
183+
};
184+
```
185+
186+
The returned `ReadableStream` has an extra `allReady: Promise<void>` property that resolves once every suspended subtree has been written. Await it before sending the response if you need the complete document before anything is flushed (e.g. for static export). At which point you might be better off using `renderToStringAsync` though.
187+
188+
```js
189+
const stream = renderToReadableStream(<App />);
190+
await stream.allReady; // wait for full render
191+
```
192+
193+
#### `renderToPipeableStream` — Node.js Streams
194+
195+
```jsx
196+
import { createServer } from 'node:http';
197+
import { renderToPipeableStream } from 'preact-render-to-string/stream-node';
198+
import { Suspense, lazy } from 'preact/compat';
199+
200+
const Profile = lazy(() => import('./Profile'));
201+
202+
const App = () => (
203+
<html>
204+
<head><title>My App</title></head>
205+
<body>
206+
<Suspense fallback={<p>Loading profile…</p>}>
207+
<Profile />
208+
</Suspense>
209+
</body>
210+
</html>
211+
);
212+
213+
createServer((req, res) => {
214+
res.setHeader('Content-Type', 'text/html');
215+
216+
const { pipe, abort } = renderToPipeableStream(<App />, {
217+
onShellReady() {
218+
// Called once the synchronous shell is ready to stream.
219+
pipe(res);
220+
},
221+
onAllReady() {
222+
// Called once every suspended subtree has been flushed.
223+
},
224+
onError(error) {
225+
console.error(error);
226+
res.statusCode = 500;
227+
}
228+
});
229+
230+
// Optional: abort the render after a timeout
231+
setTimeout(abort, 10_000);
232+
}).listen(8080);
233+
```
234+
235+
| Option | Description |
236+
|---|---|
237+
| `onShellReady()` | Called synchronously once the initial shell has been rendered and streaming is about to start. Pipe here for fastest TTFB. |
238+
| `onAllReady()` | Called after all `<Suspense>` boundaries have resolved and the stream is complete. |
239+
| `onError(error)` | Called for render errors inside suspended subtrees. |
240+
241+
Calling `abort()` stops the render and destroys the stream; any pending suspended subtrees are dropped.
242+
243+
---
244+
147245
### License
148246

149247
[MIT](http://choosealicense.com/licenses/mit/)

0 commit comments

Comments
 (0)