Skip to content

Commit dadcf7e

Browse files
committed
update template to avoid repeated cache calls
2 parents f3a7194 + ad9d4d1 commit dadcf7e

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Insert description and implementation here
8686
ReactCC gives you access to all four of React 16's server-side rendering methods, as well as additional functionality. ReactCC methods are described below.
8787

8888
### ComponentCache
89-
- `size`: (Optional) An integer representing the maximum size (in characters) of the cache. Defaults to 1 million.
89+
- `size`: (*Optional*) An integer representing the maximum size (in characters) of the cache. Defaults to 1 million.
9090

9191
**Example:**
9292
```javascript
@@ -96,7 +96,7 @@ const cache = new ReactCC.ComponentCache();
9696
### renderToString
9797
- `component`: The React component being rendered.
9898
- `cache`: The component cache
99-
- `memLife`: (Only if using Memcached) A number representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.
99+
- `memLife`: (*Only if using Memcached*) A number representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.
100100

101101
**Example:**
102102
```javascript
@@ -106,7 +106,7 @@ ReactCC.renderToString(<App />, cache);
106106
### renderToStaticMarkup
107107
- `component`: The React component being rendered.
108108
- `cache`: The component cache
109-
- `memLife`: (Only if using Memcached) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.
109+
- `memLife`: (*Only if using Memcached*) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.
110110

111111
**Example:**
112112
```javascript
@@ -116,7 +116,7 @@ ReactCC.renderToStaticMarkup(<App />, cache);
116116
### renderToNodeStream
117117
- `component`: The React component being rendered.
118118
- `cache`: The component cache
119-
- `memLife`: (Only if using Memcached) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.
119+
- `memLife`: (*Only if using Memcached*) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.
120120

121121
**Example:**
122122
```javascript
@@ -126,7 +126,7 @@ ReactCC.renderToNodeStream(<App />, cache);
126126
### renderToStaticNodeStream
127127
- `component`: The React component being rendered.
128128
- `cache`: The component cache
129-
- `memLife`: (Only if using Memcached) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.
129+
- `memLife`: (*Only if using Memcached*) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.
130130

131131
**Example:**
132132
```javascript

SSRtest/ModifiedReact.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,7 +2216,8 @@ var ReactDOMServerRenderer = function () {
22162216
getAsync: Convert asynchronous get method into a promise
22172217
*/
22182218
const start = {};
2219-
let saveTemplates;
2219+
let saveTemplates = [];
2220+
let loadedTemplates = {};
22202221
const restoreProps = (template, props, lookup) => {
22212222
return template.replace(/\{\{[0-9]+\}\}/g, match => props[lookup[match]]);
22222223
};
@@ -2292,28 +2293,32 @@ var ReactDOMServerRenderer = function () {
22922293
if(memLife){
22932294
cacheKey = cacheKey.replace(/\s+/g, '|')
22942295
}
2296+
let r;
2297+
let restoredTemplate;
22952298

2296-
const reply = await getAsync(cacheKey);
2297-
if (!reply) { // Component not found in cache
2298-
let r;
2299-
2300-
// If templatized component and template hasn't been generated, render a template
2301-
if (!start[cacheKey] && isTemplate) {
2302-
r = this.render(modifiedChild, frame.context, frame.domNamespace);
2303-
start[cacheKey] = { startIndex: out.length, realProps, lookup };
2304-
}
2305-
// Otherwise, render with actual props
2306-
else r = this.render(child, frame.context, frame.domNamespace);
2307-
2308-
// For simple (non-template) caching, save start index of component in output string
2309-
if (!isTemplate) start[cacheKey] = out.length;
2310-
2311-
out += r;
2312-
} else { // Component found in cache
2313-
let restoredTemplate;
2314-
if (isTemplate) restoredTemplate = restoreProps(reply, realProps, lookup);
2315-
out += restoredTemplate ? restoredTemplate : reply;
2299+
if (loadedTemplates[cacheKey]) { // Component found in loaded templates
2300+
restoredTemplate = restoreProps(loadedTemplates[cacheKey], realProps, lookup);
2301+
} else {
2302+
const reply = await getAsync(cacheKey);
2303+
if (!reply) { // Component not found in cache
2304+
// If templatized component and template hasn't been generated, render a template
2305+
if (!start[cacheKey] && isTemplate) {
2306+
r = this.render(modifiedChild, frame.context, frame.domNamespace);
2307+
start[cacheKey] = { startIndex: out.length, realProps, lookup };
2308+
}
2309+
// Otherwise, render with actual props
2310+
else r = this.render(child, frame.context, frame.domNamespace);
2311+
2312+
// For simple (non-template) caching, save start index of component in output string
2313+
if (!isTemplate) start[cacheKey] = out.length;
2314+
} else { // Component found in cache
2315+
if (isTemplate) {
2316+
restoredTemplate = restoreProps(reply, realProps, lookup);
2317+
loadedTemplates[cacheKey] = reply;
2318+
}
2319+
}
23162320
}
2321+
out += restoredTemplate ? restoredTemplate : r;
23172322
} else {
23182323
// Normal rendering for non-cached components
23192324
out += this.render(child, frame.context, frame.domNamespace);
@@ -2348,7 +2353,6 @@ var ReactDOMServerRenderer = function () {
23482353
// Cache component by slicing 'out'
23492354
const cachedComponent = out.slice(componentStart, tagEnd);
23502355
if (typeof start[component] === 'object') {
2351-
if (!saveTemplates) saveTemplates = [];
23522356
saveTemplates.push(start[component]);
23532357
start[component].endIndex = tagEnd;
23542358
}
@@ -2369,7 +2373,7 @@ var ReactDOMServerRenderer = function () {
23692373
saveTemplates.sort((a, b) => a.startIndex - b.startIndex);
23702374
// Rebuild output string with actual props
23712375
saveTemplates.forEach(savedTemplate => {
2372-
out += outCopy.substring(bookmark, savedTemplate.startIndex)
2376+
out += outCopy.substring(bookmark, savedTemplate.startIndex);
23732377
bookmark = savedTemplate.endIndex;
23742378
out += restoreProps(outCopy.slice(savedTemplate.startIndex, savedTemplate.endIndex),
23752379
savedTemplate.realProps, savedTemplate.lookup);

SSRtest/src/server/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import flushChunks from 'webpack-flush-chunks';
88
import App from '../shared/App';
99

1010
// can pass in max-size, otherwise defaults to 1 million
11-
// const cache = new ReactCC.ComponentCache();
11+
const cache = new ReactCC.ComponentCache();
1212
// import redis from 'redis';
1313
// const cache = redis.createClient();
14-
import memcached from 'memcached';
15-
const cache = new memcached('localhost:11211');
14+
// import memcached from 'memcached';
15+
// const cache = new memcached('localhost:11211');
1616

1717
/**
1818
* @param clientStats Parameter passed by hot server middleware
@@ -21,7 +21,7 @@ export default ({ clientStats }) => async (req, res) => {
2121
const app = <App />;
2222
const start_cached = process.hrtime();
2323

24-
const appString = await ReactCC.renderToString(app, cache, 20);
24+
const appString = await ReactCC.renderToString(app, cache);
2525
const end_cached = process.hrtime(start_cached);
2626
console.info(
2727
"Cached render time: %ds %dms",

0 commit comments

Comments
 (0)