Skip to content

Commit

Permalink
docs: add nitro plugins examples (#1403)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hebilicious authored Jul 12, 2023
1 parent 13263d1 commit bbfcfbe
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions docs/content/1.guide/7.plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,40 @@ export default defineNuxtConfig({
})
```
::

## Examples

### Graceful Shutdown

You can use plugins to register a hook that resolves when Nitro is closed.

```ts
export default defineNitroPlugin((nitro) => {
nitro.hooks.hookOnce("close", async () => {
// Will run when nitro is closed
console.log("Closing nitro server...")
await new Promise((resolve) => setTimeout(resolve, 500));
console.log("Task is done!");
});
})
```

### Renderer Response

You can use plugins to register a hook that modifies the renderer response.

::alert
This **only works** for render handler defined with [`renderer`](https://nitro.unjs.io/config#renderer) and won't be called for other api/server routes.
In [Nuxt](https://nuxt.com/) this hook will be called for Server Side Rendered pages
::

```ts
export default defineNitroPlugin((nitro) => {

nitro.hooks.hook('render:response', (response) => {
// Inspect or Modify the renderer response here
console.log(response)
})
})
```

0 comments on commit bbfcfbe

Please sign in to comment.