feat: onServerPrefetch (#3070)
Support equivalent of `serverPrefetch` option via Composition API.
This commit is contained in:
@@ -65,8 +65,9 @@ export function injectHook(
|
||||
export const createHook = <T extends Function = () => any>(
|
||||
lifecycle: LifecycleHooks
|
||||
) => (hook: T, target: ComponentInternalInstance | null = currentInstance) =>
|
||||
// post-create lifecycle registrations are noops during SSR
|
||||
!isInSSRComponentSetup && injectHook(lifecycle, hook, target)
|
||||
// post-create lifecycle registrations are noops during SSR (except for serverPrefetch)
|
||||
(!isInSSRComponentSetup || lifecycle === LifecycleHooks.SERVER_PREFETCH) &&
|
||||
injectHook(lifecycle, hook, target)
|
||||
|
||||
export const onBeforeMount = createHook(LifecycleHooks.BEFORE_MOUNT)
|
||||
export const onMounted = createHook(LifecycleHooks.MOUNTED)
|
||||
@@ -74,6 +75,7 @@ export const onBeforeUpdate = createHook(LifecycleHooks.BEFORE_UPDATE)
|
||||
export const onUpdated = createHook(LifecycleHooks.UPDATED)
|
||||
export const onBeforeUnmount = createHook(LifecycleHooks.BEFORE_UNMOUNT)
|
||||
export const onUnmounted = createHook(LifecycleHooks.UNMOUNTED)
|
||||
export const onServerPrefetch = createHook(LifecycleHooks.SERVER_PREFETCH)
|
||||
|
||||
export type DebuggerHook = (e: DebuggerEvent) => void
|
||||
export const onRenderTriggered = createHook<DebuggerHook>(
|
||||
@@ -83,15 +85,15 @@ export const onRenderTracked = createHook<DebuggerHook>(
|
||||
LifecycleHooks.RENDER_TRACKED
|
||||
)
|
||||
|
||||
export type ErrorCapturedHook = (
|
||||
err: unknown,
|
||||
export type ErrorCapturedHook<TError = unknown> = (
|
||||
err: TError,
|
||||
instance: ComponentPublicInstance | null,
|
||||
info: string
|
||||
) => boolean | void
|
||||
|
||||
export const onErrorCaptured = (
|
||||
hook: ErrorCapturedHook,
|
||||
export function onErrorCaptured<TError = Error>(
|
||||
hook: ErrorCapturedHook<TError>,
|
||||
target: ComponentInternalInstance | null = currentInstance
|
||||
) => {
|
||||
) {
|
||||
injectHook(LifecycleHooks.ERROR_CAPTURED, hook, target)
|
||||
}
|
||||
|
||||
@@ -153,7 +153,7 @@ export type Component<
|
||||
|
||||
export { ComponentOptions }
|
||||
|
||||
type LifecycleHook = Function[] | null
|
||||
type LifecycleHook<TFn = Function> = TFn[] | null
|
||||
|
||||
export const enum LifecycleHooks {
|
||||
BEFORE_CREATE = 'bc',
|
||||
@@ -168,7 +168,8 @@ export const enum LifecycleHooks {
|
||||
ACTIVATED = 'a',
|
||||
RENDER_TRIGGERED = 'rtg',
|
||||
RENDER_TRACKED = 'rtc',
|
||||
ERROR_CAPTURED = 'ec'
|
||||
ERROR_CAPTURED = 'ec',
|
||||
SERVER_PREFETCH = 'sp'
|
||||
}
|
||||
|
||||
export interface SetupContext<E = EmitsOptions> {
|
||||
@@ -414,6 +415,10 @@ export interface ComponentInternalInstance {
|
||||
* @internal
|
||||
*/
|
||||
[LifecycleHooks.ERROR_CAPTURED]: LifecycleHook
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
[LifecycleHooks.SERVER_PREFETCH]: LifecycleHook<() => Promise<unknown>>
|
||||
}
|
||||
|
||||
const emptyAppContext = createAppContext()
|
||||
@@ -497,7 +502,8 @@ export function createComponentInstance(
|
||||
a: null,
|
||||
rtg: null,
|
||||
rtc: null,
|
||||
ec: null
|
||||
ec: null,
|
||||
sp: null
|
||||
}
|
||||
if (__DEV__) {
|
||||
instance.ctx = createRenderContext(instance)
|
||||
|
||||
@@ -40,7 +40,8 @@ import {
|
||||
onDeactivated,
|
||||
onRenderTriggered,
|
||||
DebuggerHook,
|
||||
ErrorCapturedHook
|
||||
ErrorCapturedHook,
|
||||
onServerPrefetch
|
||||
} from './apiLifecycle'
|
||||
import {
|
||||
reactive,
|
||||
@@ -555,6 +556,7 @@ export function applyOptions(
|
||||
renderTracked,
|
||||
renderTriggered,
|
||||
errorCaptured,
|
||||
serverPrefetch,
|
||||
// public API
|
||||
expose
|
||||
} = options
|
||||
@@ -798,6 +800,9 @@ export function applyOptions(
|
||||
if (unmounted) {
|
||||
onUnmounted(unmounted.bind(publicThis))
|
||||
}
|
||||
if (serverPrefetch) {
|
||||
onServerPrefetch(serverPrefetch.bind(publicThis))
|
||||
}
|
||||
|
||||
if (__COMPAT__) {
|
||||
if (
|
||||
|
||||
@@ -37,7 +37,8 @@ export {
|
||||
onDeactivated,
|
||||
onRenderTracked,
|
||||
onRenderTriggered,
|
||||
onErrorCaptured
|
||||
onErrorCaptured,
|
||||
onServerPrefetch
|
||||
} from './apiLifecycle'
|
||||
export { provide, inject } from './apiInject'
|
||||
export { nextTick } from './scheduler'
|
||||
|
||||
Reference in New Issue
Block a user