FROM node:22-slim AS build

WORKDIR /app

ARG VITE_APP_VERSION
ENV VITE_APP_VERSION=$VITE_APP_VERSION
# Optional browser error reporting, baked in at build time (empty = off). The
# DSN's host must also be allowed by CSP_CONNECT_SRC at run time.
ARG VITE_SENTRY_DSN=""
ARG VITE_SENTRY_ENVIRONMENT=""
ENV VITE_SENTRY_DSN=$VITE_SENTRY_DSN
ENV VITE_SENTRY_ENVIRONMENT=$VITE_SENTRY_ENVIRONMENT

COPY package.json package-lock.json .npmrc ./
RUN npm ci

COPY . .
RUN npm run build

# Serve with nginx
FROM nginxinc/nginx-unprivileged:alpine

COPY --from=build /app/dist /usr/share/nginx/html
COPY default.conf.template /etc/nginx/templates/default.conf.template
COPY nginx/security-headers.conf /etc/nginx/security-headers.conf

# Have the image derive the DNS resolver from /etc/resolv.conf, so the same
# image proxies to the backend on Docker, Podman, Kubernetes and Railway with
# no per-platform configuration. 19-resolver.envsh turns that into
# NGINX_RESOLVER (with a Docker fallback) for the config template; setting
# NGINX_RESOLVER explicitly still overrides both.
ENV NGINX_ENTRYPOINT_LOCAL_RESOLVERS=1
# Extra connect-src origins for the CSP (e.g. https://o123.ingest.sentry.io
# when built with VITE_SENTRY_DSN). Declared even when empty: the template
# engine only substitutes variables that exist, and would otherwise leave the
# literal "${CSP_CONNECT_SRC}" in the header.
ENV CSP_CONNECT_SRC=""
COPY --chmod=755 docker-entrypoint.d/19-resolver.envsh /docker-entrypoint.d/

EXPOSE 8080
