1
1
import {
2
2
ComponentInternalInstance ,
3
- ComponentOptions ,
4
3
currentInstance ,
5
4
isInSSRComponentSetup ,
6
5
LifecycleHooks ,
@@ -66,15 +65,17 @@ export function injectHook(
66
65
export const createHook = < T extends Function = ( ) => any > (
67
66
lifecycle : LifecycleHooks
68
67
) => ( hook : T , target : ComponentInternalInstance | null = currentInstance ) =>
69
- // post-create lifecycle registrations are noops during SSR
70
- ! isInSSRComponentSetup && injectHook ( lifecycle , hook , target )
68
+ // post-create lifecycle registrations are noops during SSR (except for serverPrefetch)
69
+ ( ! isInSSRComponentSetup || lifecycle === LifecycleHooks . SERVER_PREFETCH ) &&
70
+ injectHook ( lifecycle , hook , target )
71
71
72
72
export const onBeforeMount = createHook ( LifecycleHooks . BEFORE_MOUNT )
73
73
export const onMounted = createHook ( LifecycleHooks . MOUNTED )
74
74
export const onBeforeUpdate = createHook ( LifecycleHooks . BEFORE_UPDATE )
75
75
export const onUpdated = createHook ( LifecycleHooks . UPDATED )
76
76
export const onBeforeUnmount = createHook ( LifecycleHooks . BEFORE_UNMOUNT )
77
77
export const onUnmounted = createHook ( LifecycleHooks . UNMOUNTED )
78
+ export const onServerPrefetch = createHook ( LifecycleHooks . SERVER_PREFETCH )
78
79
79
80
export type DebuggerHook = ( e : DebuggerEvent ) => void
80
81
export const onRenderTriggered = createHook < DebuggerHook > (
@@ -96,32 +97,3 @@ export function onErrorCaptured<TError = Error>(
96
97
) {
97
98
injectHook ( LifecycleHooks . ERROR_CAPTURED , hook , target )
98
99
}
99
-
100
- export function onServerPrefetch <
101
- T extends ( ) => Promise < any > = ( ) => Promise < unknown >
102
- > ( handler : T ) {
103
- const target = currentInstance
104
- if ( target ) {
105
- if ( isInSSRComponentSetup ) {
106
- const type = target . type as ComponentOptions
107
- let hook = type . serverPrefetch
108
- if ( hook ) {
109
- // Merge hook
110
- type . serverPrefetch = ( ) =>
111
- Promise . all ( [ handler ( ) , ( hook as Function ) . call ( target . proxy ) ] )
112
- } else {
113
- type . serverPrefetch = handler
114
- }
115
- }
116
- } else if ( __DEV__ ) {
117
- warn (
118
- `onServerPrefetch is called when there is no active component instance to be ` +
119
- `associated with. ` +
120
- `Lifecycle injection APIs can only be used during execution of setup().` +
121
- ( __FEATURE_SUSPENSE__
122
- ? ` If you are using async setup(), make sure to register lifecycle ` +
123
- `hooks before the first await statement.`
124
- : `` )
125
- )
126
- }
127
- }
0 commit comments