-
Notifications
You must be signed in to change notification settings - Fork 43
Description
Summary
When using Next.js 16's use cache directive with Neon database adapter (@prisma/adapter-neon), the build process times out during static generation/prerendering when poolQueryViaFetch = true is enabled.
Environment
- Next.js: 16.0.0
- Next.js Feature: Cache Components enabled (
cacheComponents: true) - Database Adapter:
@prisma/adapter-neon - Neon Config:
poolQueryViaFetch = true - Runtime: Node.js (not Edge)
Current Behavior
When calling a database query function with use cache directive from generateStaticParams() during build, Next.js throws a timeout error:
Error: Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".
or
Error [NeonDbError]: Error connecting to database: Error: During prerendering, fetch() rejects when the prerender is complete. Typically these errors are handled by React but if you move fetch() to a different context by using `setTimeout`, `after`, or similar functions you may observe this error and you should handle it in that context.
Expected Behavior
The use cache directive should work with database queries during prerendering, as documented in the Next.js docs.
Steps to Reproduce
- Set up Neon database with Prisma and
@prisma/adapter-neon - Configure
neonConfig.poolQueryViaFetch = truein your database configuration - Create a function with
use cachedirective that queries the database:
export const getAllGames = async () => {
"use cache";
cacheLife("max");
return await handlePrismaOperation(
async (prisma) => await prisma.game.findMany(),
);
};- Call this function from
generateStaticParams():
export async function generateStaticParams() {
const games = await getAllGames();
// ...
}- Run
npm run build
Workaround
Disabling poolQueryViaFetch (using WebSocket connections instead) resolves the issue:
// Comment out or remove this line:
// neonConfig.poolQueryViaFetch = true;With WebSocket connections, use cache works correctly during build-time prerendering.
Root Cause Analysis
When poolQueryViaFetch = true, Neon uses fetch() internally for database queries. During build-time prerendering, Next.js detects fetch() calls and treats them as potentially request-specific/dynamic APIs. Since there's no request context during generateStaticParams(), Next.js times out waiting for request-specific data that will never arrive.
WebSocket connections are not flagged as request-specific APIs by Next.js, so they work correctly with use cache during prerendering.
Additional Context
- This issue only occurs during build-time static generation (
generateStaticParams) - The same code works fine with
unstable_cacheinstead ofuse cache - Edge runtime environments require
poolQueryViaFetch = true, so this workaround may not be viable for Edge deployments - According to Next.js documentation,
use cacheshould support database queries, suggesting this may be a compatibility issue