title: Must Read: Runtime Configuration Made Easy with useRuntimeConfig
date: 2024/7/29
updated: 2024/7/29
author: cmdragon
excerpt:
This article describes in detail the runtime configuration features in, including the methods of defining and using runtime configurations, and how to access configurations through useRuntimeConfig. It also explains the use of environment variables and .env files, especially for configuration management in different environments.
categories:
- front-end development
tags:
- Runtime Configuration
- SSR
- environment variable
- .env file
- useRuntimeConfig
scanningtwo-dimensional barcodeFollow or microsoft search:Programming Intelligence Front-End to Full-Stack Communication and Growth
is a framework based on , it greatly simplifies the development process of server-side rendering (SSR) and static site generation . Runtime configuration is a powerful feature that allows developers to dynamically adapt the configuration to different environments (e.g., development, production, etc.).
I. What is runtime configuration?
Runtime configuration is a feature in that allows you to use different configurations in different environments. For example, you may need to use one API base URL in one environment and another URL in another. runtime configuration allows you to do this easily.
II. How to define the runtime configuration?
In the file to define the runtime configuration. The following is a simple example:
export default defineNuxtConfig({
runtimeConfig: {
// private key, only available on server side
apiSecret: '123', // private key.
// public key, accessible on both client and server side
public: {
apiBase: .NUXT_PUBLIC_API_BASE || '/api'
}
}
})
In this example, theapiSecret
is a private key, available only on the server side.apiBase
is a public key that can be accessed on both the client and server side.
III. How to useuseRuntimeConfig
?
useRuntimeConfig
is a combinatorial function used to access the runtime configuration in a component or API interface. The following is an example of how to use it in a component:
<template>
<div>
<h1>API Base URL: {{ }}</h1>
</div>
</template>
<script>
export default {
setup() {
const config = useRuntimeConfig()
return {
config
}
}
}
</script>
In the server-side API, you can use it like thisuseRuntimeConfig
:
export default defineEventHandler((event) => {
const config = useRuntimeConfig(event)
// Using Configurations
})
IV. Environment variables and.env
file
You can find more information on the.env
file to set environment variables so that they can be accessed during development and build. Example:
NUXT_PUBLIC_API_BASE = ":5555"
NUXT_API_SECRET = "123"
These variables can be accessed via the Accessed in the Nuxt application.
existDuring production runsin which you should use the platform's environment variable configurations instead of the
.env
Documentation.After the build is completeWhen you run the server, the.env
The file will not be read. Exactly how you set the environment variables depends on your environment.
Five, app
namespace (computing)
In, the app namespace is used to store some specific runtime configurations that are usually related to the global behavior and settings of the application. There are two important keys in the app namespace: baseURL and cdnURL.
1.
corresponds English -ity, -ism, -ization: is a key used to store the root URL of the application. By default, this value is set to
'/'
. This key is mainly used to unify the handling of URL prefixes in applications, for example in scenarios such as API calls, routing links, and static resource access.
How to use:
// Access in your componentbaseURL
export default {
setup() {
const config = useRuntimeConfig()
('Base URL:', )
}
}
2.
corresponds English -ity, -ism, -ization: is a key used to store a CDN (Content Delivery Network) URL. This key is mainly used to provide a custom CDN URL in production environments when the application uses a CDN to speed up the loading of static resources.In development environments, this value is usually set to an empty string or a default value.
How to use:
// in yourAPIAccess in ServicecdnURL
export default defineEventHandler((event) => {
const config = useRuntimeConfig()
const cdnURL =
('CDN URL:', cdnURL)
})
Setting environment variables
In order to customize these values at runtime, you can do so by setting environment variables. For example, to change theYou can find more information on the
.env
file is added:
NUXT_APP_BASE_URL=
insofar asYou can find more information on the
.env
file is added:
NUXT_APP_CDN_URL=
sample code (computing)
Suppose you have aplugins/
file in which you want to use the:
export default defineNuxtPlugin((NuxtApp) => {
const config = useRuntimeConfig()
const baseURL =
('Using base URL:', baseURL)
})
insofar asserver/api/
hit the nail on the head:
export default defineEventHandler((event) => {
const config = useRuntimeConfig()
const cdnURL =
('Using CDN URL:', cdnURL)
})
In this way, you can ensure that applications can use different configurations in different environments (e.g., development, test, production), thereby increasing application flexibility and maintainability.
For the rest of the article, please click to jump to the personal blog page or scan the code to follow or WeChat search:Programming Intelligence Front-End to Full-Stack Communication and Growth
, read the full article:Must Read: Runtime Configuration Made Easy with useRuntimeConfig | cmdragon's Blog
Past articles are archived:
- Route Management: The useRouter Method and Routing Middleware Applications | cmdragon's Blog
- UseRoute Function Details and Examples | cmdragon's Blog
- Accessing a request URL using the useRequestURL combinatorial function | cmdragon's Blog
- Configuring and Using Environment Variables | cmdragon's Blog
- Data fetching in server-side rendering: combining useRequestHeaders and useFetch | cmdragon's Blog
- Accessing Request Events with the useRequestEvent Hook | cmdragon's Blog
- Efficient Data Acquisition and Management with useNuxtData | cmdragon's Blog
- Nuxt 3 User Guide: Getting to grips with useNuxtApp and the runtime context | cmdragon's Blog
- Asynchronous Data Fetching with useLazyFetch | cmdragon's Blog
- Enhancing the data loading experience with useLazyAsyncData | cmdragon's Blog
- Using useHydration for Data Hydration and Synchronization | cmdragon's Blog
- useHeadSafe: Generating HTML Head Elements Safely | cmdragon's Blog
- Header Magic: Easily Customize Page Meta Information to Improve User Experience | cmdragon's Blog
- Exploring useFetch: A Guide to Efficient Data Acquisition and Processing | cmdragon's Blog
- Error Detective: useError Combined Functions | cmdragon's Blog
- useCookie function: Managing cookies in an SSR environment | cmdragon's Blog
- Easy to master useAsyncData to get asynchronous data | cmdragon's Blog