Skip to content

Commit e3b30c6

Browse files
committed
migrate server rendering reference pages
1 parent e2091bc commit e3b30c6

4 files changed

Lines changed: 270 additions & 83 deletions

File tree

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,118 @@
11
---
22
title: renderToStream
33
use_cases: >-
4-
streaming ssr, progressive rendering, async data loading, suspense boundaries,
5-
large page optimization
4+
streaming server rendering, progressive HTML delivery, suspense-driven server
5+
output
66
tags:
77
- ssr
88
- streaming
99
- async
1010
- suspense
11-
- performance
1211
version: "1.0"
1312
description: >-
14-
Stream HTML progressively with renderToStream. Render content synchronously
15-
then stream async resources as they complete for faster loading.
13+
Stream server-rendered HTML and continue writing async content as it
14+
resolves.
1615
---
1716

17+
`renderToStream` streams server-rendered HTML and continues writing async content as it resolves.
18+
19+
## Import
20+
1821
```ts
1922
import { renderToStream } from "solid-js/web";
23+
```
2024

25+
## Type
26+
27+
```ts
2128
function renderToStream<T>(
2229
fn: () => T,
2330
options?: {
2431
nonce?: string;
2532
renderId?: string;
26-
onCompleteShell?: () => void;
27-
onCompleteAll?: () => void;
33+
onCompleteShell?: (info: { write: (v: string) => void }) => void;
34+
onCompleteAll?: (info: { write: (v: string) => void }) => void;
35+
onError?: (err: any) => void;
2836
}
2937
): {
3038
pipe: (writable: { write: (v: string) => void }) => void;
31-
pipeTo: (writable: WritableStream) => void;
39+
pipeTo: (writable: WritableStream) => Promise<void>;
3240
};
3341
```
3442

35-
This method renders to a stream.
36-
It renders the content synchronously including any Suspense fallback placeholders, and then continues to stream the data and HTML from any async resource as it completes.
43+
## Parameters
44+
45+
### `fn`
46+
47+
- **Type:** `() => T`
48+
49+
Function that returns the root output to render.
50+
51+
### `options`
52+
53+
#### `nonce`
54+
55+
- **Type:** `string`
56+
57+
Nonce applied to inline scripts emitted during rendering.
58+
59+
#### `renderId`
60+
61+
- **Type:** `string`
62+
63+
Identifier used to namespace the render output.
64+
65+
#### `onCompleteShell`
66+
67+
- **Type:** `(info: { write: (v: string) => void }) => void`
68+
69+
Callback invoked when the shell is ready to flush.
70+
71+
#### `onCompleteAll`
72+
73+
- **Type:** `(info: { write: (v: string) => void }) => void`
74+
75+
Callback invoked after all server suspense boundaries have settled.
76+
77+
#### `onError`
78+
79+
- **Type:** `(err: any) => void`
80+
81+
Callback invoked when a rendering error is encountered.
82+
83+
## Return value
84+
85+
- **Type:** `{ pipe: ..., pipeTo: ... }`
86+
87+
Streaming controller with `pipe` and `pipeTo` methods.
88+
89+
## Behavior
90+
91+
- `renderToStream` writes the shell synchronously, including suspense fallback content.
92+
- Async content and serialized data continue streaming as resources resolve.
93+
- `pipe` writes to Node-style writable targets.
94+
- `pipeTo` writes to a `WritableStream`.
95+
96+
## Examples
97+
98+
### `pipe`
3799

38100
```ts
39-
// node
40-
renderToStream(App).pipe(res);
101+
import { renderToStream } from "solid-js/web";
41102

42-
// web stream
43-
const { readable, writable } = new TransformStream();
44-
renderToStream(App).pipeTo(writable);
103+
renderToStream(() => <App />).pipe(response);
45104
```
46105

47-
`onCompleteShell` fires when synchronous rendering is complete before writing the first flush to the stream out to the browser.
48-
`onCompleteAll` is called when all server Suspense boundaries have settled.
49-
`renderId` is used to namespace renders when having multiple top level roots.
106+
### `pipeTo`
50107

51-
:::note
52-
This API replaces the previous pipeToWritable and pipeToNodeWritable
53-
APIs.
54-
:::
108+
```ts
109+
import { renderToStream } from "solid-js/web";
110+
111+
const { writable } = new TransformStream();
112+
await renderToStream(() => <App />).pipeTo(writable);
113+
```
55114

56-
## Options
115+
## Related
57116

58-
| Name | Type | Description |
59-
| --------------- | ---------- | ---------------------------------------------------------------- |
60-
| nonce | string | The nonce to use for inline scripts. |
61-
| renderId | string | The id to use for this render. |
62-
| onCompleteShell | () => void | A callback that fires when the shell is complete. |
63-
| onCompleteAll | () => void | A callback that fires when all Suspense boundaries have settled. |
117+
- [`renderToString`](/reference/rendering/render-to-string)
118+
- [`renderToStringAsync`](/reference/rendering/render-to-string-async)
Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,104 @@
11
---
22
title: renderToStringAsync
33
use_cases: >-
4-
ssr with async data, suspense resolution, complete page rendering, data
5-
serialization, seo optimization
4+
async server rendering, waiting for suspense boundaries, HTML generation with
5+
async data
66
tags:
77
- ssr
88
- async
99
- suspense
10-
- serialization
1110
- rendering
1211
version: "1.0"
1312
description: >-
14-
Render complete HTML with async data using renderToStringAsync. Waits for all
15-
Suspense boundaries before returning serialized results.
13+
Render HTML to a string after async suspense boundaries settle.
1614
---
1715

16+
`renderToStringAsync` renders HTML to a string after async suspense boundaries settle.
17+
18+
## Import
19+
1820
```ts
1921
import { renderToStringAsync } from "solid-js/web";
22+
```
2023

24+
## Type
25+
26+
```ts
2127
function renderToStringAsync<T>(
2228
fn: () => T,
2329
options?: {
2430
timeoutMs?: number;
25-
renderId?: string;
2631
nonce?: string;
32+
renderId?: string;
33+
noScripts?: boolean;
34+
onError?: (err: any) => void;
2735
}
2836
): Promise<string>;
2937
```
3038

31-
Same as `renderToString` except that it will wait for all `<Suspense>` boundaries to resolve before returning the results.
32-
Resource data is automatically serialized into the script tag and will be hydrated on client load.
39+
## Parameters
40+
41+
### `fn`
42+
43+
- **Type:** `() => T`
44+
45+
Function that returns the root output to render.
46+
47+
### `options`
48+
49+
#### `timeoutMs`
50+
51+
- **Type:** `number`
52+
53+
Maximum wait time for suspense boundaries before the render resolves.
54+
55+
#### `nonce`
56+
57+
- **Type:** `string`
58+
59+
Nonce applied to inline scripts emitted during rendering.
3360

34-
`renderId` is used to namespace renders when having multiple top level roots.
61+
#### `renderId`
62+
63+
- **Type:** `string`
64+
65+
Identifier used to namespace the render output.
66+
67+
#### `noScripts`
68+
69+
- **Type:** `boolean`
70+
71+
Disables script emission in the rendered output.
72+
73+
#### `onError`
74+
75+
- **Type:** `(err: any) => void`
76+
77+
Callback invoked when a rendering error is encountered.
78+
79+
## Return value
80+
81+
- **Type:** `Promise<string>`
82+
83+
Promise that resolves to the rendered HTML string.
84+
85+
## Behavior
86+
87+
- `renderToStringAsync` waits for server suspense boundaries to settle before resolving.
88+
- Resource data is serialized for client hydration unless scripts are disabled.
89+
- `renderId` namespaces the render output when multiple top-level roots are present.
90+
91+
## Examples
92+
93+
### Basic usage
3594

3695
```ts
37-
const html = await renderToStringAsync(App);
96+
import { renderToStringAsync } from "solid-js/web";
97+
98+
const html = await renderToStringAsync(() => <App />);
3899
```
39100

40-
## Options
101+
## Related
41102

42-
| Name | Type | Description |
43-
| ----------- | -------- | -------------------------------------------------------------------------------------------- |
44-
| `timeoutMs` | `number` | The number of milliseconds to wait for a `<Suspense>` boundary to resolve before timing out. |
45-
| `renderId` | `string` | The id to use for the render. |
46-
| `nonce` | `string` | The nonce to use for the script tag. |
103+
- [`renderToString`](/reference/rendering/render-to-string)
104+
- [`renderToStream`](/reference/rendering/render-to-stream)
Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,89 @@
11
---
22
title: renderToString
33
use_cases: >-
4-
basic ssr, static site generation, synchronous rendering, progressive
5-
hydration, seo pages
4+
synchronous server rendering, HTML generation, progressive hydration output
65
tags:
76
- ssr
87
- rendering
98
- hydration
109
- synchronous
11-
- static
1210
version: "1.0"
1311
description: >-
14-
Generate HTML strings synchronously with renderToString. Perfect for SSR with
15-
progressive hydration and static site generation needs.
12+
Render HTML to a string synchronously on the server.
1613
---
1714

15+
`renderToString` renders HTML to a string synchronously on the server.
16+
17+
## Import
18+
1819
```ts
1920
import { renderToString } from "solid-js/web";
21+
```
22+
23+
## Type
2024

25+
```ts
2126
function renderToString<T>(
2227
fn: () => T,
2328
options?: {
2429
nonce?: string;
2530
renderId?: string;
31+
onError?: (err: any) => void;
2632
}
2733
): string;
2834
```
2935

30-
Renders to a string synchronously.
31-
The function also generates a script tag for progressive hydration.
32-
Options include eventNames to listen to before the page loads and play back on hydration, and nonce to put on the script tag.
36+
## Parameters
37+
38+
### `fn`
39+
40+
- **Type:** `() => T`
41+
42+
Function that returns the root output to render.
43+
44+
### `options`
45+
46+
#### `nonce`
47+
48+
- **Type:** `string`
49+
50+
Nonce applied to inline scripts emitted during rendering.
51+
52+
#### `renderId`
3353

34-
`renderId` is used to namespace renders when having multiple top level roots.
54+
- **Type:** `string`
55+
56+
Identifier used to namespace the render output.
57+
58+
#### `onError`
59+
60+
- **Type:** `(err: any) => void`
61+
62+
Callback invoked when a rendering error is encountered.
63+
64+
## Return value
65+
66+
- **Type:** `string`
67+
68+
Rendered HTML string.
69+
70+
## Behavior
71+
72+
- `renderToString` completes synchronously.
73+
- The output includes hydration data for progressive hydration.
74+
- `renderId` namespaces the render output when multiple top-level roots are present.
75+
76+
## Examples
77+
78+
### Basic usage
3579

3680
```ts
37-
const html = renderToString(App);
81+
import { renderToString } from "solid-js/web";
82+
83+
const html = renderToString(() => <App />);
3884
```
3985

40-
## Options
86+
## Related
4187

42-
| Name | Type | Description |
43-
| ---------- | -------- | ------------------------------------ |
44-
| `nonce` | `string` | The nonce to use for the script tag. |
45-
| `renderId` | `string` | The id to use for the script tag. |
88+
- [`renderToStringAsync`](/reference/rendering/render-to-string-async)
89+
- [`renderToStream`](/reference/rendering/render-to-stream)

0 commit comments

Comments
 (0)