Repository navigation
Bug #93866
Next.js App Router memory leak when using server actions with Prisma in developmentI am testing a Next.js 15 app using the App Router and Prisma. After several hot reloads during development, memory usage keeps increasing and eventually the dev server becomes very slow or crashes. Current setup:
Example server action: 'use server'
import { prisma } from '@/lib/prisma'
export async function createPost(data: FormData) {
return prisma.post.create({
data: {
title: data.get('title') as string
}
})
}Prisma client: import { PrismaClient } from '@prisma/client'
export const prisma = new PrismaClient()Symptoms:
I read that Fast Refresh can recreate modules in development, but I am not fully sure what the recommended pattern is now with App Router + Server Actions. Should Prisma be attached to |
Replies: 2 comments
|
Yes, this is a common issue in Next.js development with Prisma. The problem is not Server Actions themselves — it is that during development, Fast Refresh/hot reload can recreate modules multiple times, which creates multiple In production this usually does not happen because modules are initialized once. The recommended approach is still to cache Prisma on Example: import { PrismaClient } from '@prisma/client'
const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined
}
export const prisma =
globalForPrisma.prisma ??
new PrismaClient()
if (process.env.NODE_ENV !== 'production') {
globalForPrisma.prisma = prisma
}Why this fixes it:
This is mostly noticeable in:
Production is usually unaffected because the server process is stable. Also make sure:
The Prisma docs still recommend the |
|
Yes, I'd still attach Prisma to What you're describing is usually Fast Refresh recreating modules and instantiating new Something like: import { PrismaClient } from '@prisma/client'
const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined
}
export const prisma =
globalForPrisma.prisma ??
new PrismaClient()
if (process.env.NODE_ENV !== 'production')
globalForPrisma.prisma = prismaAlso worth checking whether the memory growth is actually Prisma-related or if it's coming from something else in the App Router tree. I've seen people spend days chasing "Prisma leaks" that ended up being cached closures, request-time APIs, or other server-side state sticking around during development. I collected a bunch of debugging notes and architecture patterns around Next.js caching, server boundaries, and common App Router pitfalls here: https://www.skills.sh/mohamed-hossam1/nextjs-cache-architecture/nextjs-cache-architecture Repo: If any of it ends up being useful, feel free to drop a ⭐ on the repo. |
Yes, this is a common issue in Next.js development with Prisma.
The problem is not Server Actions themselves — it is that during development, Fast Refresh/hot reload can recreate modules multiple times, which creates multiple
PrismaClientinstances.In production this usually does not happen because modules are initialized once.
The recommended approach is still to cache Prisma on
globalThis.Example:
Why…