Next.js Discord

Discord Forum

Error occurred prerendering page

Unanswered
Bigeye tuna posted this in #help-forum
Open in Discord
Avatar
Bigeye tunaOP
My Next.js api downloads a file and sends it back to the client. It builds/works locally (probably because of .env file), however when ran in the pipeline (remote build), it does not receive any env variables.

export async function GET(_: NextRequest, res: Response) {
  const env = process.env.NODE_ENV;
  const baseUrl = process.env.CLIENT_DOWNLOAD_BASE_URL;
  let fileName: string | null = null;

  switch (env) {
    case "development": {
      fileName = process.env.CLIENT_DOWNLOAD_DEVELOP_FILENAME;
      break;
    }

    case "production": {
      fileName = process.env.CLIENT_DOWNLOAD_PROD_FILENAME;
      break;
    }
    default:
      throw new Error("Client file is not set for current environment.");
  }
  console.log("envs", process.env);
  const filePath = `${baseUrl}/${fileName}`;
  const response = await fetch(filePath);
  const buffer = await response.buffer();

  return new Response(buffer, {
    headers: {
      ...res.headers,
      "content-disposition": `attachment; filename=${fileName}`,
      "Content-Type": "application/zip",
    },
  });
}


I'm getting build error saying:
Error occurred prerendering page "/api/download/client". Read more: https://nextjs.org/docs/messages/prerender-error


I have configures envs in next.config.js, however none of them is available.
To make it clear: when deleting code above (accessing process.env in API routes), application builds and works as expected. Env variables used in other places are available.

0 Replies