feat: actions and metadata #2
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Server Actions vs API Routes 비교
Next.js에서 서버 로직을 처리하는 두 가지 방법에 대한 비교 문서입니다.
📌 개요
Server Actions (RPC 방식)
"use server"지시어 사용RPC란?
API Routes (REST 방식)
fetch()로 호출해야 함1️⃣ Server Actions 방식
파일 구조
코드 예시
actions/user-actions.ts
클라이언트에서 사용
장점
revalidatePath로 캐시 자동 갱신💡 Progressive Enhancement란?
Server Actions는 HTML
<form>태그의 기본 동작을 활용합니다:JavaScript 없을 때:
JavaScript 있을 때:
결과: 모든 환경에서 작동하되, JavaScript가 있으면 더 나은 경험 제공!
핵심: Progressive Enhancement가 작동하려면
<form action="...">속성에 실제 작동하는 엔드포인트가 있어야 합니다. Next.js Server Actions는 이를 자동으로 생성해주므로, 우리는 함수만 작성하면 됩니다!🤔 언제 HTML Form을 사용할까?
역사적 맥락: 왜 요즘 다시 Form을 쓰는가?
React 시대 (2013~2023): Form을 거의 안 씀
문제점:
e.preventDefault()로 Form의 기본 동작을 완전히 무시 → JavaScript 필수Next.js Server Actions (2023~): Form이 다시 유용해짐
이유: Next.js가 자동으로 엔드포인트를 생성하고 Progressive Enhancement 지원!
Form이 유용한 경우 (Server Actions 사용)
Form이 불필요한 경우 (state 사용)
Form의 장점
일반 React (CRA, Vite 등):
→ JavaScript 없으면 완전히 작동 안 함. Form이 그냥
<div>랑 똑같음.Next.js API Routes만 있는 경우:
→ API 엔드포인트가 있어도, Form의
action에 연결되지 않으면 의미 없음!Next.js Server Actions:
→ Next.js가 자동으로 엔드포인트 생성 + Form과 연결 = Form이 의미 있음!
2️⃣ API Routes 방식
파일 구조
코드 예시
api/users/[id]/like/route.ts
api/users/route.ts
클라이언트에서 사용
Next.js 앱에서:
외부 클라이언트(React Native, iOS, Android)에서:
장점
🎯 실전 사용 예시
예시 1: 블로그 댓글 작성 (Next.js 앱 내부만 사용)
✅ Server Action 사용
예시 2: 결제 웹훅 (외부 서비스 → 내 서버)
✅ API Route 사용
외부 서비스 (Stripe)에서 호출:
예시 3: 관리자 대시보드
✅ 둘 다 사용 가능
예시 4: 사용자 인증
Server Action (폼 기반 로그인):
API Route (JWT 기반 인증):
🔍 Server Actions vs API Routes 상세 비교
코드 복잡도 비교
사용자 생성 예시
Server Actions (간단):
API Routes (복잡):
코드 양 비교: Server Actions 15줄 vs API Routes 30줄 (50% 감소!)
타입 안전성 비교
Server Actions:
API Routes:
RPC의 장단점
✅ Server Actions (RPC)의 장점:
❌ Server Actions의 단점:
✅ API Routes (REST)의 장점:
❌ API Routes의 단점:
🔄 함께 사용하는 경우
실제 프로젝트에서는 두 가지를 함께 사용하는 경우가 많습니다:
📝 현재 프로젝트 구조
현재
next-nest프로젝트에는 두 가지 방식이 모두 구현되어 있습니다:Server Action:
apps/web/app/actions/user-actions.tssearchUsers()함수API Route:
apps/web/app/api/users/route.tsGET /api/users?name=xxx엔드포인트이것은 학습 목적으로 두 가지 방식을 모두 보여주기 위한 것입니다. 실제 프로젝트에서는 용도에 맞게 하나를 선택하거나, 각각의 장점을 살려 함께 사용하면 됩니다.
🤔 선택 기준 요약
🎯 최종 정리
핵심 개념 4가지
1. RPC (Remote Procedure Call)
2. Progressive Enhancement
<form action="...">+ 실제 엔드포인트 필요3. HTML Form의 가치
4. Form이 의미 있으려면
권장사항
Next.js 앱만 사용하는 경우
→ Server Actions 우선 사용
모바일 앱이나 외부 서비스와 연동하는 경우
→ API Routes 사용
하이브리드 접근 (실전에서 가장 많이 사용)
→ 두 가지를 함께 사용