Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions docs/content/migration/8.runtime-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,39 @@ import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
runtimeConfig: {
secretKey: '', // variable that can only be accessed on the server side
// Private config that is only available on the server
apiSecret: '123',
// Config within public will be also exposed to the client
public: {
BASE_URL: process.env.BASE_URL || 'https://nuxtjs.org' // variable that can also be accessed on the client side
apiBase: '/api'
}
},
})
```

```vue [pages/index.vue]
<script setup>
const config = useRuntimeConfig().public
// instead of process.env.BASE_URL you will now access config.BASE_URL
const config = useRuntimeConfig()

// instead of process.env you will now access config.public.apiBase
console.log(config.public.apiBase)
</script>
```

```ts [server/api/hello.ts]
const config = useRuntimeConfig().public
const config = useRuntimeConfig()

export default (req, res) => {
// you can now access config.BASE_URL
return {
baseURL: config.BASE_URL
}
// In server, you can now access config.apiSecret, in addition to config.public
console.log(config.apiSecret)
console.log(config.public.apiBase)
}
```

```ini [.env]
# Runtime config values are automatically replaced by matching environment variables at runtime
NUXT_API_SECRET=api_secret_token
NUXT_PUBLIC_API_BASE=https://nuxtjs.org
```

::