From bb6b9a289b1954f20fc75271d958db33b116f17e Mon Sep 17 00:00:00 2001 From: Christopher Date: Thu, 22 Jan 2026 03:17:01 -0800 Subject: [PATCH] Configure dynamic rendering and robust DB init for Vercel --- src/app/layout.tsx | 2 ++ src/db/index.ts | 35 ++++++++++------------------------- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 4c8d744..971b0f4 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -28,6 +28,8 @@ export const metadata: Metadata = { // This is appropriate for a social network where all content is user-generated export const dynamic = 'force-dynamic'; +// This is appropriate for a social network where all content is user-generated + export default function RootLayout({ children, }: { diff --git a/src/db/index.ts b/src/db/index.ts index bc05f9d..e4bbfdc 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -2,33 +2,18 @@ import { drizzle, NeonHttpDatabase } from 'drizzle-orm/neon-http'; import { neon } from '@neondatabase/serverless'; import * as schema from './schema'; -// Lazy initialization to prevent build-time crashes -let _db: NeonHttpDatabase | null = null; +// Best Practice: +// 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, { - 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); - }, -}); +const sql = neon(connectionString); -// 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 schema for use elsewhere