Detect Next.js build phase and skip DATABASE_URL check during Docker build
This commit is contained in:
+17
-17
@@ -1,26 +1,26 @@
|
||||
import { Pool } from 'pg';
|
||||
|
||||
const connectionString = process.env.DATABASE_URL;
|
||||
|
||||
function createPool() {
|
||||
const connectionString = process.env.DATABASE_URL;
|
||||
|
||||
if (!connectionString) {
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
throw new Error('DATABASE_URL environment variable is not set');
|
||||
if (typeof process.env.NEXT_PHASE === 'string' && process.env.NEXT_PHASE.includes('phase-production-build')) {
|
||||
// Next.js collects page data during the build. We don't need a real DB for that.
|
||||
return {
|
||||
query: async () => {
|
||||
throw new Error('DATABASE_URL environment variable is not set');
|
||||
},
|
||||
connect: async () => {
|
||||
throw new Error('DATABASE_URL environment variable is not set');
|
||||
},
|
||||
end: async () => {},
|
||||
on: () => {},
|
||||
removeListener: () => {},
|
||||
} as unknown as Pool;
|
||||
}
|
||||
// During build we don't need a real connection; return a stubbed pool
|
||||
// that throws clearly if someone actually tries to query.
|
||||
return {
|
||||
query: async () => {
|
||||
throw new Error('DATABASE_URL environment variable is not set');
|
||||
},
|
||||
connect: async () => {
|
||||
throw new Error('DATABASE_URL environment variable is not set');
|
||||
},
|
||||
end: async () => {},
|
||||
on: () => {},
|
||||
removeListener: () => {},
|
||||
} as unknown as Pool;
|
||||
throw new Error('DATABASE_URL environment variable is not set');
|
||||
}
|
||||
|
||||
return new Pool({ connectionString });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user