Skip to content

fix: lazy DB init to prevent ECONNREFUSED on Vercel when DATABASE_URL is unset#1

Draft
Copilot wants to merge 1 commit into
mainfrom
copilot/fix-vercel-deployment-error
Draft

fix: lazy DB init to prevent ECONNREFUSED on Vercel when DATABASE_URL is unset#1
Copilot wants to merge 1 commit into
mainfrom
copilot/fix-vercel-deployment-error

Conversation

Copilot AI commented Apr 20, 2026

Copy link
Copy Markdown

lib/comment-config.js had a top-level await ensureDbTables(), so Next.js evaluated it at build/module-load time. Without DATABASE_URL, postgres() defaults to localhost:5432ECONNREFUSED 127.0.0.1:5432 on Vercel.

Changes

  • lib/db.js — guard connection creation: sql/db are null when DATABASE_URL is absent; ensureDbTables() is a no-op in that case
  • lib/comment-config.js — remove top-level await ensureDbTables(); module no longer touches the DB on import
  • app/api/comments/[[...comment]]/route.js — call ensureDbTables() lazily inside withParams on each request; returns 503 if DB init fails
// Before: runs at module load → crashes build on Vercel without DATABASE_URL
await ensureDbTables()

// After: deferred to request time, fails gracefully
async function withParams(handler, req, ctx) {
  try {
    await ensureDbTables()
  } catch (e) {
    return NextResponse.json({ error: 'Database unavailable' }, { status: 503 })
  }
  // ...
}

The app now deploys without DATABASE_URL; comments return 503 until a real DB is wired up.

@vercel

vercel Bot commented Apr 20, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
notionic Ready Ready Preview, Comment Apr 20, 2026 7:56am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants