@@ -2310,6 +2310,31 @@ export function renderToStream(code, options = {}) {
23102310 const canBatchStubs = ! options . serializer && ! options . sink ;
23112311 let stubBatch = null ;
23122312 const STUB_BATCH_KEY = "$B" ;
2313+ // Async mode (the thenable: the whole document resolves at once) has no
2314+ // reader before completion, so nothing is gained by handing seroval a
2315+ // value as it arrives — and handing it over in arrival order makes the
2316+ // bytes follow settle order: reference ids and resolution chunks are
2317+ // assigned as seroval meets values, so identical data with different
2318+ // latency produced different documents and a body-digest ETag missed
2319+ // revalidations (#4028). From the moment the thenable is taken (after the
2320+ // synchronous first pass, whose writes are already in render order), the
2321+ // document's writes park here and the stub batch stays parked too; both
2322+ // are handed over once, at the serializer flush, sorted by hydration key,
2323+ // by which point the drained registry has settled what they hold —
2324+ // seroval then meets every value, and every fulfillment, in key order.
2325+ let stableOrder = false ;
2326+ let parkedWrites = null ;
2327+ const byKey = ( a , b ) => ( a [ 0 ] < b [ 0 ] ? - 1 : a [ 0 ] > b [ 0 ] ? 1 : 0 ) ;
2328+ const write = ( id , value ) => {
2329+ if ( stableOrder && ! dead ) ( parkedWrites ||= [ ] ) . push ( [ id , value ] ) ;
2330+ else serializer . write ( id , value ) ;
2331+ } ;
2332+ const flushParkedWrites = ( ) => {
2333+ if ( ! parkedWrites ) return ;
2334+ const writes = parkedWrites . sort ( byKey ) ;
2335+ parkedWrites = null ;
2336+ for ( let i = 0 ; i < writes . length ; i ++ ) serializer . write ( writes [ i ] [ 0 ] , writes [ i ] [ 1 ] ) ;
2337+ } ;
23132338 const flushStubBatch = ( ) => {
23142339 if ( ! stubBatch ) return ;
23152340 const batch = stubBatch ;
@@ -2320,7 +2345,9 @@ export function renderToStream(code, options = {}) {
23202345 return ;
23212346 }
23222347 const obj = { } ;
2323- for ( const [ id , p ] of batch ) obj [ id ] = p ;
2348+ const entries = [ ...batch ] ;
2349+ if ( stableOrder ) entries . sort ( byKey ) ;
2350+ for ( const [ id , p ] of entries ) obj [ id ] = p ;
23242351 serializer . write ( STUB_BATCH_KEY , obj ) ;
23252352 pushTask (
23262353 `(b=>{for(var k in b)_$HY.r[k]=b[k];delete _$HY.r["${ STUB_BATCH_KEY } "]})(_$HY.r["${ STUB_BATCH_KEY } "])`
@@ -2528,6 +2555,7 @@ export function renderToStream(code, options = {}) {
25282555 // so the stub batch must land first. The task output still rides
25292556 // the shell snapshot — `tasks` accumulates until doShell reads it.
25302557 flushStubBatch ( ) ;
2558+ flushParkedWrites ( ) ;
25312559 serializer . flush ( ) ;
25322560 } )
25332561 ) ; // double queue because of elsewhere
@@ -2874,7 +2902,7 @@ export function renderToStream(code, options = {}) {
28742902 if ( p && typeof p === "object" && typeof p . then === "function" ) {
28752903 if ( ! firstFlushed && deferStream ) {
28762904 blockingPromises . add ( p ) ;
2877- p . then ( d => serializer . write ( id , d ) ) . catch ( e => serializer . write ( id , e ) ) ;
2905+ p . then ( d => write ( id , d ) ) . catch ( e => write ( id , e ) ) ;
28782906 return ;
28792907 }
28802908 // Every pending promise handed to seroval joins the abandonment
@@ -2891,7 +2919,7 @@ export function renderToStream(code, options = {}) {
28912919 return ;
28922920 }
28932921 }
2894- serializer . write ( id , p ) ;
2922+ write ( id , p ) ;
28952923 } ,
28962924 escape : escape ,
28972925 resolve : resolveSSRNode ,
@@ -2947,7 +2975,7 @@ export function renderToStream(code, options = {}) {
29472975 } ) ;
29482976 if ( canBatchStubs && ! shellCompleted && ! dead )
29492977 ( stubBatch ||= new Map ( ) ) . set ( key + "_fr" , p ) ;
2950- else serializer . write ( key + "_fr" , p ) ;
2978+ else write ( key + "_fr" , p ) ;
29512979 }
29522980 return ( value , error , escaped ) => {
29532981 if ( registry . has ( key ) ) {
@@ -3294,7 +3322,7 @@ export function renderToStream(code, options = {}) {
32943322 // drain-turn runway they had when writes happened at registration
32953323 // time, so a settled-before-shell record is visibly settled IN the
32963324 // shell (client boundaries branch on `.s === 1` at hydrate time).
3297- flushStubBatch ( ) ;
3325+ if ( ! stableOrder ) flushStubBatch ( ) ;
32983326 if ( registry . size !== lastRegistrySize || drainTurn ++ < MIN_DRAIN_TURNS ) {
32993327 if ( registry . size !== lastRegistrySize ) drainTurn = 0 ;
33003328 lastRegistrySize = registry . size ;
@@ -3436,6 +3464,9 @@ export function renderToStream(code, options = {}) {
34363464 // still open then. Consumers see an already-committed stub, which
34373465 // `createSSRResponse`/`commitEventResponse` pass through idempotently.
34383466 then ( onFulfilled , onRejected ) {
3467+ // Document mode only: a custom serializer/sink addresses records by
3468+ // key and keeps its own delivery order.
3469+ if ( canBatchStubs ) stableOrder = true ;
34393470 const freezeHead = ( ) => {
34403471 if ( requestEvent && requestEvent . response )
34413472 commitResponseStub ( requestEvent . response , { event : requestEvent } ) ;
0 commit comments