@@ -57,34 +57,28 @@ function encode(key, value, unresolved) {
5757
5858 entry . serialized = devalue . uneval ( entry . value , ( value , uneval ) => {
5959 if ( is_promise ( value ) ) {
60+ // we serialize promises as `"${i}"`, because it's impossible for that string
61+ // to occur 'naturally' (since the quote marks would have to be escaped)
62+ // this placeholder is returned synchronously from `uneval`, which includes it in the
63+ // serialized string. Later (at least one microtask from now), when `p.then` runs, it'll
64+ // be replaced.
65+ const placeholder = `"${ uid ++ } "` ;
6066 const p = value
61- . then ( ( v ) => `r(${ uneval ( v ) } )` )
67+ . then ( ( v ) => {
68+ entry . serialized = entry . serialized . replace ( placeholder , `r(${ uneval ( v ) } )` ) ;
69+ } )
6270 . catch ( ( devalue_error ) =>
6371 e . hydratable_serialization_failed (
6472 key ,
6573 serialization_stack ( entry . stack , devalue_error ?. stack )
6674 )
6775 ) ;
6876
69- // prevent unhandled rejections from crashing the server
70- p . catch ( ( ) => { } ) ;
71-
72- // track which promises are still resolving when render is complete
7377 unresolved ?. set ( p , key ) ;
74- p . finally ( ( ) => unresolved ?. delete ( p ) ) ;
75-
76- // we serialize promises as `"${i}"`, because it's impossible for that string
77- // to occur 'naturally' (since the quote marks would have to be escaped)
78- const placeholder = `"${ uid ++ } "` ;
79-
80- ( entry . promises ??= [ ] ) . push (
81- p
82- . then ( ( s ) => {
83- entry . serialized = entry . serialized . replace ( placeholder , s ) ;
84- } )
85- . catch ( ( ) => { } )
86- ) ;
78+ // prevent unhandled rejections from crashing the server, track which promises are still resolving when render is complete
79+ p . catch ( ( ) => { } ) . finally ( ( ) => unresolved ?. delete ( p ) ) ;
8780
81+ ( entry . promises ??= [ ] ) . push ( p ) ;
8882 return placeholder ;
8983 }
9084 } ) ;
0 commit comments