Fix Docker build failure: lazy-initialize MinIO client
Build and Push Docker Image / build (push) Successful in 13m48s Details

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 <noreply@anthropic.com>
This commit is contained in:
Matt 2026-02-05 22:16:29 +01:00
parent 699248e40b
commit f038c95777
1 changed files with 18 additions and 3 deletions

View File

@ -1,6 +1,6 @@
import * as Minio from 'minio' import * as Minio from 'minio'
// MinIO client singleton // MinIO client singleton (lazy-initialized to avoid build-time errors)
const globalForMinio = globalThis as unknown as { const globalForMinio = globalThis as unknown as {
minio: Minio.Client | undefined 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 // Default bucket name
export const BUCKET_NAME = process.env.MINIO_BUCKET || 'mopc-files' export const BUCKET_NAME = process.env.MINIO_BUCKET || 'mopc-files'