2022-10-06 09:15:30 +00:00
---
title: "Lifecycle Hooks"
description: "Nuxt provides a powerful hooking system to expand almost every aspect using hooks."
---
2023-10-18 10:59:43 +00:00
::callout
The hooking system is powered by [unjs/hookable ](https://github.com/unjs/hookable ).
::
2022-04-06 05:56:08 +00:00
2022-08-13 07:27:04 +00:00
## Nuxt Hooks (Build Time)
2022-04-06 05:56:08 +00:00
2022-11-16 10:04:28 +00:00
These hooks are available for [Nuxt Modules ](/docs/guide/going-further/modules ) and build context.
2022-04-06 05:56:08 +00:00
2023-10-18 10:59:43 +00:00
### Within `nuxt.config`
2022-04-06 05:56:08 +00:00
```js [nuxt.config]
export default defineNuxtConfig({
hooks: {
2023-10-18 10:59:43 +00:00
close: () => { }
2022-04-06 05:56:08 +00:00
}
})
```
2023-10-18 10:59:43 +00:00
### Within Nuxt Modules
2022-04-06 05:56:08 +00:00
```js
import { defineNuxtModule } from '@nuxt/kit'
export default defineNuxtModule({
setup (options, nuxt) {
nuxt.hook('close', async () => { })
2023-01-19 11:20:06 +00:00
}
2022-04-06 05:56:08 +00:00
})
```
2023-10-18 10:59:43 +00:00
::read-more{to="/docs/api/advanced/hooks#nuxt-hooks-build-time"}
Explore all available Nuxt hooks.
::
2022-08-13 07:27:04 +00:00
## App Hooks (Runtime)
2022-04-06 05:56:08 +00:00
2022-11-16 10:04:28 +00:00
App hooks can be mainly used by [Nuxt Plugins ](/docs/guide/directory-structure/plugins ) to hook into rendering lifecycle but could also be used in Vue composables.
2022-04-06 05:56:08 +00:00
```js [plugins/test.ts]
2022-05-06 08:48:10 +00:00
export default defineNuxtPlugin((nuxtApp) => {
2023-07-18 10:31:45 +00:00
nuxtApp.hook('page:start', () => {
2023-07-25 08:25:14 +00:00
/* your code goes here */
})
2022-05-06 08:48:10 +00:00
})
2022-04-06 05:56:08 +00:00
```
2023-10-18 10:59:43 +00:00
::read-more{to="/docs/api/advanced/hooks#app-hooks-runtime"}
Explore all available App hooks.
2022-04-06 05:56:08 +00:00
::
2022-09-26 09:18:50 +00:00
2023-10-18 10:59:43 +00:00
## Server Hooks (Runtime)
2022-09-26 09:18:50 +00:00
2023-10-18 10:59:43 +00:00
These hooks are available for [server plugins ](/docs/guide/directory-structure/server#server-plugins ) to hook into Nitro's runtime behavior.
2022-09-26 09:18:50 +00:00
```js [~/server/plugins/test.ts]
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)
})
})
```
2023-10-18 10:59:43 +00:00
::read-more{to="/docs/api/advanced/hooks#nitro-app-hooks-runtime-server-side"}
Learn more about available Nitro lifecycle hooks.
2022-09-26 09:18:50 +00:00
::
2023-07-25 08:25:14 +00:00
2023-10-18 10:59:43 +00:00
## Additional Hooks
2023-07-25 08:25:14 +00:00
You can add additional hooks by augmenting the types provided by Nuxt. This can be useful for modules.
```ts
import { HookResult } from "@nuxt/schema";
declare module '#app' {
interface RuntimeNuxtHooks {
'your-nuxt-runtime-hook': () => HookResult
}
interface NuxtHooks {
'your-nuxt-hook': () => HookResult
}
}
declare module 'nitropack' {
interface NitroRuntimeHooks {
'your-nitro-hook': () => void;
}
}
```