-
I am running I am not using statically generated pages so I expect that this shouldn't be a problem. All my pages are dynamic and so Next shouldn't have to run my backend to generate the frontend bundle. However, I am getting errors like:
What is happening? What is "collecting page data", can I see the pages that the data is being collected for, and how can I turn it off? |
Beta Was this translation helpful? Give feedback.
Replies: 21 comments 109 replies
-
As far as I know "collecting page data" is a normal and necessary step of generating a production bundle with Next. It appears the |
Beta Was this translation helpful? Give feedback.
-
When you build a Next.js application, actual HTML is generated, so at build time, the build step runs functions such as If you don't want that to happen, use This is actually a good thing Next.js does with |
Beta Was this translation helpful? Give feedback.
-
Same here, Executing api handlers and pages with |
Beta Was this translation helpful? Give feedback.
-
@neongreen My setup might not apply to you, I'm running The reason I was getting the
- name: Create env file
run: |
touch .env
echo 'DATABASE_URL="${{ secrets.PRISMA_DATABASE_URL }}"' >> .env I prefer this solution.
and use it in ARG DATABASE_URL
ENV DATABASE_URL=$DATABASE_URL For the "10 instances" warning, I've reverted back to the solution in the article I've mentioned, since I've fixed the environment variable issue: // client.ts
import { PrismaClient } from '@prisma/client'
declare global {
// allow global `var` declarations
// eslint-disable-next-line no-var
var prisma: PrismaClient | undefined
}
export const prisma =
global.prisma ||
new PrismaClient({
log: ['query'],
})
if (process.env.NODE_ENV !== 'production') global.prisma = prisma |
Beta Was this translation helpful? Give feedback.
-
I have the same recurring issue that at build time, next is trying to render and start a lot of page lifecycles. Things like getting data from process.env to startup cache generation, render time fetch data, singleton initialisation, etc are triggering during build time and in our CI environment (the client env to be precise) it's not possible to let all this code execute when the only objective at this step is build a server ready to boot. Given the nature of the application, we don't have page that can be statically rendered, we just need the server side rendering and chunk management from the nextjs solution, and this feature is causing a lot of failed builds and rewrites of imports to async as a workaround. example : const getServerSideProps =
async ({ req, params: { documentId, documentType, siteId } = {} }) => {
const { delegation } = await import("@/services/delegation");
const {
getVatCat,
getTermTimes,
getIncotermCode,
getIncotermPlace,
getPaymentMode,
getCountry,
getTransportMode,
} = await import("@/services/referentials");
const { locale } = getLocaleFromRequest(req); export function envThrow(key: string): string {
if (process.env.hasOwnProperty(key)) {
return process.env[key] as string;
} else if (process.env.NEXT_PHASE !== "phase-production-build") {
throw new Error(`missing critical environment variable : ${key}`);
} else {
return "0";
}
} One thing that is executed and that should maybe not, is getInitialProps of _app.ts. Would anyone want that I try to setup a repo to illustrate the issue or it's not useful to take that time ? |
Beta Was this translation helpful? Give feedback.
-
I'm having a similar issue to this. I work on an application to which NextJS doesn't seem at all well suited. There is no place for SSR in this app at all. I would like to remove NextJS, however for the immediate future this isn't a practical option. In order to update react and other packages, I was forced to update from NextJS 8 to NextJS 10 (going further looked to be a big headache due to other dependencies). This appears to have completely changed the build process, which I think is now trying to build a static HTML file for each page. I now get a few "Collecting page data" output lines followed by an error (copied below) that gives me almost no detail. I've looked at the build JS file in question and to trace it back to any source code would be a big challenge. Ideally, I'd just revert the build process back to what it used to be which I don't think attempted any HTML generation. Any advice on this would be hugely appreciated! info - Collecting page data ...node:internal/process/promises:279 TypeError: Cannot read properties of undefined (reading 'N5') |
Beta Was this translation helpful? Give feedback.
-
Im running into a similar problem with using |
Beta Was this translation helpful? Give feedback.
-
This stuff is extremely problematic during build scenarios because it assumes a working server with environment setup. I understand the value. But it should catch any errors during this process and warn instead of throw them. |
Beta Was this translation helpful? Give feedback.
-
Yes, could we perhaps be provided with a flag to disable this "prerendering" test during collecting page data which make use of getServerSideProps? Or demote the errors to warnings as mentioned above? |
Beta Was this translation helpful? Give feedback.
-
Here is an easy way to reproduce this using the
This fails with:
@icyJoseph it sounds like you wanted to see a reproducible example, would this give you what you need? |
Beta Was this translation helpful? Give feedback.
-
In Nextjs 13 on api under app directory (Route Handlers) also it fails with the same issue : Error: Failed to collect page data for /api/#####/##### |
Beta Was this translation helpful? Give feedback.
-
I have an api route that I am setting up for a cron job. It is a post that requires a env var to be passed so that I can prevent it from running via publicly accessible url (need to know the secret). However, it fails on build because of this due to it throwing an error as it does not pass anything in the post request, let alone the secret.
I can change it to return a different response that isn't an error, but that seems dumb. I should be able to bypass this for an api route. |
Beta Was this translation helpful? Give feedback.
-
(I'd like to preface this with: I don't know what I'm doing, I'm not a frontend dev, I'm just trying to package an app built with next-js) I think this disables the pre-rendering of pages:
I'm yet not sure if the resulting app will work 100% correctly. |
Beta Was this translation helpful? Give feedback.
-
so how do I make it work with a const myVar = process.env. ...
if (!myVar) {
throw new Error("Must provide the env var");
}
export async function GET() {
} Thanks |
Beta Was this translation helpful? Give feedback.
-
Just ran into this. In my situation, the place where the database is supposed to be does not really exist during the build process while the A somewhat okay solution to this problem could've been next warning us that the page data does not exist & default that page to being dynamically served. Now just to workaround this, I'd have to create a mock db which is a bummer. 🙁 |
Beta Was this translation helpful? Give feedback.
-
Although this is a pretty old discussion, I wanted to share how I addressed this issue: I have a file that exports a import * as Minio from "minio";
import { env } from "@/env";
export const s3 = new Minio.Client({
endPoint: env.S3_ENDPOINT,
port: env.S3_PORT,
useSSL: env.S3_USE_SSL,
accessKey: env.S3_ACCESS_KEY,
secretKey: env.S3_SECRET_KEY,
});
export const BUCKET_NAME = env.S3_BUCKET_NAME; I have this file imported in several places; this works well using What I did to fix this was removing every From what I could tell, node itself evaluates any top-level definitions of files when they are imported statically using the Am I in the right here? Would love for some clarification. |
Beta Was this translation helpful? Give feedback.
-
Got some error after updating to next 15 with output: "standalone" :( |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I still have this issue, please remove the |
Beta Was this translation helpful? Give feedback.
-
I shouldn't be required to connect to database at build time for pages that read database at request time. |
Beta Was this translation helpful? Give feedback.
-
I got the same error because I used to check required environment variables during runtime, like DATABASE_URL. |
Beta Was this translation helpful? Give feedback.
When you build a Next.js application, actual HTML is generated, so at build time, the build step runs functions such as
getStaticProps
, to pre-render pages, it also tries to figure out which pages can be build already, and does it. It is an optimisation that's welcomed.If you don't want that to happen, use
getServerSideProps
on the page that's trying to collect build time data, and then you should be fine. However, at run time, DATABASE_URL and all things necessary to render that page, better be present!This is actually a good thing Next.js does with
getStaticProps
, it sort of type checks and makes sure things are in place do that it can work well in production.getServerSideProps
runs …