Skip to content

Commit 264df35

Browse files
committed
Add local template loading to reduce calls to cache
1 parent 5fe374f commit 264df35

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

SSRtest/ModifiedReact.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,7 +2212,8 @@ var ReactDOMServerRenderer = function () {
22122212
restoreProps: Restores actual props to a templatized component
22132213
*/
22142214
const start = {};
2215-
let saveTemplates;
2215+
let saveTemplates;
2216+
let loadedTemplates;
22162217
const restoreProps = (template, props, lookup) => {
22172218
return template.replace(/\{\{[0-9]+\}\}/g, match => props[lookup[match]]);
22182219
};
@@ -2285,28 +2286,35 @@ var ReactDOMServerRenderer = function () {
22852286
// Generate cache key for non-templatized component from its name and props
22862287
cacheKey = child.type.name + JSON.stringify(child.props);
22872288
}
2289+
2290+
let r;
2291+
let restoredTemplate;
22882292

2289-
if (!cache.get(cacheKey)) { // Component not found in cache
2290-
let r;
2291-
2292-
// If templatized component and template hasn't been generated, render a template
2293-
if (!start[cacheKey] && isTemplate) {
2294-
r = this.render(modifiedChild, frame.context, frame.domNamespace);
2295-
start[cacheKey] = { startIndex: out.length, realProps, lookup };
2296-
}
2297-
// Otherwise, render with actual props
2298-
else r = this.render(child, frame.context, frame.domNamespace);
2299-
2300-
// For simple (non-template) caching, save start index of component in output string
2301-
if (!isTemplate) start[cacheKey] = out.length;
2302-
2303-
out += r;
2304-
} else { // Component found in cache
2305-
let r = cache.get(cacheKey);
2306-
let restoredTemplate;
2307-
if (isTemplate) restoredTemplate = restoreProps(r, realProps, lookup);
2308-
out += restoredTemplate ? restoredTemplate : r;
2293+
if (loadedTemplates && loadedTemplates[cacheKey]) { // Component found in loaded templates
2294+
restoredTemplate = restoreProps(loadedTemplates[cacheKey], realProps, lookup);
2295+
} else {
2296+
let reply = cache.get(cacheKey);
2297+
if (!reply) { // Component not found in cache
2298+
// If templatized component and template hasn't been generated, render a template
2299+
if (!start[cacheKey] && isTemplate) {
2300+
r = this.render(modifiedChild, frame.context, frame.domNamespace);
2301+
start[cacheKey] = { startIndex: out.length, realProps, lookup };
2302+
}
2303+
// Otherwise, render with actual props
2304+
else r = this.render(child, frame.context, frame.domNamespace);
2305+
2306+
// For simple (non-template) caching, save start index of component in output string
2307+
if (!isTemplate) start[cacheKey] = out.length;
2308+
} else { // Component found in cache
2309+
r = reply;
2310+
if (isTemplate) {
2311+
restoredTemplate = restoreProps(r, realProps, lookup);
2312+
if (!loadedTemplates) loadedTemplates = {};
2313+
if (!loadedTemplates[cacheKey]) loadedTemplates[cacheKey] = r;
2314+
}
2315+
}
23092316
}
2317+
out += restoredTemplate ? restoredTemplate : r;
23102318
} else {
23112319
// Normal rendering for non-cached components
23122320
out += this.render(child, frame.context, frame.domNamespace);
@@ -2356,7 +2364,7 @@ var ReactDOMServerRenderer = function () {
23562364
saveTemplates.sort((a, b) => a.startIndex - b.startIndex);
23572365
// Rebuild output string with actual props
23582366
saveTemplates.forEach(savedTemplate => {
2359-
out += outCopy.substring(bookmark, savedTemplate.startIndex)
2367+
out += outCopy.substring(bookmark, savedTemplate.startIndex);
23602368
bookmark = savedTemplate.endIndex;
23612369
out += restoreProps(outCopy.slice(savedTemplate.startIndex, savedTemplate.endIndex),
23622370
savedTemplate.realProps, savedTemplate.lookup);

0 commit comments

Comments
 (0)