diff --git a/packages/react-server/src/ReactFlightServer.js b/packages/react-server/src/ReactFlightServer.js index 1bc57ffd0a..bdaa39b793 100644 --- a/packages/react-server/src/ReactFlightServer.js +++ b/packages/react-server/src/ReactFlightServer.js @@ -563,6 +563,13 @@ type Task = { interface Reference {} +// An object's reference may include a path within a row. We also keep the +// containing row's ID so that reusing the object can reuse its ledger entries. +type WrittenObjectEntry = { + id: number, + reference: string, +}; + type ReactClientReference = Reference & ReactClientValue; type DeferredDebugStore = { @@ -614,7 +621,7 @@ export type Request = { writtenSymbols: Map, writtenClientReferences: Map, writtenServerReferences: Map, number>, - writtenObjects: WeakMap, + writtenObjects: WeakMap, writtenImportStrings: Map, // The combined length of the keys in writtenImportStrings. writtenImportStringsSize: number, @@ -2224,7 +2231,7 @@ function renderClientElement( debugStack, ); // We also store this in the main dedupe set so that it can be referenced by inline React Elements. - request.writtenObjects.set(debugStack, serializeByValueID(id)); + writeToDedupeMap(request, debugStack, id, serializeByValueID(id)); } } const element = __DEV__ @@ -2898,7 +2905,7 @@ function createTaskWithID( // If we're in some kind of context we can't necessarily reuse this object depending // what parent components are used. } else { - request.writtenObjects.set(model, serializeByValueID(id)); + writeToDedupeMap(request, model, id, serializeByValueID(id)); } } const task: Task = { @@ -3057,6 +3064,17 @@ function resolveModel( return resolved; } +function writeToDedupeMap( + request: Request, + value: Reference, + id: number, + reference: string, +): WrittenObjectEntry { + const entry = {id, reference}; + request.writtenObjects.set(value, entry); + return entry; +} + function serializeByValueID(id: number): string { return '$' + id.toString(16); } @@ -3812,15 +3830,15 @@ function renderModelDestructive( if (typeof value === 'object') { switch ((value as any).$$typeof) { case REACT_ELEMENT_TYPE: { - let elementReference = null; + let elementEntry = null; const writtenObjects = request.writtenObjects; if (task.keyPath !== null || task.implicitSlot) { // If we're in some kind of context we can't reuse the result of this render or // previous renders of this element. We only reuse elements if they're not wrapped // by another Server Component. } else { - const existingReference = writtenObjects.get(value); - if (existingReference !== undefined) { + const existingEntry = writtenObjects.get(value); + if (existingEntry !== undefined) { if (modelRoot === value) { // This is the ID we're currently emitting so we need to write it // once but if we discover it again, we refer to it by id. @@ -3833,16 +3851,20 @@ function renderModelDestructive( // detect whether this already was emitted and synchronously available. In that // case we can refer to it synchronously and only make it lazy otherwise. // We currently don't have a data structure that lets us see that though. - return existingReference; + return existingEntry.reference; } } else if (parentPropertyName.indexOf(':') === -1) { // TODO: If the property name contains a colon, we don't dedupe. Escape instead. - const parentReference = writtenObjects.get(parent); - if (parentReference !== undefined) { + const parentEntry = writtenObjects.get(parent); + if (parentEntry !== undefined) { // If the parent has a reference, we can refer to this object indirectly // through the property name inside that parent. - elementReference = parentReference + ':' + parentPropertyName; - writtenObjects.set(value, elementReference); + elementEntry = writeToDedupeMap( + request, + value, + parentEntry.id, + parentEntry.reference + ':' + parentPropertyName, + ); } } } @@ -3921,12 +3943,12 @@ function renderModelDestructive( if ( typeof newChild === 'object' && newChild !== null && - elementReference !== null + elementEntry !== null ) { // If this element renders another object, we can now refer to that object through // the same location as this element. if (!writtenObjects.has(newChild)) { - writtenObjects.set(newChild, elementReference); + writtenObjects.set(newChild, elementEntry); } } return newChild; @@ -4025,14 +4047,14 @@ function renderModelDestructive( } const writtenObjects = request.writtenObjects; - const existingReference = writtenObjects.get(value); + const existingEntry = writtenObjects.get(value); // $FlowFixMe[method-unbinding] if (typeof value.then === 'function') { // A weak-pending thenable may never emit, so its reference is marked // on the wire ($w instead of $@). That way the client knows to leave // it forever pending, instead of erroring it, if the stream closes // first. - if (existingReference !== undefined) { + if (existingEntry !== undefined) { if (task.keyPath !== null || task.implicitSlot) { // If we're in some kind of context we can't reuse the result of this render or // previous renders of this element. We only reuse Promises if they're not wrapped @@ -4048,7 +4070,7 @@ function renderModelDestructive( modelRoot = null; } else { // We've seen this promise before, so we can just refer to the same result. - return existingReference; + return existingEntry.reference; } } // We assume that any object with a .then property is a "Thenable" type, @@ -4058,16 +4080,16 @@ function renderModelDestructive( enableFlightWeakThenables && (value as any).status === 'pending_weak' ? serializeWeakPromiseID(promiseId) : serializePromiseID(promiseId); - writtenObjects.set(value, promiseReference); + writeToDedupeMap(request, value, promiseId, promiseReference); return promiseReference; } - if (existingReference !== undefined) { + if (existingEntry !== undefined) { if (modelRoot === value) { - if (existingReference !== serializeByValueID(task.id)) { + if (existingEntry.reference !== serializeByValueID(task.id)) { // Turns out that we already have this root at a different reference. // Use that after all. - return existingReference; + return existingEntry.reference; } // This is the ID we're currently emitting so we need to write it // once but if we discover it again, we refer to it by id. @@ -4075,12 +4097,12 @@ function renderModelDestructive( } else { // We've already emitted this as an outlined object, so we can // just refer to that by its existing ID. - return existingReference; + return existingEntry.reference; } } else if (parentPropertyName.indexOf(':') === -1) { // TODO: If the property name contains a colon, we don't dedupe. Escape instead. - const parentReference = writtenObjects.get(parent); - if (parentReference !== undefined) { + const parentEntry = writtenObjects.get(parent); + if (parentEntry !== undefined) { // If the parent has a reference, we can refer to this object indirectly // through the property name inside that parent. let propertyName = parentPropertyName; @@ -4103,7 +4125,12 @@ function renderModelDestructive( break; } } - writtenObjects.set(value, parentReference + ':' + propertyName); + writeToDedupeMap( + request, + value, + parentEntry.id, + parentEntry.reference + ':' + propertyName, + ); } } @@ -4946,7 +4973,7 @@ function outlineComponentInfo( const ref = serializeByValueID(id); request.writtenDebugObjects.set(componentInfo, ref); // We also store this in the main dedupe set so that it can be referenced by inline React Elements. - request.writtenObjects.set(componentInfo, ref); + writeToDedupeMap(request, componentInfo, id, ref); return ref; } @@ -5345,11 +5372,11 @@ function renderDebugModel( } const writtenObjects = request.writtenObjects; - const existingReference = writtenObjects.get(value); - if (existingReference !== undefined) { + const existingEntry = writtenObjects.get(value); + if (existingEntry !== undefined) { // We've already emitted this as a real object, so we can refer to that by its existing reference. // This might be slightly different serialization than what renderDebugModel would've produced. - return existingReference; + return existingEntry.reference; } if (counter.objectLimit <= 0 && !doNotLimit.has(value)) { @@ -6354,7 +6381,12 @@ function retryTaskWithRequest(request: Request, task: Task): void { if (typeof resolvedModel === 'object' && resolvedModel !== null) { // We're not in a contextual place here so we can refer to this object by this ID for // any future references. - request.writtenObjects.set(resolvedModel, serializeByValueID(task.id)); + writeToDedupeMap( + request, + resolvedModel, + task.id, + serializeByValueID(task.id), + ); // Object might contain unresolved values like additional elements. // This is simulating what the JSON loop would do if this was part of it.