[英]How to get Next.JS environment variables on client side?
I have a Next.Js application that I will deploy with docker.我有一个 Next.Js 应用程序,我将使用 docker 部署它。 I am passing my environment variables in docker file and docker-compose.yaml.
我在 docker 文件和 docker-compose.yaml 中传递我的环境变量。 Next version: 12.1.6
下一个版本:12.1.6
Dockerfile Dockerfile
# Install dependencies only when needed
FROM node:16-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
# If using npm with a `package-lock.json` comment out above and use below instead
# COPY package.json package-lock.json ./
# RUN npm ci
# Rebuild the source code only when needed
FROM node:16-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN yarn build
# If using npm comment out above and use below instead
# RUN npm run build
# Production image, copy all the files and run next
FROM node:16-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
ENV NEXT_PUBLIC_BASE_URL example --> I'm stating it here. Example is not my value, it just takes space.
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT 3000
CMD ["node", "server.js"]
docker-compose.yaml docker-compose.yaml
version: '3'
services:
frontend:
image: caneral:test-1
ports:
- '3000:3000'
environment:
- NEXT_PUBLIC_BASE_URL=https://example.com/api
I am building with the following command:我正在使用以下命令进行构建:
docker build -t caneral:test-1 .
Then I run docker-compose:然后我运行 docker-compose:
docker-compose up -d
While I can access the NEXT_PUBLIC_BASE_URL value on the server side, I cannot access it on the client side.虽然我可以在服务器端访问 NEXT_PUBLIC_BASE_URL 值,但我无法在客户端访问它。 It returns undefined.
它返回未定义。 Shouldn't I reach it because I define it as NEXT_PUBLIC?
我不应该因为我将它定义为 NEXT_PUBLIC 而达到它吗? This is stated in the official documents.
这在官方文件中有说明。
How to get environment variables on client side?如何在客户端获取环境变量?
Details, you have:详细信息,您有:
.env
file..env
文件。 (i'm not sure how docker files will affect logic) next.config.js
filenext.config.js
文件Server-side has access to these 2 files.服务器端可以访问这两个文件。
Client-side doesn't have access to .env
file.客户端无权访问
.env
文件。
What you can do:你可以做什么:
In your next.config.js
file you can declare a variable
where value is your process.env
value.在
next.config.js
文件中,您可以声明一个variable
,其中 value 是您的process.env
值。
const baseTrustFactor = process.env.trustFactor
IMPORTANT: do not expose your private info (keys/tokens etc.) to the client-side.重要提示:不要将您的私人信息(密钥/令牌等)暴露给客户端。
If you need to compare the tokens you can:如果您需要比较令牌,您可以:
next.config.js
such as:next.config.js
中设置条件,例如: const baseTrustFactor = process.env.trustFactor == '21'? true: false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.