new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Nuxt · API · all subjects

kit functions & build

33 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

addServerHandler signature and parameters

addServerHandler adds a Nitro server handler for creating server middleware or custom routes. Signature: function addServerHandler(handler: NitroEventHandler): void. Parameters object has these properties: handler (string, required) - path to event handler; route (string, optional) - path prefix or route, empty string makes it middleware; middleware (boolean, optional) - specifies if this is middleware handler called on every route; lazy (boolean, optional) - use lazy loading to import handler; method (string, optional) - router method matcher, uses handler name as default if containing method name.

addDevServerHandler signature and parameters

addDevServerHandler adds a Nitro server handler for development mode only, excluded from production build. Signature: function addDevServerHandler(handler: NitroDevEventHandler): void. Parameters object has these properties: handler (EventHandler, required) - event handler; route (string, optional) - path prefix or route, empty string makes it middleware.

useNitro signature and usage

useNitro returns the Nitro instance. Signature: function useNitro(): Nitro. Can only be called after the 'ready' hook. Changes to the Nitro instance configuration are not applied.

addServerPlugin signature and parameters

addServerPlugin adds a plugin to extend Nitro's runtime behavior. Signature: function addServerPlugin(plugin: string): void. Parameters: plugin (string, required) - path to the plugin file. The plugin must export a default function that accepts the Nitro instance as an argument. The plugin file must explicitly import definePlugin from 'nitro'.

addPrerenderRoutes signature and parameters

addPrerenderRoutes adds routes to be prerendered to Nitro. Signature: function addPrerenderRoutes(routes: string | string[]): void. Parameters: routes (string | string[], required) - a route or an array of routes to prerender.

addServerImports signature and parameters

addServerImports adds imports to the server, making them available in Nitro without manual importing. Signature: function addServerImports(dirs: NuxtImport | NuxtImport[]): void. Parameters object (can be single or array): name (string, required) - import name to detect; from (string, required) - module specifier; priority (number, optional) - priority of import, highest used if multiple with same name; disabled (boolean, optional) - whether import is disabled; meta (Record<string, any>, optional) - metadata; type (boolean, optional) - if pure type import; typeFrom (string, optional) - from value for type declarations; as (string, optional) - import alias name.

addServerImportsDir signature and parameters

addServerImportsDir adds a directory to be scanned for auto-imports by Nitro. Signature: function addServerImportsDir(dirs: string | string[], opts: { prepend?: boolean }): void. Parameters: dirs (string | string[], required) - directory or array of directories to register for scanning; opts (optional) - options object with prepend (boolean, optional) - if true, adds directory to beginning of scan list.

addServerScanDir signature and parameters

addServerScanDir adds directories to be scanned by Nitro for subdirectories registered like the ~~/server folder. Only ~~/server/api, ~~/server/routes, ~~/server/middleware, and ~~/server/utils are scanned. Signature: function addServerScanDir(dirs: string | string[], opts: { prepend?: boolean }): void. Parameters: dirs (string | string[], required) - directory or array of directories to register as server dirs; opts (optional) - options object with prepend (boolean, optional) - if true, adds directory to beginning of scan list.

addServerHandler example

Example showing addServerHandler usage: importing addServerHandler, createResolver, defineNuxtModule from '@nuxt/kit'. In module setup, resolve runtime/robots.get using createResolver(import.meta.url). Call addServerHandler with route: '/robots.txt' and handler: resolve('./runtime/robots.get'). The handler file imports defineEventHandler from 'nitro/h3' and exports default defineEventHandler returning {body: 'User-agent: *\nDisallow: /'}. Accessing /robots.txt returns the robot exclusion body.

addServerImportsDir example

Example showing addServerImportsDir usage: module.ts imports addServerImportsDir, createResolver, defineNuxtModule from '@nuxt/kit'. In setup, resolve './runtime/server/composables' and call addServerImportsDir with resolved path. File runtime/server/composables/index.ts exports function useApiSecret() that returns useRuntimeConfig().apiSecret. Then useApiSecret can be used directly in runtime/server/api/hello.ts without explicit import.

addServerScanDir example

Example showing addServerScanDir usage: module.ts imports addServerScanDir, createResolver, defineNuxtModule from '@nuxt/kit'. In setup, resolve './runtime/server' and call addServerScanDir with resolved path. File runtime/server/utils/index.ts exports function hello() returning 'Hello from server utils!'. Then hello can be used directly in runtime/server/api/hello.ts without explicit import.

addServerPlugin example with hooks

Example showing addServerPlugin usage: module.ts imports addServerPlugin, createResolver, defineNuxtModule from '@nuxt/kit'. In setup, resolve './runtime/plugin.ts' and call addServerPlugin. Plugin file imports definePlugin from 'nitro' and exports default definePlugin function accepting nitroApp. Hook on 'request' event logs 'on request' and event.req.url. Hook on 'response' event logs 'on response' and response text.

useNuxt function signature

useNuxt() is a Kit utility that returns the Nuxt instance from the context. It will throw an error if Nuxt is not available. The function signature is: function useNuxt(): Nuxt

useNuxt return value properties

The useNuxt function returns a Nuxt instance with the following properties: options (type NuxtOptions) - the resolved Nuxt configuration; hooks (type NuxtHookRegistry<NuxtHooks>) - the Nuxt hook system for registering and listening to lifecycle events; hook (type (name: string, (...args: any[]) => Promise<void> | void) => () => void) - shortcut for nuxt.hooks.hook to register a single callback for a specific lifecycle hook; callHook (type (name: string, ...args: any[]) => Promise<any>) - shortcut for nuxt.hooks.callHook to trigger a lifecycle hook manually; addHooks (type (configHooks: NuxtNestedHooks) => () => void) - shortcut for nuxt.hooks.addHooks to register multiple hooks at once.

tryUseNuxt function signature

tryUseNuxt() is a Kit utility that returns the Nuxt instance from the context if available, or null if Nuxt is not available. The function signature is: function tryUseNuxt(): Nuxt | null

tryUseNuxt return value

The tryUseNuxt function returns the Nuxt instance with the same properties as useNuxt (options, hooks, hook, callHook, addHooks) if available, or null if Nuxt is not available.

useNuxt vs setup function argument

When working with the setup function in Nuxt modules, Nuxt is already provided as the second argument, so you can access it directly without needing to call useNuxt().

addImports function signature

The addImports function registers imports to make them available in the Nuxt application context without explicit importing. Its signature is: function addImports (imports: NuxtImport | NuxtImport[]): void. It accepts a single import object or an array of import objects.

addImports parameters and properties

addImports accepts an object or array of objects with these properties: name (string, required) - import name to be detected; from (string, required) - module specifier to import from; priority (number, optional) - priority of the import, highest priority used when names conflict; disabled (boolean, optional) - if import is disabled; meta (Record<string, any>, optional) - metadata of the import; type (boolean, optional) - if this is a pure type import; typeFrom (string, optional) - use this as the from value when generating type declarations; as (string, optional) - import as this name.

addImportsDir function signature

The addImportsDir function adds imports from a directory to the Nuxt application, automatically importing all files from the directory. Its signature is: function addImportsDir (dirs: string | string[], options?: { prepend?: boolean }): void.

addImportsDir parameters

addImportsDir accepts: dirs (string | string[], required) - a string or array of strings with the path to the directory to import from; options (object, optional) - with property prepend (boolean, optional) - if true, the imports will be prepended to the list of imports.

addImportsSources function signature

The addImportsSources function adds listed imports from 3rd party packages to the Nuxt application. Its signature is: function addImportsSources (importSources: NuxtImportPresetSource | NuxtImportPresetSource[]): void. It accepts a single import source object or an array of import source objects.

addImportsSources NuxtImportPreset parameters

The NuxtImportPreset object in addImportsSources has these properties: from (string, required) - module specifier to import from; imports (array, required) - an array of entries which can be import names, import objects or nested presets of type (NuxtImportEntry | NuxtImportPreset)[].

addImportsSources NuxtPackageImportPreset parameters

The NuxtPackageImportPreset object in addImportsSources has this property: package (string, required) - name of the package to import from.

addImports example usage

Example showing how to register multiple imports from a package: ```ts import { addImports, defineNuxtModule } from '@nuxt/kit' export default defineNuxtModule({ setup (options, nuxt) { const names = [ 'useStoryblok', 'useStoryblokApi', 'useStoryblokBridge', 'renderRichText', 'RichTextSchema', ] names.forEach(name => addImports({ name, as: name, from: '@storyblok/vue' }), ) }, }) ```

addImportsDir example usage

Example showing how to add imports from a directory: ```ts import { addImportsDir, createResolver, defineNuxtModule } from '@nuxt/kit' export default defineNuxtModule({ meta: { name: '@vueuse/motion', configKey: 'motion', }, setup (options, nuxt) { const resolver = createResolver(import.meta.url) addImportsDir(resolver.resolve('./runtime/composables')) }, }) ```

addImportsSources example usage

Example showing how to add listed imports from packages: ```ts import { addImportsSources, defineNuxtModule } from '@nuxt/kit' export default defineNuxtModule({ setup () { addImportsSources([ { package: '@vueuse/core' }, { from: 'h3', imports: [ 'defineEventHandler', 'getQuery', 'getRouterParams', 'readBody', 'sendRedirect', ], }, ]) }, }) ```

Auto-imports powered by unimport

Nuxt auto-imports are powered by unimport library, which provides the underlying auto-import mechanism used in Nuxt.

addImports note about server imports

To add imports for the Nitro server context, use the addServerImports function instead of addImports.

useLogger signature

useLogger is a function that returns a logger instance. Its signature is: function useLogger(tag?: string, options?: NuxtLoggerOptions): NuxtLogger. The tag parameter is optional and is a string to suffix all log messages with, displayed on the right near the timestamp. The options parameter is optional and includes logger options such as level, reporters, defaults and formatOptions.

useLogger basic example

Example of using useLogger in a Nuxt module: ```ts import { defineNuxtModule, useLogger } from '@nuxt/kit' export default defineNuxtModule({ setup (options, nuxt) { const logger = useLogger('my-module') logger.info('Hello from my module!') }, }) ```

useLogger with options example

Example of using useLogger with the level option in NuxtLoggerOptions: ```ts import { defineNuxtModule, useLogger } from '@nuxt/kit' export default defineNuxtModule({ setup (options, nuxt) { const logger = useLogger('my-module', { level: options.quiet ? 0 : 3 }) logger.info('Hello from my module!') }, }) ```

useLogger uses consola under the hood

The useLogger function uses consola as its underlying implementation.

Give your agent this brain