Next.js Environment Variables All In One
.env
.env.local
DB_HOST=localhost
DB_USER=myuser
DB_PASS=mypassword
// getStaticProps:
// pages/index.js
export async function getStaticProps() {
const db = await myDB.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS,
})
// ...
}
NEXT_PUBLIC_
NEXT_PUBLIC_ANALYTICS_ID=abcdefghijk
// pages/index.js
import setupAnalyticsService from '../lib/my-analytics-service'
// 'NEXT_PUBLIC_ANALYTICS_ID' can be used here as it's prefixed by 'NEXT_PUBLIC_'.
// It will be transformed at build time to `setupAnalyticsService('abcdefghijk')`.
setupAnalyticsService(process.env.NEXT_PUBLIC_ANALYTICS_ID)
function HomePage() {
return <h1>Hello World</h1>
}
export default HomePage
https://nextjs.org/docs/basic-features/environment-variables
https://nextjs.org/docs/api-reference/next.config.js/environment-variables
demo
// process.env
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 ️,侵权必究⚠️!