Skip to content

use cache directive times out during prerender with Neon adapter when poolQueryViaFetch = true #181

@tydolla00

Description

@tydolla00

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

  1. Set up Neon database with Prisma and @prisma/adapter-neon
  2. Configure neonConfig.poolQueryViaFetch = true in your database configuration
  3. Create a function with use cache directive that queries the database:
export const getAllGames = async () => {
  "use cache";
  cacheLife("max");
  return await handlePrismaOperation(
    async (prisma) => await prisma.game.findMany(),
  );
};
  1. Call this function from generateStaticParams():
export async function generateStaticParams() {
  const games = await getAllGames();
  // ...
}
  1. 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_cache instead of use cache
  • Edge runtime environments require poolQueryViaFetch = true, so this workaround may not be viable for Edge deployments
  • According to Next.js documentation, use cache should support database queries, suggesting this may be a compatibility issue

Related Documentation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions