Skip to content

Commit af13d79

Browse files
committed
merge with master
2 parents 33d33b5 + b4aaf13 commit af13d79

File tree

4 files changed

+143
-74
lines changed

4 files changed

+143
-74
lines changed

SSRtest/ModifiedReact.js

Lines changed: 48 additions & 4 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

@@ -2806,15 +2806,59 @@ class ComponentCache {
28062806
}
28072807

28082808
}
2809-
2809+
2810+
function renderToNodeStream(compo, cache, res, htmlSt, htmlEn){
2811+
2812+
const htmlStart = htmlSt;
2813+
// '<html><head><title>Page</title></head><body><div id="react-root">';
2814+
2815+
const htmlEnd = htmlEn;
2816+
// "</div></body></html>";
2817+
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+
}
2832+
2833+
function renderToStaticNodeStream(compo, cache, res, htmlSt, htmlEn){
2834+
const htmlStart = htmlSt;
2835+
// '<html><head><title>Page</title></head><body><div id="react-root">';
2836+
2837+
const htmlEnd = htmlEn;
2838+
// "</div></body></html>";
2839+
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+
}
28102854
// Note: when changing this, also consider https://github.com/facebook/react/issues/11526
28112855
var ReactDOMServerNode = {
28122856
renderToString: renderToString,
28132857
renderToStaticMarkup: renderToStaticMarkup,
28142858
renderToNodeStream: renderToNodeStream,
28152859
renderToStaticNodeStream: renderToStaticNodeStream,
28162860
ComponentCache: ComponentCache,
2817-
createCacheStream: createCacheStream,
2861+
// createCacheStream: createCacheStream,
28182862
version: ReactVersion
28192863
};
28202864

SSRtest/src/server/cacheStream.js

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

SSRtest/src/server/index.js

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

1010
// can pass in max-size, otherwise defaults to 1 million
@@ -17,31 +17,31 @@ import App from '../shared/App';
1717
// Force NodeStream
1818
// import createCacheStream from './cacheStream';
1919

20-
const htmlStart =
21-
'<html><head><title>Page</title></head><body><div id="react-root">';
22-
const htmlEnd = "</div></body></html>";
20+
// const htmlStart =
21+
// '<html><head><title>Page</title></head><body><div id="react-root">';
22+
// const htmlEnd = "</div></body></html>";
2323

2424

25-
const streamingStart = {
26-
sliceStartCount: htmlStart.length,
27-
};
25+
// const streamingStart = {
26+
// sliceStartCount: htmlStart.length,
27+
// };
2828
/**
2929
* @param clientStats Parameter passed by hot server middleware
3030
*/
3131
export default ({ clientStats }) => async (req, res) => {
3232
// Need To Come back To If Statement
33-
// if(false){
34-
// const cacheStream = createCacheStream(cache, streamingStart);
33+
if(true){
34+
let htmlStart = '<html><head><title>Page</title></head><body><div id="react-root">';
35+
36+
let htmlEnd = "</div></body></html>";
37+
38+
ReactCC.renderToNodeStream(<App/>, cache, res, htmlStart, htmlEnd);
39+
// const cacheStream = ReactCC.createCacheStream(cache, streamingStart);
3540
// cacheStream.pipe(res);
3641
// cacheStream.write(htmlStart);
3742

38-
ReactCC.renderToNodeStream(<App />, cache, res);
39-
// stream.pipe(cacheStream, { end: false });
40-
// stream.on("end", () => {
41-
// cacheStream.end(htmlEnd);
42-
// });
43-
// }
44-
// else if (true){
43+
}
44+
else if (false){
4545
const app = <App />;
4646
const start_cached = process.hrtime();
4747

@@ -61,6 +61,6 @@ export default ({ clientStats }) => async (req, res) => {
6161
styles,
6262
cssHash
6363
});
64-
// }
64+
}
6565

66-
};
66+
};

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;

0 commit comments

Comments
 (0)