// pages/+onHookCall.ts// Environment: serverimport type { Config, PageContextServer } from 'vike/types'type Hook = Parameters<Extract<Config['onHookCall'], Function>>[0]export async function onHookCall(hook: Hook, pageContext: PageContextServer) { console.log('before', hook.name, hook.filePath) await hook.call() console.log('after', hook.name, hook.filePath)}
You must always call hook.call() and do so before any await.
// ❌ This breaks sync hooksexport const onHookCall = async (hook, pageContext) => { await someAsyncOperation() await hook.call() // Too late for sync hooks!}
// ❌ This breaks sync hooksexport const onHookCall: Config['onHookCall'] = async (hook, pageContext) => { await someAsyncOperation() await hook.call() // Too late for sync hooks!}
// ✅ This works for both sync and async hooksexport const onHookCall = async (hook, pageContext) => { await hook.call() // Called before any await await someAsyncOperation()}
// ✅ This works for both sync and async hooksexport const onHookCall: Config['onHookCall'] = async (hook, pageContext) => { await hook.call() // Called before any await await someAsyncOperation()}
The first parameter hook contains:
hook.name — Name of the hook being called (e.g. 'onRenderHtml', 'data', 'guard')
hook.filePath — File path where the hook is defined