You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You may find that you need to render components that are dynamically loaded, like when using `Suspense` and `lazy` to facilitate code splitting (along with some other use cases). The async renderer will await the resolution of promises, allowing you to fully construct your HTML string:
47
+
Awaits the resolution of promises before returning the complete HTML string. This is particularly useful when utilizing suspense for lazy-loaded components or data fetching.
The above is a very typical setup for a Preact application that uses code splitting, with no changes necessary to make use of server-side rendering.
73
+
## HTML Streams
74
+
75
+
Streaming is a method of rendering that allows you to send parts of your Preact application to the client as they are ready rather than waiting for the entire render to complete.
69
76
70
-
To render this, we will deviate slightly from the basic usage example and use the `renderToStringAsync` export to render our application:
77
+
### renderToPipeableStream
78
+
79
+
`renderToPipeableStream` is a streaming method that utilizes [Node.js Streams](https://nodejs.org/api/stream.html) to render your application. If you are not using Node, you should look to [renderToReadableStream](#rendertoreadablestream) instead.
// Abandon and switch to client rendering if enough time passes.
101
+
setTimeout(abort, 2000);
102
+
}
103
+
```
75
104
76
-
constmain=async () => {
77
-
// Rendering of lazy components
78
-
consthtml=awaitrenderToStringAsync(<Main />);
105
+
### renderToReadableStream
79
106
80
-
console.log(html);
81
-
// <h1>Home page</h1>
82
-
};
107
+
`renderToReadableStream` is another streaming method and similar to `renderToPipeableStream`, but designed for use in environments that support standardized [Web Streams](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API) instead.
// Request handler syntax and form will vary across frameworks
113
+
functionhandler(req, res) {
114
+
conststream=renderToReadableStream(<App />);
115
+
116
+
returnnewResponse(stream, {
117
+
headers: {
118
+
'Content-Type':'text/html',
119
+
},
120
+
});
121
+
}
88
122
```
89
123
90
-
## Shallow Rendering
124
+
## Customize Renderer Output
91
125
92
-
For some purposes it's often preferable to not render the whole tree, but only one level. For that we have a shallow renderer which will print child components by name, instead of their return value.
126
+
We offer a number of options through the `/jsx` module to customize the output of the renderer for a handful of popular use cases.
127
+
128
+
### JSX Mode
129
+
130
+
The JSX rendering mode is especially useful if you're doing any kind of snapshot testing. It renders the output as if it was written in JSX.
If you need to get the rendered output in a more human friendly way, we've got you covered! By passing the `pretty` option, we'll preserve whitespace and indent the output as expected.
The JSX rendering mode is especially useful if you're doing any kind of snapshot testing. It renders the output as if it was written in JSX.
161
+
For some purposes it's often preferable to not render the whole tree, but only one level. For that we have a shallow renderer which will print child components by name, instead of their return value.
0 commit comments