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-tasks.md

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

Tasks

Tasks are one-off runtime operations (migrations, cleanups, cache refresh). Experimental — enable the flag first. Tasks are scanned from serverDir, which must also be set.

import { defineConfig } from "nitro";

export default defineConfig({
  serverDir: "./server",
  experimental: { tasks: true },
});

Defining tasks

Files in <serverDir>/tasks/[name].ts. Nested dirs join with : (e.g. tasks/db/migrate.ts → db:migrate). Import defineTask from nitro/task (no auto-imports in v3).

import { defineTask } from "nitro/task";

export default defineTask({
  meta: {
    name: "db:migrate",
    description: "Run database migrations",
  },
  run({ payload, context }) {
    console.log("Running DB migration...");
    return { result: "Success" };
  },
});

run receives a TaskEvent with name, payload (Record<string, unknown>), and context (may include waitUntil). Return { result }.

Tasks can also be registered in config (config handler wins over a scanned file of the same name):

import { fileURLToPath } from "node:url";

export default defineConfig({
  serverDir: "./server",
  experimental: { tasks: true },
  tasks: {
    "db:migrate": { description: "Describe a scanned task" },
    // A config handler is imported as-is (NOT resolved against rootDir) — use an absolute path:
    "db:seed": {
      handler: fileURLToPath(new URL("scripts/seed.ts", import.meta.url)),
      description: "Seed the database",
    },
  },
});

Scheduled tasks (cron)

Map cron expressions to task name(s). Multiple tasks under one expression run in parallel; scheduled runs get a payload.scheduledTime timestamp.

import { defineConfig } from "nitro";

export default defineConfig({
  scheduledTasks: {
    "* * * * *": ["cms:update"],        // every minute
    "0 0 * * *": "db:cleanup",          // daily (string shorthand)
    "*/5 * * * *": ["health:check", "metrics:collect"],
  },
});

Platform support:

  • dev, node_server, node_cluster, node_middleware, bun, deno_server → croner engine.
  • cloudflare_module / cloudflare_pages → native Cron Triggers (wrangler config auto-generated).
  • vercel → native Cron Jobs (config auto-generated; secure with CRON_SECRET).

Running tasks programmatically

import { defineHandler } from "nitro";
import { runTask } from "nitro/task";

export default defineHandler(async (event) => {
  // IMPORTANT: authenticate and validate before running!
  const payload = Object.fromEntries(event.url.searchParams);
  const { result } = await runTask("db:migrate", { payload });
  return { result };
});

runTask throws a 404 if the task doesn't exist, 501 if it has no handler; errors from run propagate to the caller.

Background work with waitUntil

import { defineTask } from "nitro/task";

export default defineTask({
  run({ context }) {
    const promise = fetch("https://api.example.com/sync");
    context.waitUntil?.(promise);
    return promise.then(() => ({ result: "ok" }));
  },
});

Dev server tools

While nitro dev runs:

  • GET /_nitro/tasks — list available tasks + scheduled tasks.
  • GET|POST /_nitro/tasks/:name — execute (payload from query and/or JSON body under "payload").
  • CLI: nitro task list and nitro task run db:migrate --payload "{}".

Key Points

  • Requires experimental.tasks: true and serverDir set; defineTask and runTask both come from nitro/task (no auto-imports in v3).
  • Each task has one running instance — parallel calls of the same name share a single run/result.
  • scheduledTasks cron config is translated to native triggers on Cloudflare and Vercel automatically.
<!-- Source references: - https://nitro.build/docs/tasks -->

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