fix(hotfix-001): NEXT_PUBLIC_API_URL env 의존 제거 → Next.js rewrites 프록시

문제: FE 빌드 시 NEXT_PUBLIC_API_URL env 없으면 localhost:3005로 하드베이킹됨
→ 배포 환경에서 API 요청이 localhost로 가는 버그

해결:
- next.config.ts: /api/* → BE(API_URL) rewrites 추가
- lib/config.ts: CSR에서 API_URL='' (상대경로) 사용
  SSR에서는 process.env.API_URL ?? NEXT_PUBLIC_API_URL fallback
- lib/adminFetch.ts: 상대경로 직접 사용
- 모든 페이지가 use client이므로 CSR 상대경로 + rewrites로 완전 처리
- .env.local.example 업데이트 (API_URL 서버사이드용 추가)
This commit is contained in:
2026-04-04 12:13:23 +09:00
parent 033c142c0e
commit 3d8a1efa2a
3 changed files with 30 additions and 4 deletions

View File

@@ -1,12 +1,17 @@
import { API_URL } from './config';
/**
* 관리자 API 호출 유틸.
* API_URL이 빈 문자열이면 상대경로 → Next.js rewrites → BE 프록시.
* Authorization 헤더에 localStorage의 admin key 삽입.
*/
export function adminFetch(path: string, options?: RequestInit) {
const apiKey =
typeof window !== 'undefined'
? (localStorage.getItem('hanarang_admin_key') ?? '')
: '';
return fetch(`${API_URL}${path}`, {
// CSR: path가 /api/... 이면 상대경로 그대로 사용
// SSR에서는 adminFetch 미사용 (모든 admin 페이지가 'use client')
return fetch(path, {
...options,
headers: {
'Content-Type': 'application/json',

View File

@@ -1,4 +1,15 @@
export const API_URL = process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:3005';
/**
* API_URL: 프론트엔드에서 API 호출 시 사용하는 기본 URL.
*
* - 브라우저(CSR): 빈 문자열 → `/api/...` 상대경로 → Next.js rewrites → BE로 프록시
* - 서버사이드(SSR): 서버에서 직접 BE 호출 필요. API_URL_SERVER 사용.
* - Next.js rewrites는 브라우저 요청만 처리하므로 SSR에서는 절대 URL 필요.
*/
export const API_URL =
typeof window === 'undefined'
? (process.env.API_URL ?? process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:3005')
: '';
export const POLL_INTERVAL_MS = parseInt(
process.env.NEXT_PUBLIC_POLL_INTERVAL_MS ?? '30000',
10,

View File

@@ -1,9 +1,19 @@
import type { NextConfig } from "next";
const apiUrl = process.env.NEXT_PUBLIC_API_URL ?? process.env.API_URL ?? 'http://localhost:3005';
const nextConfig: NextConfig = {
compiler: {
styledComponents: true,
},
async rewrites() {
return [
{
source: '/api/:path*',
destination: `${apiUrl}/api/:path*`,
},
];
},
};
export default nextConfig;