C.W.K.
Stream
Lesson 02 of 09 · published

Proxy (옛 Middleware)

~22 min · proxy, middleware, Node runtime

Level 0Curious
0 XP0/68 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

큰 rename

Next.js 16 에서 middleware.tsproxy.ts 로 rename 됨, export function 이 middleware() 에서 proxy() 로 변경. Default runtime 도 Edge 에서 Node.js 로 이동 (v15.5 부터).

어디 사는지

proxy.ts 가 project root, app/ 옆에. matcher config 와 일치하는 모든 request 에 돌아 — route render 전에.

왜 rename

옛 이름이 "React rendering 의 가운데에서" 라고 시사했어. 안 그래 — network 와 route 사이에 앉음, reverse proxy 처럼. 새 이름이 그걸 반영. Codemod 가 본인 위해 rename 해줘.

Code

Project root 의 proxy.ts·ts
// proxy.ts
import { NextRequest, NextResponse } from 'next/server';

export function proxy(request: NextRequest) {
  const session = request.cookies.get('session')?.value;
  if (!session && request.nextUrl.pathname.startsWith('/dashboard')) {
    return NextResponse.redirect(new URL('/login', request.url));
  }
  return NextResponse.next();
}

export const config = {
  matcher: ['/dashboard/:path*', '/api/:path*'],
};
Matcher pattern·ts
export const config = {
  matcher: [
    '/dashboard/:path*',
    '/api/:path*',
    // Static 과 image asset skip
    '/((?!_next/static|_next/image|favicon.ico).*)',
  ],
};

External links

Exercise

기존 middleware.ts 를 codemod 사용해서 proxy.ts 로 migrate. 같은 matcher rule 여전히 적용되는지 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.