Next.js SSG Server Selection Timeout During Build


Hello everyone,

I encountered an issue while using Next.js with Static Site Generation (SSG) on the DigitalOcean App platform. Specifically, I faced a problem where the data from the database was not rendered during the build process. I hope to share my experience and provide insights on troubleshooting and resolving this issue.

During the build process of my Next.js application on App platform, I noticed that the data from the MongoDB cluster was not being fetched, as the connection was timing out. As a result, the generated pages displayed empty or incomplete content, affecting the overall functionality and user experience.

Steps taken

  1. Tried increasing staticPageGenerationTimeout
  2. Removed everything from trusted sources, leaving MongoDB cluster available to everyone
  3. Disabled redis caching
  4. Force rebuilt with cleared cache


I have placed console log statements in my code to have better view of the problem. Here’s the log during build process, describing that connection to the database was timing out.

[2023-05-10 16:12:06] │ info  - Collecting page data...
[2023-05-10 16:13:12] │ info  - Generating static pages (0/11)
[2023-05-10 16:13:13] │ info  - Generating static pages (2/11)
[2023-05-10 16:13:13] │ info  - Generating static pages (5/11)
[2023-05-10 16:13:13] │ info  - Generating static pages (8/11)
[2023-05-10 16:13:43] │ getProjects error:  PrismaClientInitializationError: 
[2023-05-10 16:13:43] │ Invalid `.Z.product.count()` invocation in
[2023-05-10 16:13:43] │ /workspace/.next/server/chunks/1786.js:33:97
[2023-05-10 16:13:43] │ 
[2023-05-10 16:13:43] │   30 */ const getProducts = async (filter, active, visible)=>{
[2023-05-10 16:13:43] │   31    try {
[2023-05-10 16:13:43] │   32        const result = await lib_prismadb__WEBPACK_IMPORTED_MODULE_1__/* ["default"].$transaction */ .Z.$transaction([
[2023-05-10 16:13:43] │ → 33            lib_prismadb__WEBPACK_IMPORTED_MODULE_1__/* ["default"].product.count */ .Z.product.count(
[2023-05-10 16:13:43] │ Raw query failed. Code: `unknown`. Message: `Server selection timeout: No available servers. Topology: { Type: ReplicaSetNoPrimary, Servers: [ { Address:, Type: Unknown, Error: timed out }, ] }`
[2023-05-10 16:13:43] │     at Kr.handleRequestError (/workspace/node_modules/@prisma/client/runtime/library.js:163:19773)
[2023-05-10 16:13:43] │     at Kr.handleAndLogRequestError (/workspace/node_modules/@prisma/client/runtime/library.js:163:19031)
[2023-05-10 16:13:43] │     at /workspace/node_modules/@prisma/client/runtime/library.js:173:282
[2023-05-10 16:13:43] │     at async /workspace/node_modules/@prisma/client/runtime/library.js:173:397 {
[2023-05-10 16:13:43] │   clientVersion: '4.10.1',
[2023-05-10 16:13:43] │   errorCode: undefined
[2023-05-10 16:13:43] │ }
[2023-05-10 16:13:43] │ Debug whether products get fetched during build {
[2023-05-10 16:13:43] │   data: [],
[2023-05-10 16:13:43] │   meta: { current_page: 1, from: 0, last_page: 1, to: 0, total: 0 }
[2023-05-10 16:13:43] │ }
[2023-05-10 16:13:43] │ info  - Generating static pages (11/11)


If you have encountered a similar issue where the database data is not rendered during the Next.js SSG build process, I would appreciate any insights, solutions, or workarounds that you have discovered.

Thank you for your assistance!

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
May 11, 2023

Hi there,

Connecting to a database during the build phase is not supported in App Platform, and so this limitation also applies to trusted sources:

As a best practice, you should not really run your migrations during the build process, as that way if the deploy process fails, your database migrations would have been executed anyway meaning that your app will still use the old version of your code base, but the new database schema, and if you have backwards incompatible schema changes it will be a problem.

It is best to run your database migrations during the deploy stage, that way you will know that right before your app starts your schema will be up to date.

Let me know how it goes!



Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel