# Source: https://nuxt.com/raw/docs/3.x/api/advanced/hooks.md

# Source: https://nuxt.com/raw/docs/3.x/guide/going-further/hooks.md

# Source: https://nuxt.com/raw/docs/4.x/api/advanced/hooks.md

# Source: https://nuxt.com/raw/docs/4.x/guide/going-further/hooks.md

# Lifecycle Hooks

> Nuxt provides a powerful hooking system to expand almost every aspect using hooks.

<tip>

The hooking system is powered by [unjs/hookable](https://github.com/unjs/hookable).

</tip>

## Nuxt Hooks (Build Time)

These hooks are available for [Nuxt modules](/docs/4.x/guide/modules) and build context.

### Within `nuxt.config.ts`

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  hooks: {
    close: () => { },
  },
})
```

### Within Nuxt Modules

```js
import { defineNuxtModule } from '@nuxt/kit'

export default defineNuxtModule({
  setup (options, nuxt) {
    nuxt.hook('close', async () => { })
  },
})
```

<read-more to="/docs/4.x/api/advanced/hooks#nuxt-hooks-build-time">

Explore all available Nuxt hooks.

</read-more>

## App Hooks (Runtime)

App hooks can be mainly used by [Nuxt Plugins](/docs/4.x/directory-structure/app/plugins) to hook into rendering lifecycle but could also be used in Vue composables.

```ts [app/plugins/test.ts]
export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.hook('page:start', () => {
    /* your code goes here */
  })
})
```

<read-more to="/docs/4.x/api/advanced/hooks#app-hooks-runtime">

Explore all available App hooks.

</read-more>

## Server Hooks (Runtime)

These hooks are available for [server plugins](/docs/4.x/directory-structure/server#server-plugins) to hook into Nitro's runtime behavior.

```ts [~/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)
  })
})
```

<read-more to="/docs/4.x/api/advanced/hooks#nitro-app-hooks-runtime-server-side">

Learn more about available Nitro lifecycle hooks.

</read-more>

## Adding Custom Hooks

You can define your own custom hooks support by extending Nuxt's hook interfaces.

```ts
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
  }
}
```