FROM node:22-alpine AS deps
WORKDIR /app
ENV NEXT_TELEMETRY_DISABLED=1

# Use a free mainland-China npm mirror by default. CI can override this with
# --build-arg NPM_REGISTRY=https://registry.npmjs.org if its egress allows it.
ARG NPM_REGISTRY=https://registry.npmmirror.com

COPY package.json package-lock.json ./
RUN npm ci \
    --registry="${NPM_REGISTRY}" \
    --fetch-retries=5 \
    --fetch-timeout=120000 \
    --fetch-retry-mintimeout=10000 \
    --fetch-retry-maxtimeout=60000 \
    --no-audit \
    --no-fund

FROM node:22-alpine AS builder
WORKDIR /app
ENV NEXT_TELEMETRY_DISABLED=1

COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

FROM nginxinc/nginx-unprivileged:1.30.4-alpine AS runner

COPY deploy/nginx.conf /etc/nginx/nginx.conf
COPY --from=builder /app/out /usr/share/nginx/html

EXPOSE 3000

ENTRYPOINT []
CMD ["nginx", "-g", "daemon off;"]
