Compare commits

...

2 Commits

Author SHA1 Message Date
Andrey Yolkin 7e9ff791e7
feat: describe `addServerHandler` 2023-09-03 23:45:38 +03:00
Andrey Yolkin aa75b8c320
feat(docs): describe `useNitro` 2023-09-03 23:33:38 +03:00
1 changed files with 113 additions and 2 deletions

View File

@ -2029,11 +2029,122 @@ export default defineNuxtModule({
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/nitro.ts)
### `addServerHandler (handler)`
### `addServerHandler`
Adds a nitro server handler.
#### Type
```ts
function addServerHandler (handler: NitroEventHandler): void
export interface NitroEventHandler {
handler: string;
route?: string;
middleware?: boolean;
lazy?: boolean;
method?: string;
}
```
#### Parameters
##### `handler`
**Type**: `NitroEventHandler`
**Required**: `true`
A handler object with the following properties:
- `handler` (required)
**Type**: `string`
Path to event handler.
- `route` (optional)
**Type**: `string`
Path prefix or route. If an empty string used, will be used as a middleware.
- `middleware` (optional)
**Type**: `boolean`
Specifies this is a middleware handler. Middleware are called on every route and should normally return nothing to pass to the next handlers.
- `lazy` (optional)
**Type**: `boolean`
Use lazy loading to import handler.
- `method` (optional)
**Type**: `string`
Router method matcher.
### `addDevServerHandler (handler)`
### `useNitro()` (only usable after `ready` hook)
### `useNitro`
Returns the Nitro instance.
::alert{type=warning}
You can call `useNitro()` only after `ready` hook.
::
::alert{type=info}
Changes to the Nitro instance configuration are not applied.
::
#### Type
```ts
function useNitro (): Nitro
export interface Nitro {
options: NitroOptions;
scannedHandlers: NitroEventHandler[];
vfs: Record<string, string>;
hooks: Hookable<NitroHooks>;
unimport?: Unimport;
logger: ConsolaInstance;
storage: Storage;
close: () => Promise<void>;
updateConfig: (config: NitroDynamicConfig) => void | Promise<void>;
}
```
#### Examples
```ts
// https://github.com/nuxt/nuxt/blob/4e05650cde31ca73be4d14b1f0d23c7854008749/packages/nuxt/src/core/nuxt.ts#L404
import { defineNuxtModule, useNitro, addPlugin, createResolver } from '@nuxt/kit'
export default defineNuxtModule({
setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url)
nuxt.hook('ready', () => {
const nitro = useNitro()
if (nitro.options.static && nuxt.options.experimental.payloadExtraction === undefined) {
console.warn('Using experimental payload extraction for full-static output. You can opt-out by setting `experimental.payloadExtraction` to `false`.')
nuxt.options.experimental.payloadExtraction = true
}
nitro.options.replace['process.env.NUXT_PAYLOAD_EXTRACTION'] = String(!!nuxt.options.experimental.payloadExtraction)
nitro.options._config.replace!['process.env.NUXT_PAYLOAD_EXTRACTION'] = String(!!nuxt.options.experimental.payloadExtraction)
if (!nuxt.options.dev && nuxt.options.experimental.payloadExtraction) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/payload.client'))
}
})
}
})
```
### `addServerPlugin`