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."
---
2022-04-06 05:56:08 +00:00
# Lifecycle Hooks
2022-10-06 09:15:30 +00:00
Nuxt provides a powerful hooking system to expand almost every aspect using hooks. This feature 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
### Usage with `nuxt.config`
```js [nuxt.config]
export default defineNuxtConfig({
hooks: {
'close': () => { }
}
})
```
### Usage with Nuxt Modules
```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
})
```
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
### Usage with Plugins
```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
```
::alert{icon=👉}
2022-11-16 10:04:28 +00:00
Learn more about [available lifecycle hooks ](/docs/api/advanced/hooks )
2022-04-06 05:56:08 +00:00
::
2022-09-26 09:18:50 +00:00
## Nitro App Hooks (Runtime)
2023-04-15 10:30:17 +00:00
These hooks are available for [Nitro plugins ](https://nitro.unjs.io/guide/plugins ) to hook into Nitro's runtime behavior.
2022-09-26 09:18:50 +00:00
### Usage within a Nitro Plugin
```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)
})
})
```
::alert{icon=👉}
2023-04-15 10:30:17 +00:00
Learn more about available [Nitro lifecycle hooks ](/docs/api/advanced/hooks#nitro-app-hooks-runtime-server-side ).
2022-09-26 09:18:50 +00:00
::
2023-07-25 08:25:14 +00:00
## Add additional hooks
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;
}
}
```