Fix Vercel build: force dynamic rendering, lazy db init, type fixes
This commit is contained in:
@@ -255,9 +255,12 @@ export default function AdminPage() {
|
|||||||
{report.targetType === 'post' && report.target && 'content' in report.target && (
|
{report.targetType === 'post' && report.target && 'content' in report.target && (
|
||||||
<button
|
<button
|
||||||
className="btn btn-ghost btn-sm"
|
className="btn btn-ghost btn-sm"
|
||||||
onClick={() => handlePostAction(report.target.id, report.target.isRemoved ? 'restore' : 'remove')}
|
onClick={() => {
|
||||||
|
const target = report.target as AdminPost;
|
||||||
|
handlePostAction(target.id, target.isRemoved ? 'restore' : 'remove');
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
{report.target.isRemoved ? 'Restore post' : 'Remove post'}
|
{(report.target as AdminPost).isRemoved ? 'Restore post' : 'Remove post'}
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
{report.status === 'open' ? (
|
{report.status === 'open' ? (
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import { useEffect, useState } from 'react';
|
|||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { useSearchParams } from 'next/navigation';
|
import { useSearchParams } from 'next/navigation';
|
||||||
|
|
||||||
|
export const dynamic = 'force-dynamic';
|
||||||
|
|
||||||
type EnvStatus = {
|
type EnvStatus = {
|
||||||
required: Record<string, boolean>;
|
required: Record<string, boolean>;
|
||||||
optional: Record<string, boolean>;
|
optional: Record<string, boolean>;
|
||||||
|
|||||||
@@ -24,6 +24,10 @@ export const metadata: Metadata = {
|
|||||||
viewport: "width=device-width, initial-scale=1, maximum-scale=1",
|
viewport: "width=device-width, initial-scale=1, maximum-scale=1",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Force all routes to be dynamic (no static generation at build time)
|
||||||
|
// This is appropriate for a social network where all content is user-generated
|
||||||
|
export const dynamic = 'force-dynamic';
|
||||||
|
|
||||||
export default function RootLayout({
|
export default function RootLayout({
|
||||||
children,
|
children,
|
||||||
}: {
|
}: {
|
||||||
|
|||||||
+26
-8
@@ -1,17 +1,35 @@
|
|||||||
import { drizzle } from 'drizzle-orm/neon-http';
|
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';
|
||||||
|
|
||||||
// Create the Neon client (with fallback for UI testing)
|
// Lazy initialization to prevent build-time crashes
|
||||||
const sql = process.env.DATABASE_URL
|
let _db: NeonHttpDatabase<typeof schema> | null = null;
|
||||||
? neon(process.env.DATABASE_URL)
|
|
||||||
: null;
|
|
||||||
|
|
||||||
// Create the Drizzle client
|
export const db = new Proxy({} as NeonHttpDatabase<typeof schema>, {
|
||||||
export const db = sql ? drizzle(sql, { schema }) : null;
|
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
|
// Helper to check if DB is available
|
||||||
export const isDbAvailable = () => db !== null;
|
export const isDbAvailable = () => !!process.env.DATABASE_URL;
|
||||||
|
|
||||||
// Export schema for use elsewhere
|
// Export schema for use elsewhere
|
||||||
export * from './schema';
|
export * from './schema';
|
||||||
|
|||||||
Reference in New Issue
Block a user