Skip to content

Commit

Permalink
Merge pull request #62 from Ibadichan/update-react-streaming-ssr-exam…
Browse files Browse the repository at this point in the history
…ple-2

Removed multistream and readableString usage from react SSR
  • Loading branch information
theKashey authored Mar 29, 2024
2 parents 5125640 + e709eb6 commit 44ac5d8
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,8 @@ app.use('*', async (req, res) => {
// entry-server.js
import React from 'react';
import { renderToPipeableStream } from 'react-dom/server';
import MultiStream from 'multistream';
import { Readable } from 'node:stream';
import App from './App';

// small utility for "readable" streams
const readableString = string => {
const s = new Readable();
s.push(string);
s.push(null);
s._read = () => true;
return s;
};

const ABORT_DELAY = 10000;

async function renderApp({ res, styledStream }) {
Expand All @@ -265,19 +254,15 @@ async function renderApp({ res, styledStream }) {

// allow client to start loading js bundle
res.write(`<!DOCTYPE html><html><head><script defer src="client.js"></script></head><body><div id="root">`);

const endStream = readableString('</div></body></html>');

// concatenate all streams together
const streams = [
styledStream, // the main content
endStream, // closing tags
];

new MultiStream(streams).pipe(res);

styledStream.pipe(res, { end: false });

// start by piping react and styled transform stream
pipe(styledStream);

styledStream.on('end', () => {
res.end('</div></body></html>');
});
},
onError(error) {
didError = true;
Expand Down

0 comments on commit 44ac5d8

Please sign in to comment.