These hooks are available for Nuxt Modules and build context.
nuxt.config.tsexport default defineNuxtConfig({
hooks: {
close: () => { },
},
})
import { defineNuxtModule } from '@nuxt/kit'
export default defineNuxtModule({
setup (options, nuxt) {
nuxt.hook('close', async () => { })
},
})
App hooks can be mainly used by Nuxt Plugins to hook into rendering lifecycle but could also be used in Vue composables.
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hook('page:start', () => {
/* your code goes here */
})
})
These hooks are available for server plugins to hook into Nitro's runtime behavior.
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('render:html', (html, { event }) => {
console.log('render:html', html)
html.bodyAppend.push('<hr>Appended by custom plugin')
})
nitroApp.hooks.hook('render:response', (response, { event }) => {
console.log('render:response', response)
})
})
You can define your own custom hooks support by extending Nuxt's hook interfaces.
import type { HookResult } from '@nuxt/schema'
declare module '#app' {
interface RuntimeNuxtHooks {
'your-nuxt-runtime-hook': () => HookResult
}
interface NuxtHooks {
'your-nuxt-hook': () => HookResult
}
}
declare module 'nitropack/types' {
interface NitroRuntimeHooks {
'your-nitro-hook': () => void
}
}