Skip to content

Commit 65bdb43

Browse files
committed
fix: use runtime check instead of server-only directive for Prisma
1 parent 625d334 commit 65bdb43

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/lib/prisma.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import 'server-only';
2-
31
import { PrismaClient } from '@prisma/client';
42

5-
const globalForPrisma = global as unknown as { prisma: PrismaClient };
3+
// Prevent PrismaClient instantiation in browser environment
4+
const globalForPrisma = globalThis as unknown as { prisma: PrismaClient | undefined };
65

7-
export const prisma =
8-
globalForPrisma.prisma ||
9-
new PrismaClient({
10-
log: ['query'],
6+
function createPrismaClient(): PrismaClient {
7+
if (typeof window !== 'undefined') {
8+
throw new Error('PrismaClient cannot be used in the browser');
9+
}
10+
return new PrismaClient({
11+
log: process.env.NODE_ENV === 'development' ? ['query'] : [],
1112
});
13+
}
14+
15+
export const prisma = globalForPrisma.prisma ?? createPrismaClient();
1216

13-
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma;
17+
if (process.env.NODE_ENV !== 'production') {
18+
globalForPrisma.prisma = prisma;
19+
}

0 commit comments

Comments
 (0)