@@ -2204,13 +2204,22 @@ var ReactDOMServerRenderer = function () {
2204
2204
// TODO: type this more strictly:
2205
2205
2206
2206
2207
- ReactDOMServerRenderer . prototype . read = function read ( bytes , cache ) {
2208
- let saveTemplates ;
2209
- function restoreProps ( template , props , lookup ) {
2207
+ ReactDOMServerRenderer . prototype . read = function read ( bytes , cache ) {
2208
+ /*
2209
+ --- Component caching variables ---
2210
+ start: Tracks start index in output string and templatization data for cached components
2211
+ saveTemplates: Tracks templatized components so props can be restored in output string
2212
+ restoreProps: Restores actual props to a templatized component
2213
+ */
2214
+ const start = { } ;
2215
+ let saveTemplates ;
2216
+ const restoreProps = ( template , props , lookup ) => {
2210
2217
return template . replace ( / \{ \{ [ 0 - 9 ] + \} \} / g, match => props [ lookup [ match ] ] ) ;
2211
- }
2212
-
2213
- const start = { } ;
2218
+ } ;
2219
+
2220
+ /*
2221
+ --- Begin React 16 source code with addition of component caching logic ---
2222
+ */
2214
2223
if ( this . exhausted ) {
2215
2224
return null ;
2216
2225
}
@@ -2238,67 +2247,67 @@ var ReactDOMServerRenderer = function () {
2238
2247
{
2239
2248
setCurrentDebugStack ( this . stack ) ;
2240
2249
}
2241
- // IF THE CHILD HAS A CACHEKEY PROPERTY ON IT
2250
+
2251
+ /*
2252
+ --- Component caching logic ---
2253
+ */
2242
2254
if ( child . props && child . props . cache ) {
2243
2255
let cacheKey ;
2244
- let isTemplate ;
2245
- if ( child . props . templatized ) isTemplate = true ;
2246
- /* Lookup object for template post-processing where keys are placeholders
2247
- (e.g. {{0}}) and values are templatized prop names */
2248
- let lookup = { } ;
2256
+ let isTemplate = child . props . templatized ? true : false ;
2257
+ let lookup ;
2249
2258
let modifiedChild ;
2250
2259
let realProps ;
2251
2260
2252
2261
if ( isTemplate ) {
2253
- // First, find cache key from templatized props
2254
- // Swap out templatized props in props object with placeholder for use in cache key
2262
+ // Generate templatized version of props to generate appropriate cache key
2255
2263
let cacheProps = Object . assign ( { } , child . props ) ;
2256
2264
let templatizedProps = child . props . templatized ;
2265
+ lookup = { } ;
2266
+
2257
2267
if ( typeof templatizedProps === 'string' ) templatizedProps = [ templatizedProps ] ;
2258
- for ( let i = 0 ; i < templatizedProps . length ; i ++ ) {
2259
- const placeholder = `{{${ i } }}` ;
2260
- cacheProps [ templatizedProps [ i ] ] = placeholder ;
2261
- lookup [ placeholder ] = templatizedProps [ i ] ; // Move down to next if statement? (Extra work/space if not generating template)
2262
- }
2268
+
2269
+ // Generate template placeholders and lookup object for referencing prop names from placeholders
2270
+ templatizedProps . forEach ( ( templatizedProp , index ) => {
2271
+ const placeholder = `{{${ index } }}` ;
2272
+ cacheProps [ templatizedProp ] = placeholder ;
2273
+ lookup [ placeholder ] = templatizedProp ; // Move down to next if statement? (Extra work/space if not generating template)
2274
+ } ) ;
2275
+
2263
2276
cacheKey = child . type . name + JSON . stringify ( cacheProps ) ;
2264
2277
2265
- // Check whether the component template is being generated
2278
+ // Generate modified child with placeholder props to render template
2266
2279
if ( ! start [ cacheKey ] ) {
2267
- // Variables for templatization
2268
2280
modifiedChild = Object . assign ( { } , child ) ;
2269
2281
modifiedChild . props = cacheProps ;
2270
2282
realProps = child . props ;
2271
2283
}
2272
2284
} else {
2273
- // Not a template
2285
+ // Generate cache key for non-templatized component from its name and props
2274
2286
cacheKey = child . type . name + JSON . stringify ( child . props ) ;
2275
2287
}
2276
2288
2277
- // IF NOT FOUND IN CACHE
2278
- if ( ! cache . storage . get ( cacheKey ) ) {
2289
+ if ( ! cache . storage . get ( cacheKey ) ) { // Component not found in cache
2279
2290
let r ;
2280
2291
2281
2292
// If templatized component and template hasn't been generated, render a template
2282
2293
if ( ! start [ cacheKey ] && isTemplate ) {
2283
2294
r = this . render ( modifiedChild , frame . context , frame . domNamespace ) ;
2284
2295
start [ cacheKey ] = { startIndex : out . length , realProps, lookup } ;
2285
2296
}
2286
- // Otherwise, render as normal with props for simple caching or if template was generated earlier in this render
2297
+ // Otherwise, render with actual props
2287
2298
else r = this . render ( child , frame . context , frame . domNamespace ) ;
2288
2299
2289
- // For simple component caching, bookmark the start index of component in output string
2300
+ // For simple (non-template) caching, save start index of component in output string
2290
2301
if ( ! isTemplate ) start [ cacheKey ] = out . length ;
2291
- // Note: no bookmark needed if template was generated earlier in the same render
2302
+
2292
2303
out += r ;
2293
- // IF FOUND IN CACHE
2294
- } else {
2295
- // Cached component found in storage
2304
+ } else { // Component found in cache
2296
2305
let r = cache . storage . get ( cacheKey ) ;
2297
2306
let restoredTemplate ;
2298
2307
if ( isTemplate ) restoredTemplate = restoreProps ( r , realProps , lookup ) ;
2299
2308
out += restoredTemplate ? restoredTemplate : r ;
2300
2309
}
2301
- } else {
2310
+ } else { // Normal rendering for non-cached components
2302
2311
out += this . render ( child , frame . context , frame . domNamespace ) ;
2303
2312
}
2304
2313
{
@@ -2307,6 +2316,9 @@ var ReactDOMServerRenderer = function () {
2307
2316
}
2308
2317
}
2309
2318
2319
+ /*
2320
+ --- After initial render of cacheable components, recover from output string and store in cache ---
2321
+ */
2310
2322
for ( let component in start ) {
2311
2323
let tagStack = [ ] ;
2312
2324
let tagStart ;
@@ -2325,7 +2337,7 @@ var ReactDOMServerRenderer = function () {
2325
2337
else tagStack . pop ( ) ;
2326
2338
}
2327
2339
} while ( tagStack . length !== 0 ) ;
2328
- // cache component by slicing 'out'
2340
+ // Cache component by slicing 'out'
2329
2341
const cachedComponent = out . slice ( componentStart , tagEnd ) ;
2330
2342
if ( typeof start [ component ] === 'object' ) {
2331
2343
if ( ! saveTemplates ) saveTemplates = [ ] ;
@@ -2335,27 +2347,21 @@ var ReactDOMServerRenderer = function () {
2335
2347
cache . storage . set ( component , cachedComponent ) ;
2336
2348
}
2337
2349
2338
- console . log ( 'HERE IS OUR CURRENT CACHE' , cache . storage ) ;
2339
- // RESTORE PROPS FOR ALL TEMPLATIZED COMPONENTS ONLY AFTER CACHING
2350
+ // After caching all cacheable components, restore props to templates
2340
2351
if ( saveTemplates ) {
2341
2352
let outCopy = out ;
2342
2353
out = '' ;
2343
2354
let bookmark = 0 ;
2344
2355
saveTemplates . sort ( ( a , b ) => a . startIndex - b . startIndex ) ;
2345
- console . log ( 'SAVED THESE TEMPLATES' , saveTemplates )
2346
- for ( let i = 0 ; i < saveTemplates . length ; i ++ ) {
2347
- out += outCopy . substring ( bookmark , saveTemplates [ i ] . startIndex )
2348
- bookmark = saveTemplates [ i ] . endIndex ;
2349
- let restoredTemplate = restoreProps ( outCopy . slice ( saveTemplates [ i ] . startIndex , saveTemplates [ i ] . endIndex ) ,
2350
- saveTemplates [ i ] . realProps , saveTemplates [ i ] . lookup ) ;
2351
- console . log ( 'RESTORED TEMPLATE' , restoredTemplate ) ;
2352
- out += restoredTemplate ;
2353
- }
2356
+ // Rebuild output string with actual props
2357
+ saveTemplates . forEach ( savedTemplate => {
2358
+ out += outCopy . substring ( bookmark , savedTemplate . startIndex )
2359
+ bookmark = savedTemplate . endIndex ;
2360
+ out += restoreProps ( outCopy . slice ( savedTemplate . startIndex , savedTemplate . endIndex ) ,
2361
+ savedTemplate . realProps , savedTemplate . lookup ) ;
2362
+ } ) ;
2354
2363
out += outCopy . substring ( bookmark , outCopy . length ) ;
2355
2364
}
2356
- // let restoredTemplate = restoreProps(cachedComponent, start[component].realProps, start[component].lookup);
2357
- // THIS IS NO GOOD. IT MUTATES THE OUTPUT AND CHANGES EVERYTHING SO THE START FOR EVERYTHING IS WAY OFF
2358
-
2359
2365
return out ;
2360
2366
} ;
2361
2367
0 commit comments