nitro 3.0.1-alpha.2 → 3.0.260415-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +6 -0
- package/README.md +2 -2
- package/dist/THIRD-PARTY-LICENSES.md +1646 -0
- package/dist/_build/common.mjs +7261 -3812
- package/dist/_build/rolldown.mjs +32 -42
- package/dist/_build/rollup.mjs +28 -48
- package/dist/_build/vite.build.mjs +10 -10
- package/dist/_chunks/nitro.mjs +102 -327
- package/dist/_chunks/nitro2.mjs +8 -9
- package/dist/_chunks/nitro3.mjs +136 -0
- package/dist/_chunks/nitro4.mjs +127 -0
- package/dist/_chunks/utils.mjs +40 -29
- package/dist/_common.mjs +13 -25
- package/dist/_dev.d.mts +0 -3
- package/dist/{_chunks/dev.mjs → _dev.mjs} +132 -330
- package/dist/_libs/_.mjs +2 -0
- package/dist/_libs/_2.mjs +2 -0
- package/dist/_libs/_3.mjs +2 -0
- package/dist/_libs/_4.mjs +2 -0
- package/dist/_libs/_5.mjs +2 -0
- package/dist/_libs/_6.mjs +2 -0
- package/dist/_libs/_7.mjs +2 -0
- package/dist/_libs/c12+rc9.mjs +814 -0
- package/dist/_libs/{c12+giget+readdirp+chokidar.d.mts → c12+readdirp+chokidar.d.mts} +49 -40
- package/dist/_libs/citty.mjs +142 -41
- package/dist/_libs/commondir+is-reference.mjs +60 -56
- package/dist/_libs/compatx.d.mts +0 -2
- package/dist/_libs/compatx.mjs +1 -4
- package/dist/_libs/esbuild.d.mts +0 -1
- package/dist/_libs/escape-string-regexp.mjs +1 -4
- package/dist/_libs/{pluginutils+plugin-commonjs.d.mts → estree+pluginutils.d.mts} +0 -5
- package/dist/_libs/estree-walker.mjs +4 -92
- package/dist/_libs/exsolve.d.mts +1 -0
- package/dist/_libs/hasown+resolve+deepmerge.mjs +160 -246
- package/dist/_libs/httpxy.d.mts +20 -13
- package/dist/_libs/httpxy.mjs +519 -95
- package/dist/_libs/js-tokens.d.mts +1 -0
- package/dist/_libs/klona.mjs +1 -4
- package/dist/_libs/magic-string.d.mts +0 -2
- package/dist/_libs/mlly.d.mts +0 -2
- package/dist/_libs/nypm+tinyexec.mjs +822 -0
- package/dist/_libs/perfect-debounce.mjs +68 -0
- package/dist/_libs/pkg-types.d.mts +0 -3
- package/dist/_libs/plugin-alias.mjs +1 -5
- package/dist/_libs/plugin-inject.mjs +1 -7
- package/dist/_libs/plugin-json.mjs +1 -5
- package/dist/_libs/pluginutils.mjs +3 -35
- package/dist/_libs/readdirp+chokidar.mjs +187 -365
- package/dist/_libs/remapping.mjs +1 -5
- package/dist/_libs/resolve-uri+gen-mapping.mjs +15 -53
- package/dist/_libs/rou3.d.mts +1 -4
- package/dist/_libs/rou3.mjs +223 -78
- package/dist/_libs/std-env.d.mts +8 -2
- package/dist/_libs/strip-literal.d.mts +1 -0
- package/dist/_libs/tsconfck.mjs +21 -334
- package/dist/_libs/ultrahtml.mjs +3 -16
- package/dist/_libs/unimport+unplugin.mjs +244 -388
- package/dist/_libs/unplugin+unimport.d.mts +5 -15
- package/dist/_libs/unwasm.d.mts +0 -3
- package/dist/_presets.mjs +576 -488
- package/dist/builder.d.mts +5 -25
- package/dist/builder.mjs +3 -8
- package/dist/cli/_chunks/build.mjs +22 -25
- package/dist/cli/_chunks/common.mjs +1 -4
- package/dist/cli/_chunks/deploy.mjs +37 -0
- package/dist/cli/_chunks/dev.mjs +6 -11
- package/dist/cli/_chunks/docs.mjs +28 -0
- package/dist/cli/_chunks/list.mjs +4 -8
- package/dist/cli/_chunks/prepare.mjs +2 -6
- package/dist/cli/_chunks/preview.mjs +48 -0
- package/dist/cli/_chunks/run.mjs +7 -11
- package/dist/cli/_chunks/task.mjs +1 -5
- package/dist/cli/index.mjs +5 -6
- package/dist/docs/0.docs/0.index/index.md +56 -0
- package/dist/docs/0.docs/1.quick-start.md +110 -0
- package/dist/docs/0.docs/10.lifecycle.md +204 -0
- package/dist/docs/0.docs/11.openapi.md +291 -0
- package/dist/docs/0.docs/12.plugins.md +164 -0
- package/dist/docs/0.docs/13.tasks.md +277 -0
- package/dist/docs/0.docs/14.websocket.md +386 -0
- package/dist/docs/0.docs/15.migration.md +213 -0
- package/dist/docs/0.docs/16.nightly.md +31 -0
- package/dist/docs/0.docs/2.renderer.md +322 -0
- package/dist/docs/0.docs/3.routing.md +711 -0
- package/dist/docs/0.docs/4.server-entry.md +239 -0
- package/dist/docs/0.docs/5.cache.md +408 -0
- package/dist/docs/0.docs/6.storage.md +261 -0
- package/dist/docs/0.docs/7.assets.md +166 -0
- package/dist/docs/0.docs/8.configuration.md +256 -0
- package/dist/docs/0.docs/9.database.md +185 -0
- package/dist/docs/1.deploy/0.index/index.md +64 -0
- package/dist/docs/1.deploy/1.runtimes/0.node.md +63 -0
- package/dist/docs/1.deploy/1.runtimes/1.bun.md +15 -0
- package/dist/docs/1.deploy/1.runtimes/2.deno.md +19 -0
- package/dist/docs/1.deploy/2.providers/0.alwaysdata.md +50 -0
- package/dist/docs/1.deploy/2.providers/1.aws.md +47 -0
- package/dist/docs/1.deploy/2.providers/10.flightcontrol.md +67 -0
- package/dist/docs/1.deploy/2.providers/11.genezio.md +67 -0
- package/dist/docs/1.deploy/2.providers/12.github-pages.md +68 -0
- package/dist/docs/1.deploy/2.providers/13.gitlab-pages.md +37 -0
- package/dist/docs/1.deploy/2.providers/14.heroku.md +84 -0
- package/dist/docs/1.deploy/2.providers/15.iis.md +38 -0
- package/dist/docs/1.deploy/2.providers/16.koyeb.md +109 -0
- package/dist/docs/1.deploy/2.providers/17.netlify.md +47 -0
- package/dist/docs/1.deploy/2.providers/18.platform-sh.md +37 -0
- package/dist/docs/1.deploy/2.providers/19.render.md +37 -0
- package/dist/docs/1.deploy/2.providers/2.aws-amplify.md +81 -0
- package/dist/docs/1.deploy/2.providers/20.stormkit.md +24 -0
- package/dist/docs/1.deploy/2.providers/21.vercel.md +270 -0
- package/dist/docs/1.deploy/2.providers/22.zeabur.md +19 -0
- package/dist/docs/1.deploy/2.providers/23.zephyr.md +99 -0
- package/dist/docs/1.deploy/2.providers/24.zerops.md +102 -0
- package/dist/docs/1.deploy/2.providers/3.azure.md +72 -0
- package/dist/docs/1.deploy/2.providers/4.cleavr.md +33 -0
- package/dist/docs/1.deploy/2.providers/5.cloudflare.md +369 -0
- package/dist/docs/1.deploy/2.providers/6.deno-deploy.md +66 -0
- package/dist/docs/1.deploy/2.providers/7.digitalocean.md +46 -0
- package/dist/docs/1.deploy/2.providers/8.edgeone.md +20 -0
- package/dist/docs/1.deploy/2.providers/9.firebase.md +32 -0
- package/dist/docs/2.config/0.index/index.md +1221 -0
- package/dist/docs/3.examples/0.index/index.md +3 -0
- package/dist/docs/3.examples/1.api-routes.md +145 -0
- package/dist/docs/3.examples/10.hono.md +74 -0
- package/dist/docs/3.examples/11.import-alias.md +97 -0
- package/dist/docs/3.examples/12.middleware.md +91 -0
- package/dist/docs/3.examples/13.mono-jsx.md +68 -0
- package/dist/docs/3.examples/14.nano-jsx.md +70 -0
- package/dist/docs/3.examples/15.plugins.md +91 -0
- package/dist/docs/3.examples/16.renderer.md +113 -0
- package/dist/docs/3.examples/17.runtime-config.md +97 -0
- package/dist/docs/3.examples/18.server-fetch.md +87 -0
- package/dist/docs/3.examples/19.shiki.md +198 -0
- package/dist/docs/3.examples/2.auto-imports.md +92 -0
- package/dist/docs/3.examples/20.virtual-routes.md +74 -0
- package/dist/docs/3.examples/21.vite-nitro-plugin.md +79 -0
- package/dist/docs/3.examples/22.vite-rsc.md +1041 -0
- package/dist/docs/3.examples/23.vite-ssr-html.md +217 -0
- package/dist/docs/3.examples/24.vite-ssr-preact.md +247 -0
- package/dist/docs/3.examples/25.vite-ssr-react.md +226 -0
- package/dist/docs/3.examples/26.vite-ssr-solid.md +256 -0
- package/dist/docs/3.examples/27.vite-ssr-tsr-react.md +442 -0
- package/dist/docs/3.examples/28.vite-ssr-tss-react.md +444 -0
- package/dist/docs/3.examples/29.vite-ssr-vue-router.md +617 -0
- package/dist/docs/3.examples/3.cached-handler.md +81 -0
- package/dist/docs/3.examples/30.vite-trpc.md +335 -0
- package/dist/docs/3.examples/31.websocket.md +282 -0
- package/dist/docs/3.examples/4.custom-error-handler.md +98 -0
- package/dist/docs/3.examples/5.database.md +157 -0
- package/dist/docs/3.examples/6.elysia.md +70 -0
- package/dist/docs/3.examples/7.express.md +78 -0
- package/dist/docs/3.examples/8.fastify.md +79 -0
- package/dist/docs/3.examples/9.hello-world.md +68 -0
- package/dist/docs/README.md +86 -0
- package/dist/docs/index.md +121 -0
- package/dist/node_modules/@poppinss/dumper/build/formatters/html/main.js +8 -0
- package/dist/node_modules/@poppinss/dumper/package.json +1 -1
- package/dist/node_modules/@speed-highlight/core/dist/index.js +4 -4
- package/dist/node_modules/@speed-highlight/core/dist/terminal.js +2 -2
- package/dist/node_modules/@speed-highlight/core/package.json +1 -1
- package/dist/node_modules/cookie-es/dist/index.mjs +299 -250
- package/dist/node_modules/cookie-es/package.json +26 -23
- package/dist/node_modules/croner/dist/croner.js +1 -1
- package/dist/node_modules/croner/package.json +11 -1
- package/dist/node_modules/defu/dist/defu.mjs +2 -2
- package/dist/node_modules/defu/package.json +33 -28
- package/dist/node_modules/rendu/dist/_chunks/libs/cookie-es.mjs +151 -0
- package/dist/node_modules/rendu/dist/index.mjs +5 -112
- package/dist/node_modules/rendu/package.json +25 -24
- package/dist/node_modules/youch/build/helpers-B9BQYaS6.js +23 -0
- package/dist/node_modules/youch/build/index.js +204 -351
- package/dist/node_modules/youch/build/public/error_info/script.js +1 -1
- package/dist/node_modules/youch/build/public_dir-C5bujZKB.js +25 -0
- package/dist/node_modules/youch/build/src/templates/error_cause/main.js +44 -0
- package/dist/node_modules/youch/build/src/templates/error_info/main.js +72 -0
- package/dist/node_modules/youch/build/src/templates/error_metadata/main.js +65 -0
- package/dist/node_modules/youch/build/src/templates/error_stack/main.js +152 -0
- package/dist/node_modules/youch/build/src/templates/error_stack_source/main.js +46 -0
- package/dist/node_modules/youch/build/src/templates/header/main.js +24 -0
- package/dist/node_modules/youch/build/src/templates/layout/main.js +27 -0
- package/dist/node_modules/youch/package.json +35 -32
- package/dist/presets/_nitro/runtime/nitro-dev.mjs +12 -57
- package/dist/presets/_nitro/runtime/nitro-prerenderer.mjs +1 -0
- package/dist/presets/_nitro/runtime/service-worker.mjs +1 -2
- package/dist/presets/aws-lambda/runtime/_utils.mjs +6 -6
- package/dist/presets/aws-lambda/runtime/aws-lambda-streaming.mjs +3 -1
- package/dist/presets/azure/runtime/_utils.mjs +5 -5
- package/dist/presets/azure/runtime/azure-swa.mjs +5 -5
- package/dist/presets/bun/runtime/bun.mjs +9 -6
- package/dist/presets/cloudflare/runtime/_module-handler.mjs +1 -1
- package/dist/presets/cloudflare/runtime/cloudflare-durable.mjs +4 -4
- package/dist/presets/cloudflare/runtime/cloudflare-module.mjs +3 -3
- package/dist/presets/cloudflare/runtime/cloudflare-pages.mjs +2 -2
- package/dist/presets/cloudflare/runtime/plugin.dev.mjs +10 -21
- package/dist/presets/deno/runtime/deno-deploy.mjs +4 -4
- package/dist/presets/deno/runtime/deno-server.mjs +9 -6
- package/dist/presets/edgeone/runtime/edgeone.mjs +10 -0
- package/dist/presets/netlify/runtime/netlify-edge.mjs +2 -3
- package/dist/presets/netlify/runtime/netlify.mjs +1 -1
- package/dist/presets/node/runtime/node-cluster.mjs +5 -4
- package/dist/presets/node/runtime/node-middleware.mjs +1 -1
- package/dist/presets/node/runtime/node-server.mjs +8 -5
- package/dist/presets/stormkit/runtime/stormkit.mjs +1 -2
- package/dist/presets/vercel/runtime/cron-handler.mjs +25 -0
- package/dist/presets/vercel/runtime/queue-handler.mjs +21 -0
- package/dist/presets/vercel/runtime/vercel.node.mjs +3 -3
- package/dist/presets/vercel/runtime/vercel.web.mjs +3 -5
- package/dist/presets/winterjs/runtime/winterjs.mjs +10 -10
- package/dist/presets/zephyr/runtime/server.mjs +3 -0
- package/dist/runtime/internal/app.mjs +12 -11
- package/dist/runtime/internal/cache.d.mts +0 -4
- package/dist/runtime/internal/cache.mjs +32 -256
- package/dist/runtime/internal/context.d.mts +3 -1
- package/dist/runtime/internal/context.mjs +1 -9
- package/dist/runtime/internal/error/dev.d.mts +5 -4
- package/dist/runtime/internal/error/dev.mjs +47 -49
- package/dist/runtime/internal/error/hooks.d.mts +1 -1
- package/dist/runtime/internal/error/prod.d.mts +2 -5
- package/dist/runtime/internal/error/prod.mjs +19 -33
- package/dist/runtime/internal/error/utils.d.mts +4 -4
- package/dist/runtime/internal/meta.d.mts +1 -1
- package/dist/runtime/internal/plugin.d.mts +2 -2
- package/dist/runtime/internal/route-rules.d.mts +11 -4
- package/dist/runtime/internal/route-rules.mjs +15 -5
- package/dist/runtime/internal/routes/dev-tasks.d.mts +3 -2
- package/dist/runtime/internal/routes/dev-tasks.mjs +15 -10
- package/dist/runtime/internal/routes/openapi.mjs +2 -2
- package/dist/runtime/internal/routes/renderer-template.d.mts +1 -1
- package/dist/runtime/internal/routes/renderer-template.dev.d.mts +2 -1
- package/dist/runtime/internal/routes/scalar.mjs +3 -3
- package/dist/runtime/internal/routes/swagger.mjs +7 -4
- package/dist/runtime/internal/runtime-config.d.mts +1 -1
- package/dist/runtime/internal/runtime-config.mjs +2 -2
- package/dist/runtime/internal/static.mjs +5 -4
- package/dist/runtime/internal/task.d.mts +3 -1
- package/dist/runtime/internal/task.mjs +7 -7
- package/dist/runtime/internal/vite/dev-entry.mjs +6 -3
- package/dist/runtime/internal/vite/dev-worker.mjs +264 -0
- package/dist/runtime/meta.d.mts +1 -0
- package/dist/runtime/meta.mjs +2 -0
- package/dist/runtime/nitro.d.mts +2 -0
- package/dist/runtime/nitro.mjs +4 -2
- package/dist/runtime/virtual/error-handler.mjs +1 -1
- package/dist/runtime/virtual/renderer-template.mjs +1 -1
- package/dist/runtime/virtual/server-assets.d.mts +2 -1
- package/dist/runtime/virtual/tracing.d.mts +5 -0
- package/dist/runtime/virtual/tracing.mjs +1 -0
- package/dist/runtime/vite.d.mts +1 -1
- package/dist/types/index.d.mts +215 -195
- package/dist/types/index.mjs +1 -11
- package/dist/vite.d.mts +3 -8
- package/dist/vite.mjs +285 -253
- package/lib/tsconfig.json +3 -5
- package/package.json +97 -80
- package/dist/_libs/confbox.mjs +0 -2509
- package/dist/_libs/nypm+giget+tinyexec.mjs +0 -3603
- package/dist/_libs/rc9+c12+dotenv.mjs +0 -972
- package/dist/node_modules/hookable/dist/index.mjs +0 -266
- package/dist/node_modules/hookable/package.json +0 -52
- package/dist/node_modules/youch/build/chunk-4L7RY2JA.js +0 -42
- package/dist/node_modules/youch/build/chunk-7QV3D5YX.js +0 -83
- package/dist/node_modules/youch/build/chunk-AUGPHE32.js +0 -39
- package/dist/node_modules/youch/build/chunk-CM7DWJNZ.js +0 -44
- package/dist/node_modules/youch/build/chunk-EJH674NB.js +0 -223
- package/dist/node_modules/youch/build/chunk-OIJ3WD7L.js +0 -81
- package/dist/node_modules/youch/build/chunk-P36L72PL.js +0 -87
- package/dist/node_modules/youch/build/chunk-PE3GG3TN.js +0 -65
- package/dist/node_modules/youch/build/chunk-X53OIOJH.js +0 -70
- package/dist/presets/_nitro/runtime/nitro-dev.d.mts +0 -2
- package/dist/presets/_nitro/runtime/nitro-prerenderer.d.mts +0 -3
- package/dist/presets/_nitro/runtime/service-worker.d.mts +0 -2
- package/dist/presets/aws-amplify/runtime/aws-amplify.d.mts +0 -2
- package/dist/presets/aws-lambda/runtime/_utils.d.mts +0 -8
- package/dist/presets/aws-lambda/runtime/aws-lambda-streaming.d.mts +0 -2
- package/dist/presets/aws-lambda/runtime/aws-lambda.d.mts +0 -3
- package/dist/presets/azure/runtime/_utils.d.mts +0 -2
- package/dist/presets/azure/runtime/azure-swa.d.mts +0 -5
- package/dist/presets/bun/runtime/bun.d.mts +0 -3
- package/dist/presets/cloudflare/runtime/_module-handler.d.mts +0 -16
- package/dist/presets/cloudflare/runtime/cloudflare-durable.d.mts +0 -11
- package/dist/presets/cloudflare/runtime/cloudflare-module.d.mts +0 -3
- package/dist/presets/cloudflare/runtime/cloudflare-pages.d.mts +0 -20
- package/dist/presets/cloudflare/runtime/plugin.dev.d.mts +0 -3
- package/dist/presets/deno/runtime/deno-deploy.d.mts +0 -5
- package/dist/presets/deno/runtime/deno-server.d.mts +0 -3
- package/dist/presets/netlify/runtime/netlify-edge.d.mts +0 -3
- package/dist/presets/netlify/runtime/netlify.d.mts +0 -4
- package/dist/presets/node/runtime/node-cluster.d.mts +0 -3
- package/dist/presets/node/runtime/node-middleware.d.mts +0 -3
- package/dist/presets/node/runtime/node-server.d.mts +0 -3
- package/dist/presets/standard/runtime/server.d.mts +0 -3
- package/dist/presets/stormkit/runtime/stormkit.d.mts +0 -21
- package/dist/presets/vercel/runtime/isr.d.mts +0 -2
- package/dist/presets/vercel/runtime/vercel.node.d.mts +0 -3
- package/dist/presets/vercel/runtime/vercel.web.d.mts +0 -8
- package/dist/presets/winterjs/runtime/winterjs.d.mts +0 -2
- package/dist/presets/zeabur/runtime/zeabur.d.mts +0 -3
- package/dist/runtime/internal/vite/node-runner.mjs +0 -359
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
# KV Storage
|
|
2
|
+
|
|
3
|
+
> Nitro provides a built-in storage layer that can abstract filesystem or database or any other data source.
|
|
4
|
+
|
|
5
|
+
Nitro has built-in integration with [unstorage](https://unstorage.unjs.io) to provide a runtime agnostic persistent layer.
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
To use the storage layer, you can use the `useStorage()` utility to access the storage instance.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { useStorage } from "nitro/storage";
|
|
13
|
+
|
|
14
|
+
// Default storage (in-memory)
|
|
15
|
+
await useStorage().setItem("test:foo", { hello: "world" });
|
|
16
|
+
const value = await useStorage().getItem("test:foo");
|
|
17
|
+
|
|
18
|
+
// You can specify a base prefix with useStorage(base)
|
|
19
|
+
const testStorage = useStorage("test");
|
|
20
|
+
await testStorage.setItem("foo", { hello: "world" });
|
|
21
|
+
await testStorage.getItem("foo"); // { hello: "world" }
|
|
22
|
+
|
|
23
|
+
// You can use generics to type the return value
|
|
24
|
+
await useStorage<{ hello: string }>("test").getItem("foo");
|
|
25
|
+
await useStorage("test").getItem<{ hello: string }>("foo");
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
<read-more></read-more>
|
|
29
|
+
|
|
30
|
+
### Available methods
|
|
31
|
+
|
|
32
|
+
The storage instance returned by `useStorage()` provides the following methods:
|
|
33
|
+
|
|
34
|
+
| Method | Description |
|
|
35
|
+
| --- | --- |
|
|
36
|
+
| `getItem(key)` | Get the value of a key. Returns `null` if the key does not exist. |
|
|
37
|
+
| `getItems(items)` | Get multiple items at once. Accepts an array of keys or `{ key, options }` objects. |
|
|
38
|
+
| `getItemRaw(key)` | Get the raw value of a key without parsing. Useful for binary data. |
|
|
39
|
+
| `setItem(key, value)` | Set the value of a key. |
|
|
40
|
+
| `setItems(items)` | Set multiple items at once. Accepts an array of `{ key, value }` objects. |
|
|
41
|
+
| `setItemRaw(key, value)` | Set the raw value of a key without serialization. |
|
|
42
|
+
| `hasItem(key)` | Check if a key exists. Returns a boolean. |
|
|
43
|
+
| `removeItem(key)` | Remove a key from storage. |
|
|
44
|
+
| `getKeys(base?)` | Get all keys, optionally filtered by a base prefix. |
|
|
45
|
+
| `clear(base?)` | Clear all keys, optionally filtered by a base prefix. |
|
|
46
|
+
| `getMeta(key)` | Get metadata for a key (e.g., `mtime`, `atime`, `ttl`). |
|
|
47
|
+
| `setMeta(key, meta)` | Set metadata for a key. |
|
|
48
|
+
| `removeMeta(key)` | Remove metadata for a key. |
|
|
49
|
+
| `mount(base, driver)` | Dynamically mount a storage driver at a base path. |
|
|
50
|
+
| `unmount(base)` | Unmount a storage driver from a base path. |
|
|
51
|
+
| `watch(callback)` | Watch for changes. Callback receives `(event, key)` where event is `"update"` or `"remove"`. |
|
|
52
|
+
| `unwatch()` | Stop watching for changes. |
|
|
53
|
+
|
|
54
|
+
Shorthand aliases are also available: `get`, `set`, `has`, `del`, `remove`, `keys`.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { useStorage } from "nitro/storage";
|
|
58
|
+
|
|
59
|
+
// Get all keys under a prefix
|
|
60
|
+
const keys = await useStorage("test").getKeys();
|
|
61
|
+
|
|
62
|
+
// Check if a key exists
|
|
63
|
+
const exists = await useStorage().hasItem("test:foo");
|
|
64
|
+
|
|
65
|
+
// Remove a key
|
|
66
|
+
await useStorage().removeItem("test:foo");
|
|
67
|
+
|
|
68
|
+
// Get raw binary data
|
|
69
|
+
const raw = await useStorage().getItemRaw("assets/server:image.png");
|
|
70
|
+
|
|
71
|
+
// Get metadata (type, etag, mtime, etc.)
|
|
72
|
+
const meta = await useStorage("assets/server").getMeta("file.txt");
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Configuration
|
|
76
|
+
|
|
77
|
+
You can mount one or multiple custom storage drivers using the `storage` option.
|
|
78
|
+
|
|
79
|
+
The key is the mount point name, and the value is the driver name and configuration.
|
|
80
|
+
|
|
81
|
+
```ts [nitro.config.ts]
|
|
82
|
+
import { defineConfig } from "nitro";
|
|
83
|
+
|
|
84
|
+
export default defineConfig({
|
|
85
|
+
storage: {
|
|
86
|
+
redis: {
|
|
87
|
+
driver: "redis",
|
|
88
|
+
/* redis connector options */
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Then, you can use the redis storage using the `useStorage("redis")` function.
|
|
95
|
+
|
|
96
|
+
<read-more>
|
|
97
|
+
|
|
98
|
+
You can find the driver list on [unstorage documentation](https://unstorage.unjs.io/) with their configuration.
|
|
99
|
+
</read-more>
|
|
100
|
+
|
|
101
|
+
### Development storage
|
|
102
|
+
|
|
103
|
+
You can use the `devStorage` option to override storage configuration during development and prerendering.
|
|
104
|
+
|
|
105
|
+
This is useful when your production driver is not available in development (e.g., a managed Redis instance).
|
|
106
|
+
|
|
107
|
+
```ts [nitro.config.ts]
|
|
108
|
+
import { defineConfig } from "nitro";
|
|
109
|
+
|
|
110
|
+
export default defineConfig({
|
|
111
|
+
storage: {
|
|
112
|
+
db: {
|
|
113
|
+
driver: "redis",
|
|
114
|
+
host: "prod.example.com",
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
devStorage: {
|
|
118
|
+
db: {
|
|
119
|
+
driver: "fs",
|
|
120
|
+
base: "./.data/db"
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
When running in development mode, `devStorage` mounts are merged on top of `storage` mounts, allowing you to use a local filesystem driver or an in-memory driver while developing.
|
|
127
|
+
|
|
128
|
+
## Built-in mount points
|
|
129
|
+
|
|
130
|
+
Nitro automatically mounts the following storage paths:
|
|
131
|
+
|
|
132
|
+
### `/assets`
|
|
133
|
+
|
|
134
|
+
Server assets are mounted at the `/assets` base path. This mount point provides read-only access to bundled server assets (see [Server assets](#server-assets)).
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
import { useStorage } from "nitro/storage";
|
|
138
|
+
|
|
139
|
+
// Access server assets via the /assets mount
|
|
140
|
+
const content = await useStorage("assets/server").getItem("my-file.txt");
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Default (in-memory)
|
|
144
|
+
|
|
145
|
+
The root storage (without a base path) uses an in-memory driver by default. Data stored here is not persisted across restarts.
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
import { useStorage } from "nitro/storage";
|
|
149
|
+
|
|
150
|
+
// In-memory by default, not persisted
|
|
151
|
+
await useStorage().setItem("counter", 1);
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
To persist data, mount a driver with a persistent backend (e.g., `fs`, `redis`, etc.) using the `storage` configuration option.
|
|
155
|
+
|
|
156
|
+
## Server assets
|
|
157
|
+
|
|
158
|
+
Nitro allows you to bundle files from an `assets/` directory at the root of your project. These files are accessible at runtime via the `assets/server` storage mount.
|
|
159
|
+
|
|
160
|
+
```
|
|
161
|
+
my-project/
|
|
162
|
+
assets/
|
|
163
|
+
data.json
|
|
164
|
+
templates/
|
|
165
|
+
welcome.html
|
|
166
|
+
server/
|
|
167
|
+
routes/
|
|
168
|
+
index.ts
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
```ts [server/routes/index.ts]
|
|
172
|
+
import { useStorage } from "nitro/storage";
|
|
173
|
+
|
|
174
|
+
export default defineHandler(async () => {
|
|
175
|
+
const serverAssets = useStorage("assets/server");
|
|
176
|
+
|
|
177
|
+
const keys = await serverAssets.getKeys();
|
|
178
|
+
const data = await serverAssets.getItem("data.json");
|
|
179
|
+
const template = await serverAssets.getItem("templates/welcome.html");
|
|
180
|
+
|
|
181
|
+
return { keys, data, template };
|
|
182
|
+
});
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Custom asset directories
|
|
186
|
+
|
|
187
|
+
You can register additional asset directories using the `serverAssets` config option:
|
|
188
|
+
|
|
189
|
+
```ts [nitro.config.ts]
|
|
190
|
+
import { defineConfig } from "nitro";
|
|
191
|
+
|
|
192
|
+
export default defineConfig({
|
|
193
|
+
serverAssets: [
|
|
194
|
+
{
|
|
195
|
+
baseName: "templates",
|
|
196
|
+
dir: "./templates",
|
|
197
|
+
}
|
|
198
|
+
]
|
|
199
|
+
})
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Custom asset directories are accessible under `assets/<baseName>`:
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
import { useStorage } from "nitro/storage";
|
|
206
|
+
|
|
207
|
+
const templates = useStorage("assets/templates");
|
|
208
|
+
const keys = await templates.getKeys();
|
|
209
|
+
const html = await templates.getItem("email.html");
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Asset metadata
|
|
213
|
+
|
|
214
|
+
Server assets include metadata such as content type, ETag, and modification time:
|
|
215
|
+
|
|
216
|
+
```ts
|
|
217
|
+
import { useStorage } from "nitro/storage";
|
|
218
|
+
|
|
219
|
+
const serverAssets = useStorage("assets/server");
|
|
220
|
+
|
|
221
|
+
const meta = await serverAssets.getMeta("image.png");
|
|
222
|
+
// { type: "image/png", etag: "\"...\"", mtime: "2024-01-01T00:00:00.000Z" }
|
|
223
|
+
|
|
224
|
+
// Useful for setting response headers
|
|
225
|
+
const raw = await serverAssets.getItemRaw("image.png");
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
<note>
|
|
229
|
+
|
|
230
|
+
In development, server assets are read directly from the filesystem. In production, they are bundled and inlined into the build output.
|
|
231
|
+
</note>
|
|
232
|
+
|
|
233
|
+
## Runtime configuration
|
|
234
|
+
|
|
235
|
+
In scenarios where the mount point configuration is not known until runtime, Nitro can dynamically add mount points during startup using [plugins](/docs/plugins).
|
|
236
|
+
|
|
237
|
+
```ts [plugins/storage.ts]
|
|
238
|
+
import { useStorage } from "nitro/storage";
|
|
239
|
+
import { definePlugin } from "nitro";
|
|
240
|
+
import redisDriver from "unstorage/drivers/redis";
|
|
241
|
+
|
|
242
|
+
export default definePlugin(() => {
|
|
243
|
+
const storage = useStorage()
|
|
244
|
+
|
|
245
|
+
// Dynamically pass in credentials from runtime configuration, or other sources
|
|
246
|
+
const driver = redisDriver({
|
|
247
|
+
base: "redis",
|
|
248
|
+
host: process.env.REDIS_HOST,
|
|
249
|
+
port: process.env.REDIS_PORT,
|
|
250
|
+
/* other redis connector options */
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
// Mount driver
|
|
254
|
+
storage.mount("redis", driver)
|
|
255
|
+
})
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
<warning>
|
|
259
|
+
|
|
260
|
+
This is a temporary workaround, with a better solution coming in the future! Keep a lookout on the GitHub issue [here](https://github.com/nitrojs/nitro/issues/1161#issuecomment-1511444675).
|
|
261
|
+
</warning>
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# Assets
|
|
2
|
+
|
|
3
|
+
Nitro supports two types of assets: **public assets** served directly to clients and **server assets** bundled into the server for programmatic access.
|
|
4
|
+
|
|
5
|
+
## Public Assets
|
|
6
|
+
|
|
7
|
+
Nitro handles assets via the `public/` directory.
|
|
8
|
+
|
|
9
|
+
All assets in `public/` directory will be automatically served. This means that you can access them directly from the browser without any special configuration.
|
|
10
|
+
|
|
11
|
+
```md
|
|
12
|
+
public/
|
|
13
|
+
image.png <-- /image.png
|
|
14
|
+
video.mp4 <-- /video.mp4
|
|
15
|
+
robots.txt <-- /robots.txt
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### Caching and Headers
|
|
19
|
+
|
|
20
|
+
Public assets are served with automatic `ETag` and `Last-Modified` headers for conditional requests. When the client sends `If-None-Match` or `If-Modified-Since` headers, Nitro returns a `304 Not Modified` response.
|
|
21
|
+
|
|
22
|
+
For assets served from a non-root `baseURL` (such as `/build/`), Nitro prevents fallthrough to application handlers. If a request matches a public asset base but the file is not found, a `404` is returned immediately.
|
|
23
|
+
|
|
24
|
+
### Production Public Assets
|
|
25
|
+
|
|
26
|
+
When building your Nitro app, the `public/` directory will be copied to `.output/public/` and a manifest with metadata will be created and embedded in the server bundle.
|
|
27
|
+
|
|
28
|
+
```json
|
|
29
|
+
{
|
|
30
|
+
"/image.png": {
|
|
31
|
+
"type": "image/png",
|
|
32
|
+
"etag": "\"4a0c-6utWq0Kbk5OqDmksYCa9XV8irnM\"",
|
|
33
|
+
"mtime": "2023-03-04T21:39:45.086Z",
|
|
34
|
+
"size": 18956
|
|
35
|
+
},
|
|
36
|
+
"/robots.txt": {
|
|
37
|
+
"type": "text/plain; charset=utf-8",
|
|
38
|
+
"etag": "\"8-hMqyDrA8fJ0R904zgEPs3L55Jls\"",
|
|
39
|
+
"mtime": "2023-03-04T21:39:45.086Z",
|
|
40
|
+
"size": 8
|
|
41
|
+
},
|
|
42
|
+
"/video.mp4": {
|
|
43
|
+
"type": "video/mp4",
|
|
44
|
+
"etag": "\"9b943-4UwfQXKUjPCesGPr6J5j7GzNYGU\"",
|
|
45
|
+
"mtime": "2023-03-04T21:39:45.085Z",
|
|
46
|
+
"size": 637251
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
This allows Nitro to know the public assets without scanning the directory, giving high performance with caching headers.
|
|
52
|
+
|
|
53
|
+
### Custom Public Asset Directories
|
|
54
|
+
|
|
55
|
+
You can configure additional public asset directories using the `publicAssets` config option. Each entry supports the following properties:
|
|
56
|
+
|
|
57
|
+
- `dir` -- Path to the directory (resolved relative to `rootDir`).
|
|
58
|
+
- `baseURL` -- URL prefix for serving assets (default: `"/"`).
|
|
59
|
+
- `maxAge` -- Cache `max-age` in seconds. When set, a `Cache-Control: public, max-age=<value>, immutable` header is applied via route rules.
|
|
60
|
+
- `fallthrough` -- Whether requests should fall through to application handlers when the asset is not found. Top-level (`baseURL: "/"`) directories default to `true`; non-root directories default to `false`.
|
|
61
|
+
- `ignore` -- Pass `false` to disable ignore patterns, or an array of glob patterns to override the global `ignore` option.
|
|
62
|
+
|
|
63
|
+
```js [nitro.config.ts]
|
|
64
|
+
import { defineConfig } from "nitro";
|
|
65
|
+
|
|
66
|
+
export default defineConfig({
|
|
67
|
+
publicAssets: [
|
|
68
|
+
{
|
|
69
|
+
baseURL: "build",
|
|
70
|
+
dir: "public/build",
|
|
71
|
+
maxAge: 3600,
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
In this example, files in `public/build/` are served under `/build/` with a one-hour cache and no fallthrough to application handlers.
|
|
77
|
+
|
|
78
|
+
### Compressed Public Assets
|
|
79
|
+
|
|
80
|
+
Nitro can generate pre-compressed versions of your public assets during the build. When a client sends an `Accept-Encoding` header, the server will serve the compressed version if available. Supported encodings are gzip (`.gz`), brotli (`.br`), and zstd (`.zst`).
|
|
81
|
+
|
|
82
|
+
Set `compressPublicAssets: true` to enable all encodings:
|
|
83
|
+
|
|
84
|
+
```js [nitro.config.ts]
|
|
85
|
+
import { defineConfig } from "nitro";
|
|
86
|
+
|
|
87
|
+
export default defineConfig({
|
|
88
|
+
compressPublicAssets: true,
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Or pick specific encodings:
|
|
93
|
+
|
|
94
|
+
```js [nitro.config.ts]
|
|
95
|
+
import { defineConfig } from "nitro";
|
|
96
|
+
|
|
97
|
+
export default defineConfig({
|
|
98
|
+
compressPublicAssets: {
|
|
99
|
+
gzip: true,
|
|
100
|
+
brotli: true,
|
|
101
|
+
zstd: false,
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
> [!NOTE]
|
|
107
|
+
> Only compressible MIME types (text, JavaScript, JSON, XML, WASM, fonts, SVG, etc.) with a file size of at least 1 KB are compressed. Source map files (`.map`) are excluded.
|
|
108
|
+
|
|
109
|
+
## Server Assets
|
|
110
|
+
|
|
111
|
+
All assets in `assets/` directory will be added to the server bundle. After building your application, you can find them in the `.output/server/chunks/raw/` directory. Be careful with the size of your assets, as they will be bundled with the server bundle.
|
|
112
|
+
|
|
113
|
+
> [!TIP]
|
|
114
|
+
> Unless using `useStorage()`, assets won't be included in the server bundle.
|
|
115
|
+
|
|
116
|
+
They can be addressed by the `assets:server` mount point using the [storage layer](/docs/storage).
|
|
117
|
+
|
|
118
|
+
For example, you could store a json file in `assets/data.json` and retrieve it in your handler:
|
|
119
|
+
|
|
120
|
+
```js
|
|
121
|
+
import { defineHandler } from "nitro";
|
|
122
|
+
|
|
123
|
+
export default defineHandler(async () => {
|
|
124
|
+
const data = await useStorage("assets:server").get("data.json");
|
|
125
|
+
|
|
126
|
+
return data;
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Custom Server Assets
|
|
131
|
+
|
|
132
|
+
In order to add assets from a custom directory, you will need to define a path in your nitro config. This allows you to add assets from a directory outside of the `assets/` directory.
|
|
133
|
+
|
|
134
|
+
Each entry in `serverAssets` supports the following properties:
|
|
135
|
+
|
|
136
|
+
- `baseName` -- Name used as the storage mount point (accessed via `assets:<baseName>`).
|
|
137
|
+
- `dir` -- Path to the directory (resolved relative to `rootDir`).
|
|
138
|
+
- `pattern` -- Glob pattern for file inclusion (default: `"**/*"`).
|
|
139
|
+
- `ignore` -- Array of glob patterns to exclude files.
|
|
140
|
+
|
|
141
|
+
```js [nitro.config.ts]
|
|
142
|
+
import { defineConfig } from "nitro";
|
|
143
|
+
|
|
144
|
+
export default defineConfig({
|
|
145
|
+
serverAssets: [
|
|
146
|
+
{
|
|
147
|
+
baseName: "templates",
|
|
148
|
+
dir: "./templates",
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
});
|
|
152
|
+
```
|
|
153
|
+
Then you can use the `assets:templates` base to retrieve your assets.
|
|
154
|
+
|
|
155
|
+
```ts [handlers/success.ts]
|
|
156
|
+
import { defineHandler } from "nitro";
|
|
157
|
+
|
|
158
|
+
export default defineHandler(async (event) => {
|
|
159
|
+
const html = await useStorage("assets:templates").get("success.html");
|
|
160
|
+
|
|
161
|
+
return html;
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
> [!TIP]
|
|
166
|
+
> During development, server assets are read directly from the filesystem using the `fs` unstorage driver. In production, they are bundled into the server as lazy imports with pre-computed metadata (MIME type, ETag, modification time).
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
# Configuration
|
|
2
|
+
|
|
3
|
+
> Customize and extend Nitro defaults.
|
|
4
|
+
|
|
5
|
+
<read-more>
|
|
6
|
+
|
|
7
|
+
See [config reference](/config) for available options.
|
|
8
|
+
</read-more>
|
|
9
|
+
|
|
10
|
+
## Config file
|
|
11
|
+
|
|
12
|
+
You can customize your Nitro builder with a configuration file.
|
|
13
|
+
|
|
14
|
+
```ts [nitro.config.ts]
|
|
15
|
+
import { defineConfig } from "nitro";
|
|
16
|
+
|
|
17
|
+
export default defineConfig({
|
|
18
|
+
// Nitro options
|
|
19
|
+
})
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```ts [vite.config.ts]
|
|
23
|
+
import { defineConfig } from 'vite'
|
|
24
|
+
import { nitro } from 'nitro/vite'
|
|
25
|
+
|
|
26
|
+
export default defineConfig({
|
|
27
|
+
plugins: [
|
|
28
|
+
nitro()
|
|
29
|
+
],
|
|
30
|
+
nitro: {
|
|
31
|
+
// Nitro options
|
|
32
|
+
}
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
> [!TIP]
|
|
38
|
+
> Nitro loads the configuration using [c12](https://github.com/unjs/c12), giving more possibilities such as using `.nitrorc` file in current working directory or in the user's home directory.
|
|
39
|
+
|
|
40
|
+
### Environment-specific config
|
|
41
|
+
|
|
42
|
+
Using [c12](https://github.com/unjs/c12) conventions, you can provide environment-specific overrides using `$development` and `$production` keys:
|
|
43
|
+
|
|
44
|
+
```ts [nitro.config.ts]
|
|
45
|
+
import { defineConfig } from "nitro";
|
|
46
|
+
|
|
47
|
+
export default defineConfig({
|
|
48
|
+
logLevel: 3,
|
|
49
|
+
$development: {
|
|
50
|
+
// Options applied only in development mode
|
|
51
|
+
debug: true,
|
|
52
|
+
},
|
|
53
|
+
$production: {
|
|
54
|
+
// Options applied only in production builds
|
|
55
|
+
minify: true,
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The environment name is `"development"` during `nitro dev` and `"production"` during `nitro build`.
|
|
61
|
+
|
|
62
|
+
### Extending configs
|
|
63
|
+
|
|
64
|
+
You can extend from other configs or presets using the `extends` key:
|
|
65
|
+
|
|
66
|
+
```ts [nitro.config.ts]
|
|
67
|
+
import { defineConfig } from "nitro";
|
|
68
|
+
|
|
69
|
+
export default defineConfig({
|
|
70
|
+
extends: "./base.config",
|
|
71
|
+
})
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Config from `package.json`
|
|
75
|
+
|
|
76
|
+
You can also provide Nitro configuration under the `nitro` key in your `package.json` file.
|
|
77
|
+
|
|
78
|
+
## Directory options
|
|
79
|
+
|
|
80
|
+
Nitro provides several options for controlling directory structure:
|
|
81
|
+
|
|
82
|
+
| Option | Default | Description |
|
|
83
|
+
| --- | --- | --- |
|
|
84
|
+
| `rootDir` | `.` (current directory) | The root directory of the project. |
|
|
85
|
+
| `serverDir` | `false` | Server source directory (set to `"server"` or `"./"` to enable). |
|
|
86
|
+
| `buildDir` | `node_modules/.nitro` | Directory for build artifacts. |
|
|
87
|
+
| `output.dir` | `.output` | Production output directory. |
|
|
88
|
+
| `output.serverDir` | `.output/server` | Server output directory. |
|
|
89
|
+
| `output.publicDir` | `.output/public` | Public assets output directory. |
|
|
90
|
+
|
|
91
|
+
```ts [nitro.config.ts]
|
|
92
|
+
import { defineConfig } from "nitro";
|
|
93
|
+
|
|
94
|
+
export default defineConfig({
|
|
95
|
+
serverDir: "server",
|
|
96
|
+
buildDir: "node_modules/.nitro",
|
|
97
|
+
output: {
|
|
98
|
+
dir: ".output",
|
|
99
|
+
},
|
|
100
|
+
})
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
> [!NOTE]
|
|
104
|
+
> The `srcDir` option is deprecated. Use `serverDir` instead.
|
|
105
|
+
|
|
106
|
+
## Environment variables
|
|
107
|
+
|
|
108
|
+
Certain Nitro behaviors can be configured using environment variables:
|
|
109
|
+
|
|
110
|
+
| Variable | Description |
|
|
111
|
+
| --- | --- |
|
|
112
|
+
| `NITRO_PRESET` | Override the deployment preset. |
|
|
113
|
+
| `NITRO_COMPATIBILITY_DATE` | Set the compatibility date. |
|
|
114
|
+
| `NITRO_APP_BASE_URL` | Override the base URL (default: `/`). |
|
|
115
|
+
|
|
116
|
+
## Runtime configuration
|
|
117
|
+
|
|
118
|
+
Nitro provides a runtime config API to expose configuration within your application, with the ability to update it at runtime by setting environment variables. This is useful when you want to expose different configuration values for different environments (e.g. development, staging, production). For example, you can use this to expose different API endpoints for different environments or to expose different feature flags.
|
|
119
|
+
|
|
120
|
+
First, you need to define the runtime config in your configuration file.
|
|
121
|
+
|
|
122
|
+
```ts [nitro.config.ts]
|
|
123
|
+
import { defineConfig } from "nitro";
|
|
124
|
+
|
|
125
|
+
export default defineConfig({
|
|
126
|
+
runtimeConfig: {
|
|
127
|
+
apiToken: "dev_token", // `dev_token` is the default value
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
You can now access the runtime config using `useRuntimeConfig()`.
|
|
133
|
+
|
|
134
|
+
```ts [api/example.get.ts]
|
|
135
|
+
import { defineHandler } from "nitro";
|
|
136
|
+
import { useRuntimeConfig } from "nitro/runtime-config";
|
|
137
|
+
|
|
138
|
+
export default defineHandler((event) => {
|
|
139
|
+
return useRuntimeConfig().apiToken; // Returns `dev_token`
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Nested objects
|
|
144
|
+
|
|
145
|
+
Runtime config supports nested objects. Keys at any depth are mapped to environment variables using the `NITRO_` prefix and `UPPER_SNAKE_CASE` conversion:
|
|
146
|
+
|
|
147
|
+
```ts [nitro.config.ts]
|
|
148
|
+
import { defineConfig } from "nitro";
|
|
149
|
+
|
|
150
|
+
export default defineConfig({
|
|
151
|
+
runtimeConfig: {
|
|
152
|
+
database: {
|
|
153
|
+
host: "localhost",
|
|
154
|
+
port: 5432,
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
```bash [.env]
|
|
161
|
+
NITRO_DATABASE_HOST="db.example.com"
|
|
162
|
+
NITRO_DATABASE_PORT="5433"
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
> [!NOTE]
|
|
166
|
+
> Only keys defined in `runtimeConfig` in your config file will be considered. You cannot introduce new keys using environment variables alone.
|
|
167
|
+
|
|
168
|
+
### Serialization
|
|
169
|
+
|
|
170
|
+
Runtime config values must be serializable (strings, numbers, booleans, plain objects, and arrays). Non-serializable values (class instances, functions, etc.) will trigger a warning at build time.
|
|
171
|
+
|
|
172
|
+
Values that are `undefined` or `null` in the config are replaced with empty strings (`""`) as a fallback.
|
|
173
|
+
|
|
174
|
+
### Local development
|
|
175
|
+
|
|
176
|
+
You can update the runtime config using environment variables. You can use a `.env` or `.env.local` file in development and use platform variables in production (see below).
|
|
177
|
+
|
|
178
|
+
Create an `.env` file in your project root:
|
|
179
|
+
|
|
180
|
+
```bash [.env]
|
|
181
|
+
NITRO_API_TOKEN="123"
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Re-start the development server, fetch the `/api/example` endpoint and you should see `123` as the response instead of `dev_token`.
|
|
185
|
+
|
|
186
|
+
> [!NOTE]
|
|
187
|
+
> The `.env` and `.env.local` files are only loaded during development (`nitro dev`). In production, use your platform's native environment variable mechanism.
|
|
188
|
+
|
|
189
|
+
Do not forget that you can still universally access environment variables using `import.meta.env` or `process.env` but avoid using them in ambient global contexts to prevent unexpected behavior.
|
|
190
|
+
|
|
191
|
+
### Production
|
|
192
|
+
|
|
193
|
+
You can define variables in your production environment to update the runtime config.
|
|
194
|
+
|
|
195
|
+
<warning>
|
|
196
|
+
|
|
197
|
+
All variables must be prefixed with `NITRO_` to be applied to the runtime config. They will override the runtime config variables defined within your `nitro.config.ts` file.
|
|
198
|
+
</warning>
|
|
199
|
+
|
|
200
|
+
```bash [.env]
|
|
201
|
+
NITRO_API_TOKEN="123"
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
In runtime config, define key using camelCase. In environment variables, define key using snake_case and uppercase.
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
{
|
|
208
|
+
helloWorld: "foo"
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
```bash
|
|
213
|
+
NITRO_HELLO_WORLD="foo"
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Custom env prefix
|
|
217
|
+
|
|
218
|
+
You can configure a secondary environment variable prefix using the `nitro.envPrefix` runtime config key. This prefix is checked in addition to the default `NITRO_` prefix:
|
|
219
|
+
|
|
220
|
+
```ts [nitro.config.ts]
|
|
221
|
+
import { defineConfig } from "nitro";
|
|
222
|
+
|
|
223
|
+
export default defineConfig({
|
|
224
|
+
runtimeConfig: {
|
|
225
|
+
nitro: {
|
|
226
|
+
envPrefix: "APP_",
|
|
227
|
+
},
|
|
228
|
+
apiToken: "",
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
With this configuration, both `NITRO_API_TOKEN` and `APP_API_TOKEN` will be checked as overrides.
|
|
234
|
+
|
|
235
|
+
### Env expansion
|
|
236
|
+
|
|
237
|
+
When enabled, environment variable references using `{{VAR_NAME}}` syntax in runtime config string values are expanded at runtime:
|
|
238
|
+
|
|
239
|
+
```ts [nitro.config.ts]
|
|
240
|
+
import { defineConfig } from "nitro";
|
|
241
|
+
|
|
242
|
+
export default defineConfig({
|
|
243
|
+
experimental: {
|
|
244
|
+
envExpansion: true,
|
|
245
|
+
},
|
|
246
|
+
runtimeConfig: {
|
|
247
|
+
url: "https://{{APP_DOMAIN}}/api",
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
```bash
|
|
253
|
+
APP_DOMAIN="example.com"
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
At runtime, `useRuntimeConfig().url` will resolve to `"https://example.com/api"`.
|