_/Velog

Next.js 서버개발(/api/route.ts) pre-rendering 오류 해결

선달 2024. 9. 16. 03:24
반응형

 

Error occurred prerendering page "/api/generation". Read more: https://nextjs.org/docs/messages/prerender-error

 

Prerender Error with Next.js

Using App Router Features available in /app

nextjs.org

 

 

import { NextRequest, NextResponse } from "next/server";
import { myApi, pokeApi } from "../instance";
import { GenerationI } from "@/interface/response";

export async function GET(request: NextRequest) {
  const res = await pokeApi.get(`/generation`);
  const data = res.data;

  const count = data.count;

  const results = await Promise.all(
    Array.from({ length: count }, (_, index) => index + 1).map(async (item) => {
      const genRes = await myApi.get<GenerationI>(`/generation/${item}`);
      const genData = genRes.data;
      return {
        id: item,
        name: genData.name,
        pokemonCount: genData.pokemonCount,
        pokemonIndexes: genData.pokemonIndexes,
      };
    })
  );

  return NextResponse.json(results);
}

 

대충 이런식으로 next js에서 app router를 이용하여 api를 개발하고 있었다

근데 왜인지 특정 경로 하나에서만 빌드에러와 배포에러가 뜨는 것이였다

vsc에 밑줄도 안쳐져서 npm run build 해보기 전까지는 발견도 못한 이슈..

 

아래는 빌드 오류와 함께 나온 로그 전문이다

더보기
Error occurred prerendering page "/api/generation". Read more: https://nextjs.org/docs/messages/prerender-error

AggregateError
    at Z.from (/Users/guest1/Documents/seondal/PokemonQuiz/.next/server/chunks/688.js:22:23681)
    at w.<anonymous> (/Users/guest1/Documents/seondal/PokemonQuiz/.next/server/chunks/688.js:24:9611)
    at w.emit (node:events:518:28)
    at x.<computed> (/Users/guest1/Documents/seondal/PokemonQuiz/.next/server/chunks/688.js:1:13285)
    at ClientRequest.emit (node:events:518:28)
    at Socket.socketErrorListener (node:_http_client:500:9)
    at Socket.emit (node:events:518:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
    at aM.request (/Users/guest1/Documents/seondal/PokemonQuiz/.next/server/chunks/688.js:24:21578)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /Users/guest1/Documents/seondal/PokemonQuiz/.next/server/app/api/generation/route.js:1:1029
    at async Promise.all (index 0)
    at async u (/Users/guest1/Documents/seondal/PokemonQuiz/.next/server/app/api/generation/route.js:1:956)
    at async /Users/guest1/Documents/seondal/PokemonQuiz/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:42484
    at async eI.execute (/Users/guest1/Documents/seondal/PokemonQuiz/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:32486)
    at async eI.handle (/Users/guest1/Documents/seondal/PokemonQuiz/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:43737)
    at async exportAppRoute (/Users/guest1/Documents/seondal/PokemonQuiz/node_modules/next/dist/export/routes/app-route.js:77:26)
 ✓ Generating static pages (8/8) 

> Export encountered errors on following paths:
        /api/generation/route: /api/generation

 

https://stackoverflow.com/questions/77511955/build-next-js-error-occurred-prerendering-page-api-user

 

Build Next.js : Error occurred prerendering page "/api/user"

When I build my Next.js 14 app with app directory, I get : [Error]: Dynamic server usage: Page couldn't be rendered statically because it used `headers`. See more info here: https://nextjs.org/docs/

stackoverflow.com

 

결국 구글링 결과 찾아낸 해결법

export const dynamic = "force-dynamic";

 

문제가 생기는 파일 상단에 이 코드를 추가했더니 멀끔히 해결되었다.

다만 아직 다른 route.ts 파일은 전혀 문제가 없는데 왜 이 파일에서만 문제가 생긴건지 의문..

반응형