All skills
antfu avatar

/nitro

@d02c484 official
by Anthony Fuantfu/skills5.9k stars
335

Nitro is the framework-agnostic server toolkit (powering Nuxt) for building and deploying web servers anywhere. Use when working with nitro.config, server routes/event handlers, route rules, caching, storage, tasks, websockets, or deploying to Node/Bun/Deno/Cloudflare/Vercel.

Use this Skill: https://skilld.dev/gh/antfu/skills/nitro

This session only. Nothing lands on disk.

referencesfeatures-plugins.md

≈992 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Plugins & Lifecycle Hooks

Nitro plugins run once at server startup to extend runtime behavior. They are auto-registered from plugins/ (by filename order) and receive the nitroApp context. Import definePlugin from nitro.

import { definePlugin } from "nitro";

export default definePlugin((nitroApp) => {
  console.log("Nitro starting");
});

v3 renamed defineNitroPlugin → definePlugin. The plugin function itself must be synchronous (return void); the hooks it registers may be async. Register external plugins with the plugins: ["~/plugins/x.ts"] config option.

The nitroApp context

Property Description
hooks hookable instance to register lifecycle callbacks.
h3 The underlying H3 app instance.
fetch(req) The app's internal fetch handler.
captureError(error, ctx) Feed errors into the error hook pipeline.

Runtime hooks

Register lifecycle callbacks inside a plugin. hook() returns an unregister function.

Hook Signature When
request (event) => void | Promise Start of each request, before routing.
response (res: Response, event) => void | Promise After the response is created.
error (error, { event?, tags? }) => void When any error is captured.
close () => void On server shutdown.
import { definePlugin } from "nitro";

export default definePlugin((nitroApp) => {
  nitroApp.hooks.hook("request", (event) => {
    console.log("→", event.path);
  });

  nitroApp.hooks.hook("response", (res, event) => {
    const { pathname } = new URL(event.req.url);
    if (pathname.endsWith(".js")) res.headers.append("Vary", "Origin");
  });

  nitroApp.hooks.hook("close", async () => {
    // clean up DB connections, timers, etc.
  });
});

The request hook is the very first code per request (errors there are captured, not fatal). The response hook runs for every response, including static assets and errors. On shutdown the server waits for pending event.waitUntil tasks.

Error capture

import { definePlugin } from "nitro";

export default definePlugin((nitroApp) => {
  nitroApp.hooks.hook("error", (error, { event, tags }) => {
    console.error(`[${tags?.join(",")}] ${event?.path}`, error);
  });

  // Manually report
  nitroApp.captureError(new Error("startup issue"), { tags: ["startup"] });
});

tags identify the source: "request", "response", "cache", "plugin", "unhandledRejection", "uncaughtException". Process-level unhandled rejections and uncaught exceptions are auto-captured into the error hook.

Notes

  • features.runtimeHooks is auto-enabled when at least one plugin exists; force with features.runtimeHooks: true.
  • NitroRuntimeHooks is augmentable — presets add platform hooks (e.g. Cloudflare cloudflare:scheduled, cloudflare:email; Vercel vercel:queue).
  • useNitroApp, useNitroHooks, and getRouteRules are imported from nitro/app (no auto-imports in v3). Outside plugins, prefer useNitroHooks() over useNitroApp().hooks to guarantee a hooks instance.
  • The hook() method returns an unregister function.
  • For internal requests to your own server, use serverFetch from nitro (v2 localFetch is gone).

Key Points

  • Plugins run once at startup; use them to init resources and register hooks.
  • Import definePlugin from nitro (not defineNitroPlugin); useNitroApp/useNitroHooks from nitro/app.
  • Use request/response/error/close hooks for cross-cutting runtime behavior.
  • Customize response headers in the response hook via res.headers.
<!-- Source references: - https://nitro.build/docs/plugins - https://nitro.build/docs/lifecycle -->

Source: SKILL.md on GitHub

No alerts3d3 checks · Risk SAFE
  • Gen Agent Trust Hub3d

    The skill provides comprehensive documentation and guidelines for Nitro v3, a server toolkit for building web applications. It includes security best practices like using parameterized queries to prevent SQL injection and authenticating task execution. No security issues were detected.

  • Socket3d

    No alerts

  • Snyk3d

    Risk: LOW · No issues

Signed by skilld at d02c484. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Activeupdated 4 days ago
Other metadata
metadata
{
  "author": "Anthony Fu",
  "version": "2026.9.25",
  "source": "Generated from https://github.com/nitrojs/nitro, scripts located at https://github.com/antfu/skills"
}

README badge

README badge for antfu/skills/nitro