UNPKG

@nuxt/nitro-server

Version:

Nitro server integration for Nuxt

353 lines 12.7 kB
import { EventHandler, H3Event } from "h3"; import { Nitro, NitroConfig, NitroDevEventHandler, NitroEventHandler, NitroOptions, NitroRouteConfig, NitroRuntimeConfig, NitroRuntimeConfigApp } from "nitropack/types"; import { LogObject } from "consola"; import { NuxtIslandContext, NuxtIslandResponse, NuxtRenderChunkContext, NuxtRenderCloseContext, NuxtRenderHTMLContext, NuxtRenderRouteContext } from "#app/types"; import { HookResult, RuntimeConfig, TSReference } from "nuxt/schema"; //#region src/augments.d.ts /** * Per-channel toggles for `tracingChannel`, with a `nuxt` key for Nuxt-owned * channels (`nuxt.render`, `nuxt.island`, `nuxt.data`, `nuxt.plugin`). Channel * names follow the [untracing](https://github.com/unjs/untracing) naming * convention (`{namespace}.{operation}`). * * @experimental Channel names, payload shapes, and option keys may change. */ interface NuxtTracingChannelOptions { /** Enable Nuxt-owned channels (`nuxt.render`, `nuxt.island`, `nuxt.data`, `nuxt.plugin`). */ nuxt?: boolean; srvx?: boolean; h3?: boolean; unstorage?: boolean; } declare global { interface ImportMeta { dev: boolean; test: boolean; } } declare module "nitropack" { interface NitroRuntimeConfigApp { buildAssetsDir: string; cdnURL: string; buildId: string; } interface NitroRouteRules { ssr?: boolean; streaming?: boolean; noScripts?: boolean; /** @deprecated Use `noScripts` instead */ experimentalNoScripts?: boolean; appMiddleware?: Record<string, boolean>; } interface NitroConfig { tracingChannel?: boolean | NuxtTracingChannelOptions; } } declare module "nitropack/types" { interface NitroRuntimeConfigApp { buildAssetsDir: string; cdnURL: string; buildId: string; } interface NitroRouteRules { ssr?: boolean; streaming?: boolean; noScripts?: boolean; /** @deprecated Use `noScripts` instead */ experimentalNoScripts?: boolean; appMiddleware?: Record<string, boolean>; } interface NitroConfig { tracingChannel?: boolean | NuxtTracingChannelOptions; } } declare module "nitropack" { interface NitroRuntimeConfig extends RuntimeConfig {} interface NitroRouteConfig { ssr?: boolean; streaming?: boolean; noScripts?: boolean; /** @deprecated Use `noScripts` instead */ experimentalNoScripts?: boolean; } interface NitroRuntimeHooks { "dev:ssr-logs": (ctx: { logs: LogObject[]; path: string; }) => void | Promise<void>; "render:html": (htmlContext: NuxtRenderHTMLContext, context: { event: H3Event; streaming?: boolean; }) => void | Promise<void>; "render:html:chunk": (chunkContext: NuxtRenderChunkContext, context: { event: H3Event; }) => void | Promise<void>; "render:html:close": (closeContext: NuxtRenderCloseContext, context: { event: H3Event; }) => void | Promise<void>; "render:route": (renderRouteContext: NuxtRenderRouteContext, context: { event: H3Event; }) => void | Promise<void>; "render:island": (islandResponse: NuxtIslandResponse, context: { event: H3Event; islandContext: NuxtIslandContext; }) => void | Promise<void>; } } declare module "nitropack/types" { interface NitroRuntimeConfig extends RuntimeConfig {} interface NitroRouteConfig { ssr?: boolean; streaming?: boolean; noScripts?: boolean; /** @deprecated Use `noScripts` instead */ experimentalNoScripts?: boolean; } interface NitroRuntimeHooks { "dev:ssr-logs": (ctx: { logs: LogObject[]; path: string; }) => void | Promise<void>; "render:html": (htmlContext: NuxtRenderHTMLContext, context: { event: H3Event; streaming?: boolean; }) => void | Promise<void>; "render:html:chunk": (chunkContext: NuxtRenderChunkContext, context: { event: H3Event; }) => void | Promise<void>; "render:html:close": (closeContext: NuxtRenderCloseContext, context: { event: H3Event; }) => void | Promise<void>; "render:route": (renderRouteContext: NuxtRenderRouteContext, context: { event: H3Event; }) => void | Promise<void>; "render:island": (islandResponse: NuxtIslandResponse, context: { event: H3Event; islandContext: NuxtIslandContext; }) => void | Promise<void>; } } declare module "@nuxt/schema" { interface NuxtHooks { /** * Called when the dev middleware is being registered on the Nitro dev server. * @param handler the Vite or Webpack event handler * @returns Promise */ "server:devHandler": (handler: EventHandler, options: { cors: (path: string) => boolean; }) => HookResult; /** * Called before Nitro writes `.nuxt/tsconfig.server.json`, allowing addition of custom references and declarations. * @param options Objects containing `references`, `declarations` * @param options.references Array of TypeScript references to add * @param options.declarations Array of declaration strings to add * @returns Promise */ "nitro:prepare:types": (options: { references: TSReference[]; declarations: string[]; }) => HookResult; /** * Called before initializing Nitro, allowing customization of Nitro's configuration. * @param nitroConfig The nitro config to be extended * @returns Promise */ "nitro:config": (nitroConfig: NitroConfig) => HookResult; /** * Called after Nitro is initialized, which allows registering Nitro hooks and interacting directly with Nitro. * @param nitro The created nitro object * @returns Promise */ "nitro:init": (nitro: Nitro) => HookResult; /** * Called before building the Nitro instance. * @param nitro The created nitro object * @returns Promise */ "nitro:build:before": (nitro: Nitro) => HookResult; /** * Called after copying public assets. Allows modifying public assets before Nitro server is built. * @param nitro The created nitro object * @returns Promise */ "nitro:build:public-assets": (nitro: Nitro) => HookResult; } interface ConfigSchema { /** * Configuration for Nitro. * * @see [Nitro configuration docs](https://nitro.build/config) */ nitro: NitroConfig; /** * Global route options applied to matching server routes. * * @experimental This is an experimental feature and API may change in the future. * * @see [Nitro route rules documentation](https://nitro.build/config#routerules) */ routeRules: NitroConfig["routeRules"]; /** * Nitro server handlers. * * Each handler accepts the following options: * - handler: The path to the file defining the handler. - route: The route under which the handler is available. This follows the conventions of [rou3](https://github.com/h3js/rou3). - method: The HTTP method of requests that should be handled. - middleware: Specifies whether it is a middleware handler. - lazy: Specifies whether to use lazy loading to import the handler. * * @see [`server/` directory documentation](https://nuxt.com/docs/4.x/directory-structure/server) * * @note Files from `server/api`, `server/middleware` and `server/routes` will be automatically registered by Nuxt. * * @example * ```js * serverHandlers: [ * { route: '/path/foo/**:name', handler: '#server/foohandler.ts' } * ] * ``` */ serverHandlers: NitroEventHandler[]; /** * Nitro development-only server handlers. * * @see [Nitro server routes documentation](https://nitro.build/guide/routing) */ devServerHandlers: NitroDevEventHandler[]; /** * Enable [diagnostics-channel](https://nodejs.org/api/diagnostics_channel.html) * tracing for Nuxt-owned subsystems (the `nuxt.*` channels). * * @experimental Channel names, payload shapes, and option keys may change. * * @see [Untracing naming registry](https://github.com/unjs/untracing) */ tracingChannel: boolean | NuxtTracingChannelOptions; } interface NuxtConfig { nitro?: NitroConfig; } interface RuntimeConfig { app: NitroRuntimeConfigApp; /** Only available on the server. */ nitro?: NitroRuntimeConfig["nitro"]; } interface NuxtDebugOptions { /** Debug options for Nitro */ nitro?: NitroOptions["debug"]; } interface NuxtPage { rules?: NitroRouteConfig; } } declare module "nuxt/schema" { interface NuxtHooks { /** * Called when the dev middleware is being registered on the Nitro dev server. * @param handler the Vite or Webpack event handler * @returns Promise */ "server:devHandler": (handler: EventHandler, options: { cors: (path: string) => boolean; }) => HookResult; /** * Called before Nitro writes `.nuxt/tsconfig.server.json`, allowing addition of custom references and declarations. * @param options Objects containing `references`, `declarations` * @param options.references Array of TypeScript references to add * @param options.declarations Array of declaration strings to add * @returns Promise */ "nitro:prepare:types": (options: { references: TSReference[]; declarations: string[]; }) => HookResult; /** * Called before initializing Nitro, allowing customization of Nitro's configuration. * @param nitroConfig The nitro config to be extended * @returns Promise */ "nitro:config": (nitroConfig: NitroConfig) => HookResult; /** * Called after Nitro is initialized, which allows registering Nitro hooks and interacting directly with Nitro. * @param nitro The created nitro object * @returns Promise */ "nitro:init": (nitro: Nitro) => HookResult; /** * Called before building the Nitro instance. * @param nitro The created nitro object * @returns Promise */ "nitro:build:before": (nitro: Nitro) => HookResult; /** * Called after copying public assets. Allows modifying public assets before Nitro server is built. * @param nitro The created nitro object * @returns Promise */ "nitro:build:public-assets": (nitro: Nitro) => HookResult; } interface ConfigSchema { /** * Configuration for Nitro. * * @see [Nitro configuration docs](https://nitro.build/config) */ nitro: NitroConfig; /** * Global route options applied to matching server routes. * * @experimental This is an experimental feature and API may change in the future. * * @see [Nitro route rules documentation](https://nitro.build/config#routerules) */ routeRules: NitroConfig["routeRules"]; /** * Nitro server handlers. * * Each handler accepts the following options: * - handler: The path to the file defining the handler. - route: The route under which the handler is available. This follows the conventions of [rou3](https://github.com/h3js/rou3). - method: The HTTP method of requests that should be handled. - middleware: Specifies whether it is a middleware handler. - lazy: Specifies whether to use lazy loading to import the handler. * * @see [`server/` directory documentation](https://nuxt.com/docs/4.x/directory-structure/server) * * @note Files from `server/api`, `server/middleware` and `server/routes` will be automatically registered by Nuxt. * * @example * ```js * serverHandlers: [ * { route: '/path/foo/**:name', handler: '~/server/foohandler.ts' } * ] * ``` */ serverHandlers: NitroEventHandler[]; /** * Nitro development-only server handlers. * * @see [Nitro server routes documentation](https://nitro.build/guide/routing) */ devServerHandlers: NitroDevEventHandler[]; /** * Enable [diagnostics-channel](https://nodejs.org/api/diagnostics_channel.html) * tracing for Nuxt-owned subsystems (the `nuxt.*` channels). * * @experimental Channel names, payload shapes, and option keys may change. * * @see [Untracing naming registry](https://github.com/unjs/untracing) */ tracingChannel: boolean | NuxtTracingChannelOptions; } interface NuxtConfig { nitro?: NitroConfig; } interface RuntimeConfig { app: NitroRuntimeConfigApp; /** Only available on the server. */ nitro?: NitroRuntimeConfig["nitro"]; } interface NuxtDebugOptions { /** Debug options for Nitro */ nitro?: NitroOptions["debug"]; } interface NuxtPage { rules?: NitroRouteConfig; } } //#endregion export { NuxtTracingChannelOptions };