From f038c95777cec1228b87226f638e9e709211f94c Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 5 Feb 2026 22:16:29 +0100 Subject: [PATCH] Fix Docker build failure: lazy-initialize MinIO client The production env var check in createMinioClient() was throwing during `next build` page data collection because MINIO_ACCESS_KEY/SECRET_KEY aren't available at Docker build time. Changed from eager module-level initialization to a lazy Proxy pattern that defers client creation to first actual use, while maintaining backward compatibility with all existing `minio.method()` call sites. Co-Authored-By: Claude Opus 4.6 --- src/lib/minio.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/lib/minio.ts b/src/lib/minio.ts index 29d17b3..9610db2 100644 --- a/src/lib/minio.ts +++ b/src/lib/minio.ts @@ -1,6 +1,6 @@ import * as Minio from 'minio' -// MinIO client singleton +// MinIO client singleton (lazy-initialized to avoid build-time errors) const globalForMinio = globalThis as unknown as { minio: Minio.Client | undefined } @@ -30,9 +30,24 @@ function createMinioClient(): Minio.Client { }) } -export const minio = globalForMinio.minio ?? createMinioClient() +/** + * Get the MinIO client instance (lazy-initialized). + * The client is only created on first access, not at module import time. + * This prevents build-time errors when env vars are not available. + */ +export function getMinioClient(): Minio.Client { + if (!globalForMinio.minio) { + globalForMinio.minio = createMinioClient() + } + return globalForMinio.minio +} -if (process.env.NODE_ENV !== 'production') globalForMinio.minio = minio +// Backward-compatible export — lazy getter via Proxy +export const minio: Minio.Client = new Proxy({} as Minio.Client, { + get(_target, prop, receiver) { + return Reflect.get(getMinioClient(), prop, receiver) + }, +}) // Default bucket name export const BUCKET_NAME = process.env.MINIO_BUCKET || 'mopc-files'