File tree 2 files changed +32
-13
lines changed
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) {
873
873
expect ( html ) . toBe ( `<div>hello</div>` )
874
874
} )
875
875
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
+
876
896
// #2763
877
897
test ( 'error handling w/ async setup' , async ( ) => {
878
898
const fn = vi . fn ( )
Original file line number Diff line number Diff line change @@ -94,21 +94,20 @@ export function renderComponentVNode(
94
94
const instance = createComponentInstance ( vnode , parentComponent , null )
95
95
const res = setupComponent ( instance , true /* isSSR */ )
96
96
const hasAsyncSetup = isPromise ( res )
97
- const prefetches = instance . sp /* LifecycleHooks.SERVER_PREFETCH */
97
+ let prefetches = instance . sp /* LifecycleHooks.SERVER_PREFETCH */
98
98
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 (
106
105
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 )
112
111
return p . then ( ( ) => renderComponentSubTree ( instance , slotScopeId ) )
113
112
} else {
114
113
return renderComponentSubTree ( instance , slotScopeId )
You can’t perform that action at this time.
0 commit comments