@@ -325,6 +325,10 @@ export function createRequest(
325325 rootContext ,
326326 abortSet ,
327327 ) ;
328+ // TODO
329+ if ( model !== null && typeof model === 'object' ) {
330+ request . writtenObjects . set ( model , rootTask . id ) ;
331+ }
328332 pingedTasks . push ( rootTask ) ;
329333 return request ;
330334}
@@ -787,10 +791,6 @@ function createTask(
787791 abortSet : Set < Task > ,
788792): Task {
789793 const id = request . nextChunkId ++ ;
790- if ( typeof model === 'object' && model !== null ) {
791- // Register this model as having the ID we're about to write.
792- request . writtenObjects . set ( model , id ) ;
793- }
794794 const task : Task = {
795795 id,
796796 status : PENDING ,
@@ -988,7 +988,17 @@ function serializeClientReference(
988988 }
989989}
990990
991- function outlineModel ( request : Request , value : ReactClientValue ) : number {
991+ function outlineModel (
992+ request : Request ,
993+ value :
994+ | ClientReference < any >
995+ | ServerReference< any >
996+ | Iterable< ReactClientValue >
997+ | Array< ReactClientValue >
998+ | Map< ReactClientValue , ReactClientValue >
999+ | Set< ReactClientValue >
1000+ | ReactClientObject,
1001+ ): number {
9921002 request . pendingChunks ++ ;
9931003 const newTask = createTask (
9941004 request ,
@@ -998,6 +1008,7 @@ function outlineModel(request: Request, value: ReactClientValue): number {
9981008 rootContextSnapshot , // Therefore we don't pass any contextual information along.
9991009 request . abortableTasks ,
10001010 ) ;
1011+ request . writtenObjects . set ( value , newTask . id ) ;
10011012 retryTask ( request , newTask ) ;
10021013 return newTask . id ;
10031014}
@@ -1251,9 +1262,18 @@ function renderModelDestructive(
12511262 const writtenObjects = request . writtenObjects ;
12521263 const existingId = writtenObjects . get ( value ) ;
12531264 if ( existingId !== undefined ) {
1254- if ( existingId === - 1 ) {
1265+ if (
1266+ enableServerComponentKeys &&
1267+ ( task . keyPath !== null ||
1268+ task . implicitSlot ||
1269+ task . context !== rootContextSnapshot )
1270+ ) {
1271+ // If we're in some kind of context we can't reuse the result of this render or
1272+ // previous renders of this element. We only reuse elements if they're not wrapped
1273+ // by another Server Component.
1274+ } else if (existingId === -1) {
12551275 // Seen but not yet outlined.
1256- const newId = outlineModel ( request , value ) ;
1276+ const newId = outlineModel ( request , ( value : any ) ) ;
12571277 return serializeByValueID ( newId ) ;
12581278 } else if (modelRoot === value) {
12591279 // This is the ID we're currently emitting so we need to write it
@@ -1359,7 +1379,7 @@ function renderModelDestructive(
13591379 if ( existingId !== undefined ) {
13601380 if ( existingId === - 1 ) {
13611381 // Seen but not yet outlined.
1362- const newId = outlineModel ( request , value ) ;
1382+ const newId = outlineModel ( request , ( value : any ) ) ;
13631383 return serializeByValueID ( newId ) ;
13641384 } else if (modelRoot === value) {
13651385 // This is the ID we're currently emitting so we need to write it
0 commit comments