Configure dynamic rendering and robust DB init for Vercel

This commit is contained in:
Christopher
2026-01-22 03:17:01 -08:00
parent e370ede692
commit bb6b9a289b
2 changed files with 12 additions and 25 deletions
+2
View File
@@ -28,6 +28,8 @@ export const metadata: Metadata = {
// This is appropriate for a social network where all content is user-generated // This is appropriate for a social network where all content is user-generated
export const dynamic = 'force-dynamic'; export const dynamic = 'force-dynamic';
// This is appropriate for a social network where all content is user-generated
export default function RootLayout({ export default function RootLayout({
children, children,
}: { }: {
+10 -25
View File
@@ -2,33 +2,18 @@ import { drizzle, NeonHttpDatabase } from 'drizzle-orm/neon-http';
import { neon } from '@neondatabase/serverless'; import { neon } from '@neondatabase/serverless';
import * as schema from './schema'; import * as schema from './schema';
// Lazy initialization to prevent build-time crashes // Best Practice:
let _db: NeonHttpDatabase<typeof schema> | null = null; // In Vercel builds, environment variables might be missing during the "Build" step.
// We provide a fallback connection string to allow the code to load and type-check.
// The app will fail fast at runtime if it tries to actually query with this invalid URL.
const connectionString = process.env.DATABASE_URL || 'postgres://placeholder:placeholder@localhost:5432/placeholder';
export const db = new Proxy({} as NeonHttpDatabase<typeof schema>, { const sql = neon(connectionString);
get: (target, prop) => {
if (!_db) {
if (!process.env.DATABASE_URL) {
// Allow build to pass by returning a dummy if accessed during build
if (process.env.NODE_ENV === 'production') {
console.warn('Database accessed during build without DATABASE_URL. Returning dummy.');
// We return a proxy that logs on any access to avoid hard crashes if possible,
// but typically this path is creating the client.
// Returning a dummy DB object that matches the shape is hard.
// But if we throw, we crash.
// The issue is verify the build doesn't crash on import.
// If we are here, something ACCESSED db.
}
throw new Error('DATABASE_URL is not defined');
}
const sql = neon(process.env.DATABASE_URL);
_db = drizzle(sql, { schema });
}
return Reflect.get(_db, prop);
},
});
// Helper to check if DB is available // Create the Drizzle client with the specific schema type
export const db = drizzle(sql, { schema });
// Helper to check if DB is configured (useful for UI checks)
export const isDbAvailable = () => !!process.env.DATABASE_URL; export const isDbAvailable = () => !!process.env.DATABASE_URL;
// Export schema for use elsewhere // Export schema for use elsewhere