feat(docs): describe useNitro

This commit is contained in:
Andrey Yolkin 2023-09-03 23:33:38 +03:00
parent 6db6f6c565
commit aa75b8c320
No known key found for this signature in database
GPG Key ID: 4A2899263001EA49

View File

@ -2033,7 +2033,62 @@ export default defineNuxtModule({
### `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`