Next.js Discord

Discord Forum

"Collecting page data for /page is still timing out after 2 attempts" on build

Unanswered
Silver Marten posted this in #help-forum
Open in Discord
Avatar
Silver MartenOP
When I run an npm run build for prod, it seems to be trying to connect to redis but it can't so it just times out and the build fails after a few minutes. I recently added redis, but I didn't have this issue with my database in the past. Tried looking around online but I can't find anyone else with this issue and I'm not sure what to do about it at this point. Would love some help.

I am running next 14.2.13 with the app directory. My build process is done via docker:
FROM node:22 as base
WORKDIR /app

EXPOSE 3000

ENV PORT 3000

COPY package.json package-lock.json ./

RUN npm install --frozen-lockfile
RUN apt-get update && apt-get install -y imagemagick

COPY . .
RUN if [ -d /app/.next/ ]; then chown -R 1000:1000 /app/.next/; fi

FROM base as prod
RUN npx prisma generate && npm run build
USER 1000
CMD ["npm", "run", "start:migrate:prod"]

FROM base as dev
USER 1000
CMD ["npm", "run", "dev"]
and my app is run with docker compose:
services:
  app:
    # volumes:
    #   - ./:/app
    build:
      context: ./
      dockerfile: ./Dockerfile
      target: prod
    container_name: app
    user: "1000"
    ports:
      - "3000:3000"
      - "9230:9229"
    environment:
      - NODE_ENV=production
      - DEBUG=engine
    env_file:
      - ./.env
    restart: unless-stopped

  db:
    image: postgis/postgis:16-3.4
    container_name: app-db
    restart: always
    env_file:
      - ./.env
    environment:
      POSTGRES_DB: default
    user: "1000"
    volumes:
      - ./db:/var/lib/postgresql/data

  redis:
    image: redis:alpine
    container_name: app-redis
    restart: always
And I'm just running docker compose build.

Here is what I have for my redis.ts file:
import { createClient } from 'redis';

export const redis = await createClient({
  url: process.env.REDIS_URL,
})
  .on('error', (err) => console.error(err))
  .connect();
Image

2 Replies

Avatar
Silver MartenOP
cont. here since I hit the character limit above. Similar to my redis implementation, here is my database connection. I do not have issues with npm run build when I have only the database. Once I added redis that's when I started running into this issue. I use the same process of grabbing the connection variables from the env, and everything works perfectly fine on dev so I know all the env vars are configured correctly.
import { PrismaClient } from '@prisma/client';

const options = {
  log: [
    {
      emit: 'event',
      level: 'query',
    },
  ],
} as any;
const prismaClientSingleton = () => new PrismaClient(
  process.env.NODE_ENV === 'development' ? options : undefined,
);

declare const globalThis: {
  prismaGlobal: ReturnType<typeof prismaClientSingleton>;
} & typeof global;

// eslint-disable-next-line import/no-mutable-exports
let prisma: PrismaClient;

// Prevent nextjs from trying to load prisma in the browser
if (typeof window === 'undefined') {
  if (process.env.NODE_ENV === 'production') {
    prisma = prismaClientSingleton();
  } else {
    if (!globalThis.prismaGlobal) {
      globalThis.prismaGlobal = prismaClientSingleton();

      // @ts-ignore
      globalThis.prismaGlobal.$on('query', (e: any) => {
        console.log(`Query: ${e.query} ${e.params}`);
        console.log(`Duration: ${e.duration}ms`);
      });
    }
    prisma = globalThis.prismaGlobal;
  }
} else {
  prisma = {} as PrismaClient;
}

export default prisma;
Avatar
Silver MartenOP
When I rip out all the redis stuff, the app builds fine. For some reason nextjs is trying to connect to redis during the build process and it's failing since none of the containers are up and running. I have no idea why it doesn't fail when trying to connect to the database though since the database is also not running at build time. Do I have to create a mock redis object that just does nothing so that it can be called during build time without erroring out?