This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(nitro, nuxi): add runtimeConfig types (for
#config
and `useRunt…
…imeConfig()`) (#1783) Co-authored-by: Tobias Diez <[email protected]> Co-authored-by: Pooya Parsa <[email protected]>
- Loading branch information
1 parent
fd9e56f
commit 13a8e2b
Showing
8 changed files
with
127 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Runtime Config | ||
|
||
Nuxt provides a runtime config API to share within App and API routes. | ||
|
||
## Exposing runtime config | ||
|
||
To expose config and environment variables to the rest of your app, you will need to define runtime configuration in your `nuxt.config` file, using either the [`publicRuntimeConfig` or `privateRuntimeConfig` options](/docs/directory-structure/nuxt.config#privateruntimeconfig). Based on whether you want it to be accessible on the client-side part of your app or not. | ||
|
||
**Example:** | ||
|
||
```ts [nuxt.config.ts] | ||
export default defineNuxtConfig({ | ||
publicRuntimeConfig: { | ||
API_BASE: '/api' | ||
}, | ||
privateRunimeConfig: { | ||
API_SECRET: '123' | ||
} | ||
}) | ||
``` | ||
|
||
When adding `API_BASE` to the `publicRuntimeConfig`, Nuxt adds it to the pages payload. This way we can universally access `API_BASE` in both server and browser. | ||
|
||
### Environment Variables | ||
|
||
The most common way to provide configuration, is using [Environment Variables](https://medium.com/chingu/an-introduction-to-environment-variables-and-how-to-use-them-f602f66d15fa). | ||
Nuxt CLI has built-in [dotenv](https://github.com/motdotla/dotenv) support. | ||
|
||
In addition to any process environment variables, if you have a `.env` file in your project root directory, it will be automatically loaded into `process.env` and accessible within your `nuxt.config` file and Modules. | ||
|
||
**Example:** | ||
|
||
```sh [.env] | ||
BASE_URL=https://nuxtjs.org | ||
API_SECRET=api_secret_token | ||
``` | ||
|
||
```ts [nuxt.config.ts] | ||
export default defineNuxtConfig({ | ||
publicRuntimeConfig: { | ||
BASE_URL: process.env.BASE_URL | ||
}, | ||
privateRuntimeConfig: { | ||
API_SECRET: process.env.API_SECRET | ||
} | ||
}) | ||
``` | ||
|
||
**💡 Tip:** While it is not necessary, by using identical runtime config names as env variables, you can easily override them in production using platform environment variables. | ||
|
||
## Accessing runtime config | ||
|
||
### Vue app | ||
|
||
Within the Vue part of your Nuxt app, you will need to call `useRuntimeConfig()` to access the runtime config. | ||
|
||
**Note:** Behavior is different between client side and server side: | ||
|
||
- On Client-Side, only `publicRuntimeConfig` is available and object is writable + reactive | ||
- On Server-Side, both `publicRuntimeConfig` and `privateRuntimeConfig` are merged and object is readonly to avoid context sharing | ||
|
||
```vue | ||
<template> | ||
<div> | ||
<div>Token: {{ config.API_AUTH_TOKEN }}</div> | ||
</div> | ||
</template> | ||
<script setup> | ||
const config = useRuntimeConfig() | ||
</script> | ||
``` | ||
|
||
**🛑 Security note:** Never use example above if `API_AUTH_TOKEN` is a private config. Even if you use `privateRuntimeConfig`, you have to be still careful you do not expose such config to either payload or html! | ||
|
||
### API routes | ||
|
||
Within the API routes, you can access runtime config by directly importing from virtual `#config`. | ||
|
||
```ts | ||
import config from '#config' | ||
|
||
export default async () => { | ||
const result = await $fetch('https://my.api.com/test', { | ||
headers: { | ||
Authorization: `Bearer ${config.API_AUTH_TOKEN}` | ||
} | ||
}) | ||
return result | ||
} | ||
``` | ||
|
||
### Typing runtime config | ||
|
||
Currently it is possible to manually type your runtime config. | ||
|
||
```ts [index.d.ts] | ||
declare module '@nuxt/kit' { | ||
interface PublicRuntimeConfig { | ||
testConfig: string | ||
} | ||
interface PrivateRuntimeConfig { | ||
token: string | ||
} | ||
} | ||
// It is always important to ensure you import/export something when augmenting a type | ||
export {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface PublicRuntimeConfig extends Record<string, any> { } | ||
export interface PrivateRuntimeConfig extends PublicRuntimeConfig { } | ||
|
||
type _RuntimeConfig = PublicRuntimeConfig & Partial<PrivateRuntimeConfig> | ||
export interface RuntimeConfig extends _RuntimeConfig { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters