From 697f547b2cdaf4f5790e5035b60786dba4c190fb Mon Sep 17 00:00:00 2001 From: sumiren <42498227+sumiren@users.noreply.github.com> Date: Thu, 23 Jun 2022 03:07:23 +0900 Subject: [PATCH] docs: update usage about runtime config and environment variables (#5569) --- docs/content/migration/8.runtime-config.md | 27 ++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/docs/content/migration/8.runtime-config.md b/docs/content/migration/8.runtime-config.md index 33ddf98b9..2591f50e7 100644 --- a/docs/content/migration/8.runtime-config.md +++ b/docs/content/migration/8.runtime-config.md @@ -25,9 +25,11 @@ 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' } }, }) @@ -35,20 +37,27 @@ export default defineNuxtConfig({ ```vue [pages/index.vue] ``` ```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 +``` + ::