File tree Expand file tree Collapse file tree 2 files changed +32
-13
lines changed Expand file tree Collapse file tree 2 files changed +32
-13
lines changed Original file line number Diff line number Diff line change @@ -873,6 +873,26 @@ function testRender(type: string, render: typeof renderToString) {
873873 expect ( html ) . toBe ( `<div>hello</div>` )
874874 } )
875875
876+ test ( 'serverPrefetch w/ async setup' , async ( ) => {
877+ const msg = Promise . resolve ( 'hello' )
878+ const app = createApp ( {
879+ data ( ) {
880+ return {
881+ msg : '' ,
882+ }
883+ } ,
884+ async serverPrefetch ( ) {
885+ this . msg = await msg
886+ } ,
887+ render ( ) {
888+ return h ( 'div' , this . msg )
889+ } ,
890+ async setup ( ) { } ,
891+ } )
892+ const html = await render ( app )
893+ expect ( html ) . toBe ( `<div>hello</div>` )
894+ } )
895+
876896 // #2763
877897 test ( 'error handling w/ async setup' , async ( ) => {
878898 const fn = vi . fn ( )
Original file line number Diff line number Diff line change @@ -94,21 +94,20 @@ export function renderComponentVNode(
9494 const instance = createComponentInstance ( vnode , parentComponent , null )
9595 const res = setupComponent ( instance , true /* isSSR */ )
9696 const hasAsyncSetup = isPromise ( res )
97- const prefetches = instance . sp /* LifecycleHooks.SERVER_PREFETCH */
97+ let prefetches = instance . sp /* LifecycleHooks.SERVER_PREFETCH */
9898 if ( hasAsyncSetup || prefetches ) {
99- let p : Promise < unknown > = hasAsyncSetup
100- ? ( res as Promise < void > )
101- : Promise . resolve ( )
102- if ( prefetches ) {
103- p = p
104- . then ( ( ) =>
105- Promise . all (
99+ const p : Promise < unknown > = Promise . resolve ( res as Promise < void > )
100+ . then ( ( ) => {
101+ // instance.sp may be null until an async setup resolves, so evaluate it here
102+ if ( hasAsyncSetup ) prefetches = instance . sp
103+ if ( prefetches ) {
104+ return Promise . all (
106105 prefetches . map ( prefetch => prefetch . call ( instance . proxy ) ) ,
107- ) ,
108- )
109- // Note: error display is already done by the wrapped lifecycle hook function.
110- . catch ( NOOP )
111- }
106+ )
107+ }
108+ } )
109+ // Note: error display is already done by the wrapped lifecycle hook function.
110+ . catch ( NOOP )
112111 return p . then ( ( ) => renderComponentSubTree ( instance , slotScopeId ) )
113112 } else {
114113 return renderComponentSubTree ( instance , slotScopeId )
You can’t perform that action at this time.
0 commit comments