# Build Optimization — 2026-06-07

**Scope:** `Dockerfile`, `docker-entrypoint.sh`, `docker-compose.yml`, `.dockerignore`, `next.config.ts`, `package.json` scripts, `pnpm-lock.yaml`, `Makefile`. No `.github/workflows/` (deploy is Coolify building the Dockerfile on push to `master`).

## Pipeline overview

`git push master` → Coolify clones repo → `docker build` (single-stage `node:22-slim`: copy manifests + prisma → `pnpm install --frozen-lockfile` (all deps incl. dev) → `COPY . .` → `prisma generate && next build`) → image retains full source, devDependencies and `.next/cache` → container start runs `prisma migrate deploy` then `next start`. No multi-stage, no Next.js standalone output, no BuildKit cache, no CI gate. Time/weight is dominated by a large single-stage image (~1.3+ GB) and full network re-install on any cache miss.

## Opportunities found

| Area | Current | Proposed | Est. savings | Severity | Issue |
|---|---|---|---|---|---|
| Image size / deploy | Single-stage, no standalone, dev deps + source in runtime image (~1.3+ GB) | Multi-stage + `output: "standalone"`, expanded `.dockerignore` | Image 3–4× smaller (<450 MB); faster push/pull/start | high | #91 |
| Install speed | `pnpm install` with no persistent store cache; ~411 KB lockfile re-fetched on cache miss | BuildKit `--mount=type=cache` on pnpm store | −50%+ on install step (cache-hit) | medium | #92 |

## Recurring

First build-optimization audit for this repo — no prior `docs/insights/build-optimization/` snapshots to compare. No recurring items yet.

## Recommendations

- `next.config.ts` + `Dockerfile` → enable `output: "standalone"` and split into deps/builder/runner stages, copying only `.next/standalone`, `.next/static`, `public`, `prisma`; expand `.dockerignore` (`docs`, `.darkflow.d`, `.agents`, `.claude`, `.env*`, `*.tsbuildinfo`, `*.md`) → acceptance: runtime image < 450 MB, no devDependencies present (#91).
- `Dockerfile` → add `RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile` with `store-dir /pnpm/store` → acceptance: install step −50%+ on warm cache, `--frozen-lockfile` preserved (#92).

## Notes (minor / not filed)

- No CI pipeline (`.github/workflows/` empty): tests (`vitest`), `lint`, and typecheck never run before Coolify deploy. Not a build-*efficiency* issue, but a quality gap — consider a lightweight GitHub Actions workflow (`prisma generate && next build` for DB-less typecheck + `pnpm test`). Tracked here as a note, not an issue.
- Dockerfile layer ordering for the install cache is already correct (manifests + prisma copied before `pnpm install`).
- Related already-tracked Dockerfile concerns are security-sourced and out of scope here: #87 (secrets in build ARG), #90 (run container as non-root).
