Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: support renderToPipeableStream in documentation #234

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const { helmet } = helmetContext;

## Streams

This package only works with streaming if your `<head>` data is output outside of `renderToNodeStream()`.
This package only works with streaming if your `<head>` data is output outside of `renderToPipeableStream()`.
This is possible if your data hydration method already parses your React tree. Example:

```javascript
Expand Down Expand Up @@ -100,23 +100,40 @@ const [header, footer] = template({
helmet: helmetContext.helmet,
});

res.status(200);
res.write(header);
renderToNodeStream(app)
.pipe(
through(
function write(data) {
this.queue(data);
},
function end() {
this.queue(footer);
this.queue(null);
const { pipe } = renderToPipeableStream(appElement, {
onShellError() {
console.error("Shell rendering error");
},
onAllReady() {
const transformStream = new Transform({
transform(chunk, encoding, callback) {
res.write(chunk, encoding);
callback();
},
});
if (helmetContext) {
res.write(helmetContext.title.toString());
res.write(helmetContext.priority.toString());
res.write(helmetContext.meta.toString());
res.write(helmetContext.link.toString());
res.write(helmetContext.script.toString());
}
)
)
.pipe(res);

pipe(transformStream);

transformStream.on("finish", () => {
res.write(`</body></html>`);
});
},
onError(error) {
didError = true;
console.error("Rendering error:", error);
},
});
```

You can find a complete exemple project here: https://github.com/aqora-io/relay-vite-ssr-example

## Usage in Jest
While testing in using jest, if there is a need to emulate SSR, the following string is required to have the test behave the way they are expected to.

Expand Down