1. ํ์ ์ฌ์ ์๊ฑด & ์์คํ ์๊ตฌ์ฌ์ (Prerequisites & System Requirements)
๐ป ์์คํ ์ค์น ๋ฐ ์คํ ์๊ตฌ์ฌ์ (System Requirements)
| ํญ๋ชฉ | ์ต์ ์ฌ์ (Minimum) | ๊ถ์ฅ ์ฌ์ (Recommended) | ์ฐธ๊ณ ๋ฐ ๋น๊ณ |
|---|---|---|---|
| ์ด์์ฒด์ (OS) | Windows 10/11, macOS 12+, Linux (Ubuntu, Debian, Fedora) | macOS 14+ (Apple Silicon), Ubuntu 22.04/24.04 LTS, Windows 11 | ํฌ๋ก์ค ํ๋ซํผ ์๋ฒฝ ํธํ |
| CPU | 2 ์ฝ์ด ์ด์ (x86_64 / ARM64) | 4 ์ฝ์ด ~ 8 ์ฝ์ด ์ด์ | Vite HMR ๋ฐ routeTree.gen.ts ๊ณ ์ ํ์ผ ๊ฐ์ ๋น๋ |
| ๋ฉ๋ชจ๋ฆฌ (RAM) | 2 GB ์ด์ | 4 GB ~ 8 GB ์ด์ | Vite SSR ๋ฒ๋ค๋ฌ + Nitro ์๋ฒ ์์ง ๋์ ๊ตฌ๋ |
| ๋์คํฌ (Storage) | 500 MB ์ด์์ ์ฌ์ ๊ณต๊ฐ | 2 GB ~ 5 GB ์ด์์ ๊ณ ์ NVMe SSD | node_modules ๋ฐ .vinxi/ / .nitro/ ์บ์ |
| ๋คํธ์ํฌ / ๋ฐํ์ | Node.js v20.0.0+ LTS ๋๋ Bun 1.1+ | Node.js v22/v24 LTS ๋๋ Bun 1.2+ / pnpm 9+ | TypeScript 5.4+, React 19/18, Vite 6/7 ํธํ |
[!IMPORTANT] ์ TanStack Start & Router์ธ๊ฐ? (100% ์์ ํ์ ์์ ์ ํ์ ): ๊ธฐ์กด Next.js๋ React Router๋ URL ๊ฒฝ๋ก๋ฅผ ๋ฌธ์์ด(
'/users/' + id)๋ก ๋๊ธฐ๊ฑฐ๋ ์ฟผ๋ฆฌ์คํธ๋ง(?page=1)์ ๋ฌธ์์ด๋ก ๋ค๋ฃจ๊ธฐ ๋๋ฌธ์, ๊ฒฝ๋ก ์คํ๋ ํ๋ผ๋ฏธํฐ ๋๋ฝ์ ๋ฐํ์ 404๊ฐ ํฐ์ง ํ์์ผ ๋ฐ๊ฒฌํ๋ ์น๋ช ์ ์ธ ํ๊ณ๊ฐ ์์์ต๋๋ค.
TanStack Router & Start๋ Vite ํ๋ฌ๊ทธ์ธ์ด ํ์ผ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ํ์ฌrouteTree.gen.ts๋ฅผ ์ค์๊ฐ ์๋ ์์ฑํฉ๋๋ค. ๋ฐ๋ผ์ ๊ฒฝ๋ก๋ช , URL ํ๋ผ๋ฏธํฐ($postId), ์ฟผ๋ฆฌ์คํธ๋ง(search), ์๋ฒ ํจ์ ์ ์ถ๋ ฅ(createServerFn), ๋ผ์ฐํธ ๋ก๋(loader)๊น์ง ๋ชจ๋ ๋จ๊ณ์์ 1๊ธ์์ ์คํ๋ TypeScript ์ปดํ์ผ ์๋ฌ๋ก ์ฌ์ ์ฐจ๋จํฉ๋๋ค!
๐ ๏ธ 1. ์ ๊ท ํ๋ก์ ํธ ์์ฑ ๋ฐ ๊ฐ๋ฐ ์๋ฒ ๊ธฐ๋
TanStack Start๋ ๊ณต์ ์คํํฐ CLI๋ฅผ ์ ๊ณตํ๋ฉฐ, ๊ธฐ๋ณธ์ ์ผ๋ก Vite ๋ฐ Nitro SSR ์์ง์ ๊ธฐ๋ฐ์ผ๋ก ๋ฒ๋ค๋ง๋ฉ๋๋ค.
# 1. TanStack Start ์ต์ ํ๋ก์ ํธ ์์ฑ
npm create @tanstack/start@latest my-tanstack-app
# ๋๋ pnpm ์ฌ์ฉ ์:
# pnpm create @tanstack/start my-tanstack-app
# ๋๋ bun ์ฌ์ฉ ์:
# bun create @tanstack/start my-tanstack-app
# 2. ํ๋ก์ ํธ ๋๋ ํ ๋ฆฌ ์ด๋ ๋ฐ ์์กด์ฑ ํ์ธ
cd my-tanstack-app
# 3. ๋ก์ปฌ ๊ฐ๋ฐ ์๋ฒ ์คํ (๊ธฐ๋ณธ ํฌํธ: 3000)
npm run dev
์ฝ์ ์ถ๋ ฅ ์์ (npm run dev):
vite v6.2.0 dev server running at:
โ Local: http://localhost:3000/
โ Network: use --host to expose
[tanstack-router] Generated route tree: src/routeTree.gen.ts
ํ์ค ๋๋ ํ ๋ฆฌ ๊ตฌ์กฐ (Standard Project Layout):
my-tanstack-app/
โโโ app/
โ โโโ routes/ # ํ์ผ ๊ธฐ๋ฐ ๋ผ์ฐํธ ๋๋ ํ ๋ฆฌ
โ โ โโโ __root.tsx # ์ต์์ ๋ฃจํธ ๋ ์ด์์ (<html>, <Outlet />, ์ ์ญ Devtools)
โ โ โโโ index.tsx # ๋ฉ์ธ ํ์ด์ง ("/")
โ โ โโโ about.tsx # ์ ์ ํ์ด์ง ("/about")
โ โ โโโ posts.tsx # ํฌ์คํธ ๊ณตํต ๋ ์ด์์ ("/posts")
โ โ โโโ posts.index.tsx # ํฌ์คํธ ๋ชฉ๋ก ("/posts/")
โ โ โโโ posts.$postId.tsx # ๋์ ์์ธ ํ์ด์ง ("/posts/:postId")
โ โโโ client.tsx # ํด๋ผ์ด์ธํธ ์ํธ๋ฆฌํฌ์ธํธ (createRouter ํ์ด๋๋ ์ด์
)
โ โโโ routeTree.gen.ts # โจ ๋ผ์ฐํฐ ํ๋ฌ๊ทธ์ธ์ด ์๋ ์์ฑํ๋ ํ์
ํธ๋ฆฌ (์์ ๊ธ์ง!)
โ โโโ router.tsx # ๋ผ์ฐํฐ ์ธ์คํด์ค ํฉํ ๋ฆฌ (createRouter)
โ โโโ ssr.tsx # ์๋ฒ ์ฌ์ด๋ ๋ ๋๋ง ์ํธ๋ฆฌํฌ์ธํธ (defaultStreamHandler)
โโโ public/ # ์ ์ ํ์ผ (ํ๋น์ฝ, ์ด๋ฏธ์ง ๋ฑ)
โโโ app.config.ts # TanStack Start ํ๋ ์์ํฌ ์ค์ (Vite + Vinxi/Nitro)
โโโ tsconfig.json # TypeScript ์ปดํ์ผ๋ฌ ์ค์
โโโ package.json # ์์กด์ฑ ํจํค์ง ๋ช
์ธ
2. ์ง๊ด์ ์ธ ๋ฉํ ๋ชจ๋ธ (Mental Model & Architecture Diagram)
TanStack Start๋ ๋ธ๋ผ์ฐ์ ์ ์๋ฒ ๊ฐ์ ๋ฐ์ดํฐ ํ๋ฆ์ ์ ๋ฐํ 6๋จ๊ณ ๋จ๋ฐฉํฅ ํ์ดํ๋ผ์ธ์ผ๋ก ์ฐ๊ฒฐํฉ๋๋ค.
+---------------------------------------------------------------------------------------------------+
| TanStack Start & Router Request Lifecycle Pipeline |
+---------------------------------------------------------------------------------------------------+
| |
| [ ๋ธ๋ผ์ฐ์ ์์ฒญ URL ] ์: /posts/42?sort=desc&page=1 |
| โ |
| โผ |
| โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ |
| โ 1. Search Params Parsing & Validation (`validateSearch` with Zod) โ |
| โ - URL ์ฟผ๋ฆฌ์คํธ๋ง ๋ฌธ์์ด์ Zod ์คํค๋ง๋ก ๊ฒ์ฆํ์ฌ ์๋ฒฝํ ๊ฐ์ฒด({ sort: 'desc', page: 1 })๋ก ํ์ฑ โ |
| โ - ์ ํจํ์ง ์์ ํ๋ผ๋ฏธํฐ๋ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์๋ ์นํ ๋๋ ์๋ฌ ์ฒ๋ฆฌ โ |
| โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ |
| โ Type-Safe Search Context ์ฃผ์
|
| โผ |
| โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ |
| โ 2. Route Matching & Authentication Guard (`beforeLoad`) โ |
| โ - ์ค์ฒฉ ๋ ์ด์์(Parent โ Child) ์์ฐจ ์คํ โ |
| โ - ๋ก๊ทธ์ธ ์ธ์ฆ ์ฌ๋ถ ์ฒดํฌ โ ๋ฏธ์ธ์ฆ ์ `throw redirect({ to: '/login' })` ์ฆ์ ์ธํฐ์
ํธ โ |
| โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ |
| โ Context ์ ๋ฌ |
| โผ |
| โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ |
| โ 3. Parallel Preloading & Server Function (`loader` & `createServerFn`) โ |
| โ - ๋ชจ๋ ๋งค์นญ๋ ์ค์ฒฉ ๋ผ์ฐํธ์ loader๋ฅผ ์ํฐํด ์์ด "๋ณ๋ ฌ(Parallel)"๋ก ๋์ ์คํ โ |
| โ - `createServerFn` RPC ํจ์๋ฅผ ํตํด ์์ ํ๊ฒ ์๋ฒ DB/ORM ์ง์ ์กฐํ โ |
| โ - ๋งํฌ์ ๋ง์ฐ์ค ํธ๋ฒ ์ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ๋ฐ์ดํฐ๋ฅผ 0.1์ด ๋ง์ ๋ฏธ๋ฆฌ ํ๋ฆฌํ์นญ(Prefetch) โ |
| โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ |
| โ loaderData ์ฃผ์
|
| โผ |
| โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ |
| โ 4. Type-Safe UI Component Rendering (`component`) โ |
| โ - `Route.useLoaderData()`๋ฅผ ํตํด ํ์
์ ์ธ ์์ด๋ 100% ์์ ํ๊ฒ ๋ฐ์ดํฐ ๋ ๋๋ง โ |
| โ - ๋ก๋ฉ ์ค์๋ `pendingComponent`, ์คํจ ์ `errorComponent` ๊ฒฉ๋ฆฌ ๋ ๋๋ง โ |
| โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ |
| โ ์ฌ์ฉ์ ์
๋ ฅ / ํผ ์ ์ถ |
| โผ |
| โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ |
| โ 5. Server Mutation (`createServerFn` or `useMutation`) โ |
| โ - ํด๋ผ์ด์ธํธ์์ ํ์
์์ ํ ํ๋ผ๋ฏธํฐ๋ก ์๋ฒ ํจ์ RPC ์ง์ ํธ์ถ โ |
| โ - ์๋ฒ์์ Zod ์
๋ ฅ๊ฐ ๊ฒ์ฆ ํ DB INSERT / UPDATE / DELETE ์ํ โ |
| โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ |
| โ |
| ๐ 6. Automatic Invalidation: `router.invalidate()` ํธ์ถ ์ ๋ณ๊ฒฝ๋ ๋ฐ์ดํฐ๋ง ๋ฐฑ๊ทธ๋ผ์ด๋ ์ฌ์กฐํ |
+---------------------------------------------------------------------------------------------------+
โ๏ธ TanStack Start vs Next.js vs Remix(React Router 7) ๊ธฐ์ ๋น๊ต
| ๋น๊ต ํญ๋ชฉ | TanStack Start | Next.js 15 (App Router) | Remix & React Router 7 |
|---|---|---|---|
| ํ์ ์์ ์ฑ (Type Safety) | 100% ์์ ํ์ ์์ (๊ฒฝ๋ก, ํ๋ผ๋ฏธํฐ, ์ฟผ๋ฆฌ์คํธ๋ง, ๋ก๋ ๋ชจ๋ ์ปดํ์ผ ํ์ ๊ฒ์ฆ) | ๋ถ๋ถ ์ง์ (next/link ์ ์ ๊ฒ์ฌ ์์ค, Search Params ์๋ ํ์ดํ) | Route Module Typegen (+types) ์๋ ์์ฑ ์ง์ |
| ์ฟผ๋ฆฌ์คํธ๋ง (Search Params) | 1๊ธ ์๋ฏผ(First-Class): Zod ๊ฒ์ฆ, ๊ฐ์ฒด/์ซ์/๋ถ๋ฆฌ์ธ ํ์ ์๋ ์บ์คํ | ๋ฌธ์์ด ๊ธฐ๋ฐ searchParams (์๋ ํ์ฑ ๋ฐ ๊ฒ์ฆ ํ์) | request.url ๋ฌธ์์ด ํ์ฑ (์๋ ๊ฒ์ฆ) |
| ์๋ฒ ํจ์ ํธ์ถ (RPC) | createServerFn (Zod ๊ฒ์ฆ๊ธฐ ๋ฐ ๋ฏธ๋ค์จ์ด ํ์ดํ๋ผ์ธ ์๋ฒฝ ๊ฒฐํฉ) | 'use server' Server Actions | <Form> ์ ์ถ + ๋ผ์ฐํธ ๋ชจ๋ action ํจ์ |
| ๋ฐ์ดํฐ ํ์นญ ํตํฉ | TanStack Query (@tanstack/react-query) ์๋ฒฝ ๋ค์ดํฐ๋ธ ์กฐํ | React fetch ํจ์น ๊ธฐ๋ฐ ์บ์ ๋ฐ use ํ
| ๋ผ์ฐํธ loader ๋จ๋ฐฉํฅ ์ฌ์ดํด |
| ๋ฐํ์ ๋ฐฐํฌ ์ ์ฐ์ฑ | Nitro ๊ธฐ๋ฐ: Node.js, Vercel, Cloudflare, Bun, Docker ์ด๋๋ ์ํด๋ฆญ ์ด๋ํฐ | Node.js ๋ฐ Vercel์ ์ต์ ํ๋จ | Web Fetch API ํ์ค ์ค์ (Cloudflare, Node, Bun) |
3. [์ด๋ณด์ 1๋จ๊ณ] ํฌ๋ก์๋: ํ์ผ ๊ธฐ๋ฐ ๋ผ์ฐํ ๊ณผ ํ์ ์์ ํ ๋ฃจํธ ๊ตฌ์ฑ
๐ 1. ์ต์์ ๋ฃจํธ ๋ ์ด์์ (app/routes/__root.tsx)
๋ชจ๋ ํ์ ํ์ด์ง์ ๊ณตํต HTML ๊ณจ๊ฒฉ(<html>, <head>, <body>), ๊ธ๋ก๋ฒ ๋ด๋น๊ฒ์ด์
๋ฐ, ๊ทธ๋ฆฌ๊ณ ๋ผ์ฐํฐ ๋๋ฒ๊น
์ ์ํ TanStackRouterDevtools๋ฅผ ๋ฐฐ์นํฉ๋๋ค.
// app/routes/__root.tsx
import { createRootRoute, Link, Outlet, ScrollRestoration } from '@tanstack/react-router';
import { TanStackRouterDevtools } from '@tanstack/router-devtools';
import { Meta, Scripts } from '@tanstack/start';
import type { ReactNode } from 'react';
export const Route = createRootRoute({
head: () => ({
meta: [
{ charSet: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ title: 'TanStack Start & Router ์๋ฒฝ ๊ฐ์ด๋' },
],
}),
component: RootComponent,
});
function RootComponent() {
return (
<html lang="ko">
<head>
<Meta />
</head>
<body style={{ margin: 0, fontFamily: 'system-ui, -apple-system, sans-serif' }}>
{/* ๊ณตํต ๋ด๋น๊ฒ์ด์
๋ฐ */}
<header style={{ padding: '1rem 2rem', background: '#0f172a', color: '#ffffff', display: 'flex', gap: '1.5rem', alignItems: 'center' }}>
<strong style={{ fontSize: '1.1rem', color: '#38bdf8' }}>๐๏ธ TanStack Start</strong>
<nav style={{ display: 'flex', gap: '1rem' }}>
{/* ์คํ๊ฐ ๋๋ฉด TypeScript ์ปดํ์ผ ๋จ๊ณ์์ ๋นจ๊ฐ ์ค๋ก ์ค๋ฅ๋ฅผ ์ก์! */}
<Link to="/" activeProps={{ style: { fontWeight: 'bold', color: '#38bdf8' } }} style={{ color: '#e2e8f0', textDecoration: 'none' }}>
ํ
</Link>
<Link to="/posts" activeProps={{ style: { fontWeight: 'bold', color: '#38bdf8' } }} style={{ color: '#e2e8f0', textDecoration: 'none' }}>
๊ฒ์๊ธ ๋ชฉ๋ก
</Link>
</nav>
</header>
{/* ํ์ ๋ผ์ฐํธ ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง๋๋ ์์น */}
<main style={{ maxWidth: '900px', margin: '2rem auto', padding: '0 1rem' }}>
<Outlet />
</main>
<ScrollRestoration />
<Scripts />
{/* ๊ฐ๋ฐ ํ๊ฒฝ ์ ์ฉ ๋ผ์ฐํฐ ์ธ์คํํฐ ํจ๋ */}
<TanStackRouterDevtools position="bottom-right" />
</body>
</html>
);
}
๐ 2. ๋ฉ์ธ ์ธ๋ฑ์ค ํ์ด์ง (app/routes/index.tsx)
// app/routes/index.tsx
import { createFileRoute, Link } from '@tanstack/react-router';
export const Route = createFileRoute('/')({
component: HomeComponent,
});
function HomeComponent() {
return (
<section>
<h1>๐ ํ์ํฉ๋๋ค! TanStack Start ํ์คํ ์ํคํ
์ฒ</h1>
<p style={{ color: '#64748b', fontSize: '1.05rem', lineHeight: 1.6 }}>
TanStack Router๋ URL ๊ฒฝ๋ก(`to`), ํ๋ผ๋ฏธํฐ(`params`), ์ฟผ๋ฆฌ์คํธ๋ง(`search`)์ ๋ํด
<strong>100% ์์ ํ End-to-End ํ์
์์ ์ฑ</strong>์ ๋ณด์ฅํฉ๋๋ค.
</p>
<div style={{ marginTop: '1.5rem' }}>
{/* ๋งํฌ์ ๋ง์ฐ์ค๋ฅผ ์ฌ๋ฆฌ๋ฉด(preload="intent") ํด๋น ๋ผ์ฐํธ์ ์ฝ๋์ ๋ฐ์ดํฐ๋ฅผ ์ฆ์ ์ฌ์ ๋ก๋! */}
<Link
to="/posts"
preload="intent"
style={{
display: 'inline-block',
padding: '0.65rem 1.25rem',
background: '#0284c7',
color: '#ffffff',
borderRadius: '6px',
textDecoration: 'none',
fontWeight: 600,
}}
>
ํฌ์คํธ ๋๋ฌ๋ณด๊ธฐ (Preload on Hover) โ
</Link>
</div>
</section>
);
}
4. [2๋จ๊ณ] ํต์ฌ ๊ธฐ๋ฅ 1: Type-Safe Search Params & Zod ์คํค๋ง ๊ฒ์ฆ
TanStack Router์ ๊ฐ์ฅ ๋
๋ณด์ ์ธ ์ฅ์ ์ URL ์ฟผ๋ฆฌ์คํธ๋ง(?page=2&sort=asc&filter=tech)์ ๊ฐ์ฒด ์ํ์ฒ๋ผ ์๊ฒฉํ๊ฒ ๊ด๋ฆฌํ ์ ์๋ค๋ ์ ์
๋๋ค.
// app/routes/products.tsx
import { createFileRoute, Link, useNavigate } from '@tanstack/react-router';
import { z } from 'zod';
// 1. Zod๋ฅผ ํ์ฉํ ์ฟผ๋ฆฌ์คํธ๋ง ์คํค๋ง ์ ์ (๊ธฐ๋ณธ๊ฐ ๋ฐ ํ์
๋ณํ ํฌํจ)
const productSearchSchema = z.object({
page: z.number().catch(1),
sort: z.enum(['newest', 'price_asc', 'price_desc']).catch('newest'),
q: z.string().optional().catch(''),
});
type ProductSearch = z.infer<typeof productSearchSchema>;
export const Route = createFileRoute('/products')({
// 2. validateSearch ์ต์
์ผ๋ก ์คํค๋ง ๋ฑ๋ก: ์๋ชป๋ ์ฟผ๋ฆฌ๋ catch์ ์ํด ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์๋ ๊ต์ ๋จ!
validateSearch: (search: Record<string, unknown>): ProductSearch => {
return productSearchSchema.parse(search);
},
component: ProductsComponent,
});
function ProductsComponent() {
// 3. useSearch()๋ฅผ ํธ์ถํ๋ฉด ๋ฐํ๊ฐ์ ํ์
์ด ์๋ฒฝํ๊ฒ { page: number, sort: string, q?: string }๋ก ์ถ๋ก ๋จ!
const { page, sort, q } = Route.useSearch();
const navigate = useNavigate({ from: Route.fullPath });
return (
<div>
<h2>๐๏ธ ์ํ ํ์ (Type-Safe Search Params)</h2>
{/* ํ์ฌ ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ ์ํ ํ์ธ */}
<div style={{ background: '#f8fafc', padding: '1rem', borderRadius: '8px', marginBottom: '1.5rem' }}>
<p>ํ์ฌ ํ์ด์ง: <strong>{page}</strong></p>
<p>์ ๋ ฌ ๊ธฐ์ค: <strong>{sort}</strong></p>
<p>๊ฒ์์ด: <strong>{q || '์์'}</strong></p>
</div>
{/* ์ ๋ ฌ ํํฐ ๋ณ๊ฒฝ: search ํ๋ผ๋ฏธํฐ๋ฅผ ํจ์ํ์ผ๋ก ๊ฐฑ์ */}
<div style={{ display: 'flex', gap: '0.5rem', marginBottom: '1rem' }}>
<button
onClick={() => navigate({ search: (prev) => ({ ...prev, sort: 'newest', page: 1 }) })}
style={{ fontWeight: sort === 'newest' ? 'bold' : 'normal' }}
>
์ต์ ์
</button>
<button
onClick={() => navigate({ search: (prev) => ({ ...prev, sort: 'price_asc', page: 1 }) })}
style={{ fontWeight: sort === 'price_asc' ? 'bold' : 'normal' }}
>
๋ฎ์ ๊ฐ๊ฒฉ์
</button>
</div>
{/* ๋ค์ ํ์ด์ง ๋งํฌ: ์ปดํ์ผ๋ฌ๊ฐ search ์์ฑ์ ๋๋ฝ๋ ํ์ ํ๋๊ฐ ์๋์ง ๊ฒ์ฆ */}
<Link
to="/products"
search={(prev) => ({ ...prev, page: prev.page + 1 })}
style={{ color: '#0284c7', fontWeight: 600 }}
>
๋ค์ ํ์ด์ง ({page + 1}) ์ด๋ โ
</Link>
</div>
);
}
5. [2๋จ๊ณ] ํต์ฌ ๊ธฐ๋ฅ 2: createServerFn ์๋ฒ ํจ์์ loader ๋ฐ์ดํฐ ํ๋ฆฌํ์นญ
TanStack Start๋ ๋ฐฑ์๋ REST API๋ฅผ ๋ณ๋๋ก ๋ง๋ค ํ์ ์์ด, **ํด๋ผ์ด์ธํธ์์ ์ง์ ํธ์ถ ๊ฐ๋ฅํ RPC ์คํ์ผ์ createServerFn**์ ์ ๊ณตํฉ๋๋ค.
// app/routes/posts.$postId.tsx
import { createFileRoute, notFound } from '@tanstack/react-router';
import { createServerFn } from '@tanstack/start';
import { z } from 'zod';
// ============================================================================
// 1. [Server RPC] ์๋ฒ ์ ์ฉ ๋ฐ์ดํฐ ์กฐํ ํจ์ ์ ์ (ํด๋ผ์ด์ธํธ ๋ฒ๋ค์๋ RPC ํ๋ก์๋ง ๋จ์)
// ============================================================================
export const fetchPostById = createServerFn({ method: 'GET' })
.validator((d: unknown) => z.object({ postId: z.string() }).parse(d))
.handler(async ({ data }) => {
// ๐ก ์ด ํธ๋ค๋ฌ๋ 100% ์๋ฒ(Node/Bun/Cloudflare) ํ๊ฒฝ์์๋ง ์์ ํ๊ฒ ์คํ๋ฉ๋๋ค.
// ์ฌ๊ธฐ์ ์ง์ Prisma, Drizzle ORM, ๋๋ ์ธ๋ถ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฟผ๋ฆฌ๋ฅผ ์คํํฉ๋๋ค.
console.log(`[Server] DB ์ฟผ๋ฆฌ ์คํ: Post ID = ${data.postId}`);
const mockDb: Record<string, { id: string; title: string; content: string; author: string }> = {
'1': { id: '1', title: 'TanStack Router์ ์๋ํจ', content: '100% End-to-End Type Safety๋ฅผ ๊ฒฝํํด๋ณด์ธ์.', author: 'ํ๊ธธ๋' },
'2': { id: '2', title: 'createServerFn์ ์๋ฆฌ', content: 'ํด๋ผ์ด์ธํธ-์๋ฒ ๊ฐ ๊ฒฝ๊ณ๊ฐ ์๋ฒฝํ ์ถ์ํ๋ฉ๋๋ค.', author: '์ด์์ ' },
};
const post = mockDb[data.postId];
if (!post) {
// 404 ์๋ฌ ๋ฐ์ ์ notFound() ํธ์ถ
throw notFound();
}
return post;
});
// ============================================================================
// 2. ๋ผ์ฐํธ ์ ์ ๋ฐ loader ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ
// ============================================================================
export const Route = createFileRoute('/posts/$postId')({
// ํ์ด์ง ์ง์
์ ์๋ฒ ์ฌ์ด๋์์ loader๊ฐ ๋จผ์ ์คํ๋์ด ๋ฐ์ดํฐ๋ฅผ ํ๋ฆฌํ์นญํจ
loader: async ({ params }) => {
const post = await fetchPostById({ data: { postId: params.postId } });
return { post };
},
// 404 ๋ฐ์ ์ ๋ณด์ฌ์ค ์ปดํฌ๋ํธ
notFoundComponent: () => {
return <div style={{ padding: '2rem', color: '#ef4444' }}>โ ๏ธ ์์ฒญํ์ ๊ฒ์๊ธ์ ์ฐพ์ ์ ์์ต๋๋ค. (404)</div>;
},
component: PostDetailComponent,
});
// ============================================================================
// 3. UI ์ปดํฌ๋ํธ ๋ ๋๋ง
// ============================================================================
function PostDetailComponent() {
// ๋ณ๋์ ์ ๋ค๋ฆญ ํ์
์ง์ ์์ด๋ post ๊ฐ์ฒด์ ํ๋๊ฐ ์ ํํ๊ฒ ์๋ ์์ฑ๋จ!
const { post } = Route.useLoaderData();
return (
<article style={{ border: '1px solid #cbd5e1', borderRadius: '8px', padding: '1.5rem' }}>
<span style={{ fontSize: '0.8rem', color: '#64748b' }}>Post ID: #{post.id} ยท ์์ฑ์: {post.author}</span>
<h1 style={{ marginTop: '0.5rem', color: '#0f172a' }}>{post.title}</h1>
<p style={{ lineHeight: 1.7, color: '#334155' }}>{post.content}</p>
</article>
);
}
6. [3๋จ๊ณ] ์ค๋ฌด ์์ฉ ํจํด 1: TanStack Query (@tanstack/react-query) ๊ฒฐํฉ
TanStack Router์ loader์ @tanstack/react-query์ ๊ฐ๋ ฅํ ์ธ๋ฉ๋ชจ๋ฆฌ ์บ์ฑ(queryClient.ensureQueryData)์ ๊ฒฐํฉํ๋ฉด ์ํฐํด ์๋ ์๋ฒฝํ SSR + ํด๋ผ์ด์ธํธ ์บ์ฑ์ด ์์ฑ๋ฉ๋๋ค.
// app/routes/users.tsx
import { createFileRoute } from '@tanstack/react-router';
import { queryOptions, useSuspenseQuery } from '@tanstack/react-query';
import { createServerFn } from '@tanstack/start';
// 1. ์๋ฒ ํจ์ ์ ์
const fetchUsersServer = createServerFn({ method: 'GET' }).handler(async () => {
return [
{ id: 1, name: '๊น์ฒ ์', email: 'chulsoo@example.com' },
{ id: 2, name: '์ด์ํฌ', email: 'younghee@example.com' },
];
});
// 2. React Query ์ต์
์ ์ (SSR ๋ฐ ํด๋ผ์ด์ธํธ ๊ณต์ )
export const usersQueryOptions = () =>
queryOptions({
queryKey: ['users'],
queryFn: () => fetchUsersServer(),
staleTime: 1000 * 60 * 5, // 5๋ถ ๋์ ์บ์ ์ ์ง
});
export const Route = createFileRoute('/users')({
// ๋ผ์ฐํธ ๋ก๋์์ QueryClient์ ๋ฐ์ดํฐ๋ฅผ ํ๋ฆฌ๋ก๋!
loader: ({ context }) => {
// router ์์ฑ ์ ์ฃผ์
๋ queryClient ์ปจํ
์คํธ ํ์ฉ
return context.queryClient.ensureQueryData(usersQueryOptions());
},
component: UsersComponent,
});
function UsersComponent() {
// useSuspenseQuery๋ฅผ ์ฌ์ฉํ์ฌ ๋ก๋ฉ ์ํ ์์ด ์ฆ์ ๋ฐ์ดํฐ ์ฌ์ฉ
const { data: users } = useSuspenseQuery(usersQueryOptions());
return (
<div>
<h2>๐ฅ ์ฌ์ฉ์ ๋ช
๋จ (TanStack Query ์ฐ๋)</h2>
<ul>
{users.map((u) => (
<li key={u.id} style={{ marginBottom: '0.5rem' }}>
<strong>{u.name}</strong> ({u.email})
</li>
))}
</ul>
</div>
);
}
7. [3๋จ๊ณ] ์ค๋ฌด ์์ฉ ํจํด 2: beforeLoad ๊ธฐ๋ฐ ์ธ์ฆ ๊ฐ๋(Auth Guard)
๋ก๊ทธ์ธ์ด ํ์ํ ๋น๊ณต๊ฐ ๋ผ์ฐํธ์ ์ง์
ํ ๋, ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง๋๊ธฐ๋ ์ ์ beforeLoad์์ ์ธ์
์ ๊ฒ์ฌํ์ฌ ๋ก๊ทธ์ธ ํ์ด์ง๋ก ์ฆ์ ๋ฆฌ๋ค์ด๋ ํธํฉ๋๋ค.
// app/routes/_authenticated.tsx (๊ณตํต ๋ ์ด์์ ๋ผ์ฐํธ)
import { createFileRoute, Outlet, redirect } from '@tanstack/react-router';
import { createServerFn } from '@tanstack/start';
// ํ์ฌ ๋ก๊ทธ์ธ๋ ์ฌ์ฉ์ ์ธ์
์ ํ์ธํ๋ ์๋ฒ ํจ์
const getAuthSession = createServerFn({ method: 'GET' }).handler(async () => {
// ์ค์ ๊ตฌํ์์๋ ์ฟ ํค์ JWT ๋๋ ์ธ์
ํ ํฐ์ ๊ฒ์ฆํฉ๋๋ค.
const isAuthenticated = false; // (ํ
์คํธ์ฉ: ๋ฏธ์ธ์ฆ ์ํ ๊ฐ์ )
return isAuthenticated ? { user: { id: 'u1', name: '๊ด๋ฆฌ์' } } : null;
});
export const Route = createFileRoute('/_authenticated')({
beforeLoad: async ({ location }) => {
const session = await getAuthSession();
// ๋ฏธ์ธ์ฆ ์ฌ์ฉ์์ธ ๊ฒฝ์ฐ ๋ก๊ทธ์ธ ํ์ด์ง๋ก ๋ฆฌ๋ค์ด๋ ํธ (๋ณต๊ทํ URL์ search ํ๋ผ๋ฏธํฐ๋ก ๋ณด์กด)
if (!session) {
throw redirect({
to: '/login',
search: {
redirectUrl: location.href,
},
});
}
// ์ธ์ฆ๋ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ํ์ ๋ผ์ฐํธ์ Context๋ก ์ฃผ์
return {
auth: session.user,
};
},
component: () => <Outlet />,
});
8. [3๋จ๊ณ] ์ค๋ฌด ์์ฉ ํจํด 3: createServerFn ๋ฐ์ดํฐ ๋ณ๊ฒฝ(Mutation)๊ณผ router.invalidate()
// app/routes/todos.tsx
import { createFileRoute, useRouter } from '@tanstack/react-router';
import { createServerFn } from '@tanstack/start';
import { useState } from 'react';
import { z } from 'zod';
// ์ธ๋ฉ๋ชจ๋ฆฌ ํ
์คํธ์ฉ DB
interface Todo { id: number; title: string; }
const globalTodos: Todo[] = [{ id: 1, title: 'TanStack Start ์นํธ์ํธ ์ ๋
ํ๊ธฐ' }];
// 1. ์กฐํ ์๋ฒ ํจ์
const getTodos = createServerFn({ method: 'GET' }).handler(async () => {
return globalTodos;
});
// 2. ์ถ๊ฐ ์๋ฒ ํจ์ (POST)
const addTodoMutation = createServerFn({ method: 'POST' })
.validator((d: unknown) => z.object({ title: z.string().min(2) }).parse(d))
.handler(async ({ data }) => {
const newTodo = { id: Date.now(), title: data.title };
globalTodos.push(newTodo);
return { success: true, todo: newTodo };
});
export const Route = createFileRoute('/todos')({
loader: async () => {
const todos = await getTodos();
return { todos };
},
component: TodosComponent,
});
function TodosComponent() {
const { todos } = Route.useLoaderData();
const router = useRouter();
const [title, setTitle] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!title.trim()) return;
try {
setIsSubmitting(true);
// ์๋ฒ ํจ์ ์ง์ ์คํ
await addTodoMutation({ data: { title } });
setTitle('');
// โจ ํต์ฌ: router.invalidate()๋ฅผ ํธ์ถํ๋ฉด ํ์ฌ ํ์ฑํ๋ ๋ผ์ฐํธ์ loader๋ง ์๋์ผ๋ก ์ฌํธ์ถ๋์ด ํ๋ฉด์ด ์ฆ์ ์ต์ ํ๋จ!
await router.invalidate();
} catch (err) {
alert('ํ ์ผ ์ถ๊ฐ ์คํจ: ' + err);
} finally {
setIsSubmitting(false);
}
};
return (
<div style={{ maxWidth: '500px' }}>
<h2>๐ ํ ์ผ ๋ชฉ๋ก (Mutation & Invalidation)</h2>
<form onSubmit={handleSubmit} style={{ display: 'flex', gap: '0.5rem', marginBottom: '1.5rem' }}>
<input
type="text"
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="์๋ก์ด ํ ์ผ ์
๋ ฅ..."
disabled={isSubmitting}
style={{ flex: 1, padding: '0.5rem' }}
/>
<button type="submit" disabled={isSubmitting} style={{ padding: '0.5rem 1rem', background: '#10b981', color: '#fff', border: 'none', borderRadius: '4px' }}>
{isSubmitting ? '์ถ๊ฐ ์ค...' : '๋ฑ๋ก'}
</button>
</form>
<ul>
{todos.map((todo) => (
<li key={todo.id} style={{ padding: '0.4rem 0' }}>{todo.title}</li>
))}
</ul>
</div>
);
}
9. ์ค๋ฌด ์ฃผ์์ฌํญ & ํ๋ก๋์ ์ฒดํฌ๋ฆฌ์คํธ (Troubleshooting & Checklist)
โ ๏ธ ์ด๋ณด์๊ฐ ํํ ๊ฒช๋ ์ค์ Top 5
routeTree.gen.ts๋ฅผ ์๋์ผ๋ก ์์ ํ๋ ์ค์:routeTree.gen.ts๋ Vite ํ๋ฌ๊ทธ์ธ์ด ํ์ผ ๋ณ๊ฒฝ์ ๊ฐ์งํ์ฌ 100% ์๋ ์์ฑํ๋ ๋น๋ ์ฐ์ถ๋ฌผ์ ๋๋ค. ์๋์ผ๋ก ํธ์งํ๋ฉด ๋ค์ ์ ์ฅ ์ ๋ฎ์ด์ฐ์ฌ ์ฌ๋ผ์ง๋๋ค. ๋ผ์ฐํธ ๋ณ๊ฒฝ์ ํญ์app/routes/ํด๋ ์์ ํ์ผ ๊ตฌ์กฐ๋ก ์ ์ดํ์ธ์.
beforeLoad์loader์ ์ญํ ํผ๋:beforeLoad๋ ๊ถํ ์ฒดํฌ(์ธ์ฆ ๊ฐ๋), ๋ฆฌ๋ค์ด๋ ํธ, ์ปจํ ์คํธ ์ฃผ์ ์ ๋ด๋นํ๋ฉฐ ๋ถ๋ชจ ๋ผ์ฐํธ๋ถํฐ ์์ฐจ ์คํ๋ฉ๋๋ค.loader๋ ํ์ด์ง ๋ ๋๋ง์ ํ์ํ ๋น์ฆ๋์ค ๋ฐ์ดํฐ ํ์นญ์ ๋ด๋นํ๋ฉฐ, ๋ชจ๋ ์ค์ฒฉ ๋ผ์ฐํธ์ loader๊ฐ **๋ณ๋ ฌ(Parallel)**๋ก ์คํ๋ฉ๋๋ค.
createServerFn๋ด๋ถ์์ ๋ธ๋ผ์ฐ์ ์ ์ฉ ๊ฐ์ฒด ์ฐธ์กฐ:createServerFn์.handler()์ฝ๋๋ ์๋ฒ ๋ฐํ์์์ ์คํ๋ฉ๋๋ค. ๋ฐ๋ผ์ ๋ด๋ถ์์window,document,localStorage๋ฑ์ ์ ๊ทผํ๋ฉด ์๋ฒ ๋ฐํ์ ์๋ฌ(ReferenceError)๊ฐ ๋ฐ์ํ๋ฏ๋ก ์ฟ ํค๋ ์์ฒญ ํค๋๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
- Zod ๊ฒ์ ํ๋ผ๋ฏธํฐ(Search Params) ํ์ฑ ์
catch๋๋ฝ:validateSearch์์z.number()๋ง ์ ์ธํ๊ณ.catch(๊ธฐ๋ณธ๊ฐ)์ ์๋ตํ๋ฉด, ์ฌ์ฉ์๊ฐ ์ฃผ์์ฐฝ์?page=abc๋ฅผ ์ง์ ์ ๋ ฅํ์ ๋ ๋ผ์ฐํธ๊ฐ ํฌ๋์๋ฉ๋๋ค. ํญ์.catch(1)๋๋.default(1)๋ก ๋ฐฉ์ด์ ํด๋ฐฑ์ ์ง์ ํ์ธ์.
- ์๋ฒ ์๋ต ์ ๋ถํ์ํ ๋ฒ๋ค ์ค์ผ ๋ฐฉ์ง:
createServerFn์์ Node ์ ์ฉ ๋ชจ๋(fs,path, DB ์ปค๋ฅํฐ)์ ๋ค๋ฃฐ ๋๋ ํด๋น ๋ก์ง์ด ํด๋ผ์ด์ธํธ ๋ฒ๋ค์ ์์ด์ง ์๋๋กserver-onlyํจํค์ง ๋๋ ์๋ฒ ์ ์ฉ ํ์ผ ๋ถ๋ฆฌ๋ฅผ ์ค์ํด์ผ ํฉ๋๋ค.
๐ ํ๋ก๋์ ๋ฐฐํฌ ์ฒดํฌ๋ฆฌ์คํธ (Nitro Deployment Presets)
TanStack Start๋ ๋ด๋ถ์ ์ผ๋ก Nitro ๋ฐฐํฌ ์์ง์ ์ฑํํ๊ณ ์์ด, ํ๊ฒฝ๋ณ์ ํ ์ค ์ค์ ์ผ๋ก ์ ์ธ๊ณ ๋ชจ๋ ํด๋ผ์ฐ๋์ ์ฆ์ ๋ฐฐํฌํ ์ ์์ต๋๋ค.
# 1. Node.js ํ์ค ์๋ฒ ์ปจํ
์ด๋ ๋ฐฐํฌ (Docker / AWS ECS / Bare-Metal)
NITRO_PRESET=node-server npm run build
node .output/server/index.mjs
# 2. Vercel ์๋ฒ๋ฆฌ์ค ๋ฐฐํฌ
NITRO_PRESET=vercel npm run build
# 3. Cloudflare Pages / Workers ์ฃ์ง ๋ฐฐํฌ
NITRO_PRESET=cloudflare-pages npm run build
ํ๋ก๋์ ๋ฐฐํฌ ์ ์ ๊ฒ ํญ๋ชฉ:
-
npm run build์คํ ์ TypeScript ์ปดํ์ผ ์ค๋ฅ 0๊ฑด ํ์ธ - ๋ชจ๋ ๋์ ๋ผ์ฐํธ์
notFoundComponent๋ฐerrorComponent์๋ฌ ๊ฒฝ๊ณ ๋ฑ๋ก ์ฌ๋ถ - ํ๋ก๋์
๋น๋ ์
TanStackRouterDevtools๊ฐ ํธ๋ฆฌ์์ดํน(Tree-shaking)๋์ด ๋ฒ๋ค์์ ์ ์ธ๋์๋์ง ํ์ธ -
createServerFnํธ์ถ ์ ๋ฐ์ํ ์ ์๋ DB ์์ธ์ ๋ํ ๊ธ๋ก๋ฒ ํธ๋ค๋ฌ ๊ตฌ์ฑ ์๋ฃ - ์ฃผ์ ํ์ด์ง ์ด๋ ๋งํฌ์
preload="intent"๋ฅผ ์ ์ฉํ์ฌ ์ฌ์ฉ์ ์ฒด๊ฐ ์ ํ ์๋ 0ms ๋ฌ์ฑ ์ฌ๋ถ
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์