next: 14.0.3
next-auth: 4.24.5
: app router사용 중이고, api를 추가한 후 Build 중 error발생
⇒ pre-rendering 중 발생한 에러라는 내용, 경로도 잘못됬다는 내용
Error occurred prerendering page "/api/tradingpost/withdraw/limit". Read more: https://nextjs.org/docs/messages/prerender-error
Export encountered errors on following paths: /api/tradingpost/withdraw/limit/route: /api/tradingpost/withdraw/limit Creating an optimized production build .
getServerSession(authOption)
이 try-catch 문 내부에서 실행되도록 되어있음.
// /api/tradingpost/withdraw/limit/route.ts
export async function GET(req: NextRequest, res: NextResponse) {
try {
const session = (await getServerSession(authOption)) as sessionType;
const gold = JSON.parse(req.nextUrl.searchParams.get('gold') ?? '');
...
const response = await AxiosServer.get(api, {
params: query
}).then((res) => {
return res.data;
});
return new Response(JSON.stringify(response));
} catch (error: any) {
return NextResponse.json(
{
client_code: error.response.data.client_code,
client_message: error.response.data.client_message,
description: error.response.data.description
},
{ status: error.response.data.status }
);
}
}
getServerSession(authOption)
이 try-catch 문 이전에 실행되도록 수정함.
export async function GET(req: NextRequest, res: NextResponse) {
//! 수정된 부분
const session = (await getServerSession(authOption)) as sessionType;
try {
const gold = JSON.parse(req.nextUrl.searchParams.get('gold') ?? '');
...
const response = await AxiosServer.get(api, {
params: query
}).then((res) => {
return res.data;
});
return new Response(JSON.stringify(response));
} catch (error: any) {
return NextResponse.json(
{
client_code: error.response.data.client_code,
client_message: error.response.data.client_message,
description: error.response.data.description
},
{ status: error.response.data.status }
);
}
}
어떠한 이유로 발생 되는지 찾지못함… next render 에 대한 study가 더 필요함.
next-auth.getServerSession 사용은 try-catch 문내에서 사용하지말고, api 함수 내 최상단에서 사용해야한다.