Skip to content

Commit 603d727

Browse files
committed
createCacheStream function in main exports
1 parent d2bedb0 commit 603d727

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

SSRtest/ModifiedReact.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2730,6 +2730,53 @@ function renderToStaticNodeStream(element, cache, streamingStart, memLife=0) {
27302730
return new ReactMarkupReadableStream(element, true, cache, streamingStart, memLife);
27312731
}
27322732

2733+
function createCacheStream(cache, streamingStart, memLife=0) {
2734+
const bufferedChunks = [];
2735+
return new Transform({
2736+
// transform() is called with each chunk of data
2737+
transform(data, enc, cb) {
2738+
// We store the chunk of data (which is a Buffer) in memory
2739+
bufferedChunks.push(data);
2740+
// Then pass the data unchanged onwards to the next stream
2741+
cb(null, data);
2742+
},
2743+
2744+
// flush() is called when everything is done
2745+
flush(cb) {
2746+
// We concatenate all the buffered chunks of HTML to get the full HTML, then cache it at "key"
2747+
let html = bufferedChunks.join("");
2748+
delete streamingStart.sliceStartCount;
2749+
2750+
for (let component in streamingStart) {
2751+
let tagStack = [];
2752+
let tagStart;
2753+
let tagEnd;
2754+
2755+
do {
2756+
if (!tagStart) tagStart = streamingStart[component];
2757+
else tagStart = (html[tagEnd] === '<') ? tagEnd : html.indexOf('<', tagEnd);
2758+
tagEnd = html.indexOf('>', tagStart) + 1;
2759+
// Skip stack logic for void/self-closing elements and HTML comments
2760+
if (html[tagEnd - 2] !== '/' && html[tagStart + 1] !== '!') {
2761+
// Push opening tags onto stack; pop closing tags off of stack
2762+
if (html[tagStart + 1] !== '/') tagStack.push(html.slice(tagStart, tagEnd));
2763+
else tagStack.pop();
2764+
}
2765+
} while (tagStack.length !== 0);
2766+
// cache component by slicing 'html'
2767+
if (memLife) {
2768+
cache.set(component, html.slice(streamingStart[component], tagEnd), memLife, (err) => {
2769+
if(err) console.log(err)
2770+
});
2771+
} else {
2772+
cache.set(component, html.slice(streamingStart[component], tagEnd));
2773+
}
2774+
}
2775+
cb();
2776+
}
2777+
});
2778+
};
2779+
27332780
class ComponentCache {
27342781
constructor(config = {}) {
27352782

@@ -2765,6 +2812,7 @@ var ReactDOMServerNode = {
27652812
renderToNodeStream: renderToNodeStream,
27662813
renderToStaticNodeStream: renderToStaticNodeStream,
27672814
ComponentCache: ComponentCache,
2815+
createCacheStream: createCacheStream,
27682816
version: ReactVersion
27692817
};
27702818

SSRtest/src/server/cacheStream.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,7 @@ const createCacheStream = (cache, streamingStart, memLife=0) => {
1515

1616
// flush() is called when everything is done
1717
flush(cb) {
18-
// We concatenate all the buffered chunks of HTML to get the full HTML
19-
// then cache it at "key"
20-
21-
// console.log(cache);
22-
// console.log(bufferedChunks.join());
23-
// cache.set(key, Buffer.concat(bufferedChunks));
24-
// console.log("final", streamingStart.finalSliceStart);
25-
//joinedChunks = bufferedChunks.join("");
18+
// We concatenate all the buffered chunks of HTML to get the full HTML, then cache it at "key"
2619
let html = bufferedChunks.join("");
2720
delete streamingStart.sliceStartCount;
2821

@@ -36,7 +29,6 @@ const createCacheStream = (cache, streamingStart, memLife=0) => {
3629
else tagStart = (html[tagEnd] === '<') ? tagEnd : html.indexOf('<', tagEnd);
3730
tagEnd = html.indexOf('>', tagStart) + 1;
3831
// Skip stack logic for void/self-closing elements and HTML comments
39-
// Note: Does not account for tags inside HTML comments
4032
if (html[tagEnd - 2] !== '/' && html[tagStart + 1] !== '!') {
4133
// Push opening tags onto stack; pop closing tags off of stack
4234
if (html[tagStart + 1] !== '/') tagStack.push(html.slice(tagStart, tagEnd));

SSRtest/src/server/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import App from '../shared/App';
99

1010
import createCacheStream from "./cacheStream";
1111
// can pass in max-size, otherwise defaults to 1 million
12-
// const cache = new ReactCC.ComponentCache();
12+
const cache = new ReactCC.ComponentCache();
1313
// import redis from 'redis';
1414
// const cache = redis.createClient();
1515
import memcached from 'memcached';

0 commit comments

Comments
 (0)