mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
Compare commits
2 Commits
6db6f6c565
...
7e9ff791e7
Author | SHA1 | Date | |
---|---|---|---|
|
7e9ff791e7 | ||
|
aa75b8c320 |
@ -2029,11 +2029,122 @@ export default defineNuxtModule({
|
|||||||
|
|
||||||
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/nitro.ts)
|
[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)`
|
### `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`
|
### `addServerPlugin`
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user