Skip to content

Commit cb211cd

Browse files
committed
added htmlStart and htmlEnd as parameters to renderToNodeStream and renderToStaticNodeStream functions
1 parent 47515ec commit cb211cd

File tree

4 files changed

+123
-96
lines changed

4 files changed

+123
-96
lines changed

SSRtest/ModifiedReact.js

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,7 @@ var ReactMarkupReadableStream = function (_Readable) {
27132713
*/
27142714

27152715

2716-
function renderToNodeStream(element, cache, streamingStart, memLife=0) {
2716+
function originalRenderToNodeStream(element, cache, streamingStart, memLife=0) {
27172717
return new ReactMarkupReadableStream(
27182718
element,
27192719
false,
@@ -2728,7 +2728,7 @@ function renderToNodeStream(element, cache, streamingStart, memLife=0) {
27282728
* such as data-react-id that React uses internally.
27292729
* See https://reactjs.org/docs/react-dom-stream.html#rendertostaticnodestream
27302730
*/
2731-
function renderToStaticNodeStream(element, cache, streamingStart, memLife=0) {
2731+
function originalRenderToStaticNodeStream(element, cache, streamingStart, memLife=0) {
27322732
return new ReactMarkupReadableStream(element, true, cache, streamingStart, memLife);
27332733
}
27342734

@@ -2807,28 +2807,50 @@ class ComponentCache {
28072807

28082808
}
28092809

2810-
// function renderToNodeStream(compo, cache, res){
2810+
function renderToNodeStream(compo, cache, res, htmlSt, htmlEn){
28112811

2812-
// const htmlStart =
2813-
// '<html><head><title>Page</title></head><body><div id="react-root">';
2812+
const htmlStart = htmlSt;
2813+
// '<html><head><title>Page</title></head><body><div id="react-root">';
28142814

2815-
// const htmlEnd = "</div></body></html>";
2815+
const htmlEnd = htmlEn;
2816+
// "</div></body></html>";
28162817

2817-
// const streamingStart = {
2818-
// sliceStartCount: htmlStart.length,
2819-
// }
2818+
const streamingStart = {
2819+
sliceStartCount: htmlStart.length,
2820+
}
2821+
2822+
const cacheStream = createCacheStream(cache, streamingStart);
2823+
cacheStream.pipe(res);
2824+
cacheStream.write(htmlStart);
2825+
2826+
const stream = originalRenderToNodeStream(compo, cache, streamingStart);
2827+
stream.pipe(cacheStream, { end: false });
2828+
stream.on("end", () => {
2829+
cacheStream.end(htmlEnd);
2830+
});
2831+
}
28202832

2821-
// const cacheStream = createCacheStream(cache, streamingStart);
2822-
// cacheStream.pipe(res);
2823-
// cacheStream.write(htmlStart);
2833+
function renderToStaticNodeStream(compo, cache, res, htmlSt, htmlEn){
2834+
const htmlStart = htmlSt;
2835+
// '<html><head><title>Page</title></head><body><div id="react-root">';
28242836

2825-
// const stream = originalRenderToNodeStream(compo, cache, streamingStart);
2826-
// stream.pipe(cacheStream, { end: false });
2827-
// stream.on("end", () => {
2828-
// cacheStream.end(htmlEnd);
2829-
// });
2837+
const htmlEnd = htmlEn;
2838+
// "</div></body></html>";
28302839

2831-
// }
2840+
const streamingStart = {
2841+
sliceStartCount: htmlStart.length,
2842+
}
2843+
2844+
const cacheStream = createCacheStream(cache, streamingStart);
2845+
cacheStream.pipe(res);
2846+
cacheStream.write(htmlStart);
2847+
2848+
const stream = originalRenderToStaticNodeStream(compo, cache, streamingStart);
2849+
stream.pipe(cacheStream, { end: false });
2850+
stream.on("end", () => {
2851+
cacheStream.end(htmlEnd);
2852+
});
2853+
}
28322854
// Note: when changing this, also consider https://github.com/facebook/react/issues/11526
28332855
var ReactDOMServerNode = {
28342856
renderToString: renderToString,

SSRtest/src/server/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import React from 'react';
44
import ReactCC from '../../ModifiedReact';
55
import { flushChunkNames } from 'react-universal-component/server';
66
import flushChunks from 'webpack-flush-chunks';
7-
import zNodeStream from "./zNodeStream.js";
7+
// import nodeStream from "./nodeStream.js";
88
import App from '../shared/App';
99

1010
// can pass in max-size, otherwise defaults to 1 million
@@ -30,7 +30,11 @@ const cache = new ReactCC.ComponentCache();
3030
export default ({ clientStats }) => async (req, res) => {
3131
// Need To Come back To If Statement
3232
if(true){
33-
zNodeStream(<App/>, false, cache, res);
33+
let htmlStart = '<html><head><title>Page</title></head><body><div id="react-root">';
34+
35+
let htmlEnd = "</div></body></html>";
36+
37+
ReactCC.renderToNodeStream(<App/>, cache, res, htmlStart, htmlEnd);
3438
// const cacheStream = ReactCC.createCacheStream(cache, streamingStart);
3539
// cacheStream.pipe(res);
3640
// cacheStream.write(htmlStart);

SSRtest/src/server/nodeStream.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// import { Transform } from "stream";
2+
// // import { create } from "domain";
3+
// // import { lstat } from "fs";
4+
// import { renderToNodeStream, renderToStaticNodeStream } from "../../ModifiedReact.js";
5+
6+
// const createCacheStream = (cache, streamingStart, memLife=0) => {
7+
// const bufferedChunks = [];
8+
// return new Transform({
9+
// // transform() is called with each chunk of data
10+
// transform(data, enc, cb) {
11+
// // We store the chunk of data (which is a Buffer) in memory
12+
// bufferedChunks.push(data);
13+
// // Then pass the data unchanged onwards to the next stream
14+
// cb(null, data);
15+
// },
16+
17+
// // flush() is called when everything is done
18+
// flush(cb) {
19+
// // We concatenate all the buffered chunks of HTML to get the full HTML, then cache it at "key"
20+
// let html = bufferedChunks.join("");
21+
// delete streamingStart.sliceStartCount;
22+
23+
// for (let component in streamingStart) {
24+
// let tagStack = [];
25+
// let tagStart;
26+
// let tagEnd;
27+
28+
// do {
29+
// if (!tagStart) tagStart = streamingStart[component];
30+
// else tagStart = (html[tagEnd] === '<') ? tagEnd : html.indexOf('<', tagEnd);
31+
// tagEnd = html.indexOf('>', tagStart) + 1;
32+
// // Skip stack logic for void/self-closing elements and HTML comments
33+
// if (html[tagEnd - 2] !== '/' && html[tagStart + 1] !== '!') {
34+
// // Push opening tags onto stack; pop closing tags off of stack
35+
// if (html[tagStart + 1] !== '/') tagStack.push(html.slice(tagStart, tagEnd));
36+
// else tagStack.pop();
37+
// }
38+
// } while (tagStack.length !== 0);
39+
// // cache component by slicing 'html'
40+
// if (memLife) {
41+
// cache.set(component, html.slice(streamingStart[component], tagEnd), memLife, (err) => {
42+
// if(err) console.log(err)
43+
// });
44+
// } else {
45+
// cache.set(component, html.slice(streamingStart[component], tagEnd));
46+
// }
47+
// }
48+
// cb();
49+
// }
50+
// });
51+
// };
52+
53+
// function nodeStream(compo, staticMarkup, cache, res, htmlSt, htmlEn){
54+
55+
// const htmlStart = htmlSt;
56+
// // '<html><head><title>Page</title></head><body><div id="react-root">';
57+
58+
// const htmlEnd = htmlEn;
59+
// // "</div></body></html>";
60+
61+
// const streamingStart = {
62+
// sliceStartCount: htmlStart.length,
63+
// }
64+
65+
// const cacheStream = createCacheStream(cache, streamingStart);
66+
// cacheStream.pipe(res);
67+
// cacheStream.write(htmlStart);
68+
69+
// const stream = staticMarkup? renderToStaticNodeStream(compo, cache, streamingStart) : renderToNodeStream(compo, cache, streamingStart);
70+
// stream.pipe(cacheStream, { end: false });
71+
// stream.on("end", () => {
72+
// cacheStream.end(htmlEnd);
73+
// });
74+
75+
// }
76+
77+
// export default nodeStream;

SSRtest/src/server/zNodeStream.js

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)