@kood/claude-code 0.3.7 → 0.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/templates/.claude/agents/code-reviewer.md +124 -124
- package/templates/.claude/agents/dependency-manager.md +85 -85
- package/templates/.claude/agents/deployment-validator.md +56 -56
- package/templates/.claude/agents/git-operator.md +64 -64
- package/templates/.claude/agents/implementation-executor.md +95 -95
- package/templates/.claude/agents/ko-to-en-translator.md +74 -0
- package/templates/.claude/agents/lint-fixer.md +78 -78
- package/templates/.claude/agents/refactor-advisor.md +122 -122
- package/templates/.claude/commands/agent-creator.md +185 -185
- package/templates/.claude/commands/bug-fix.md +193 -193
- package/templates/.claude/commands/command-creator.md +54 -54
- package/templates/.claude/commands/docs-creator.md +57 -57
- package/templates/.claude/commands/docs-refactor.md +26 -26
- package/templates/.claude/commands/execute.md +12 -12
- package/templates/.claude/commands/git-all.md +32 -32
- package/templates/.claude/commands/git-session.md +42 -42
- package/templates/.claude/commands/git.md +34 -34
- package/templates/.claude/commands/lint-fix.md +138 -138
- package/templates/.claude/commands/lint-init.md +61 -61
- package/templates/.claude/commands/plan.md +260 -260
- package/templates/.claude/commands/prd.md +24 -24
- package/templates/.claude/commands/pre-deploy.md +109 -109
- package/templates/.claude/commands/refactor.md +147 -147
- package/templates/.claude/commands/version-update.md +17 -17
- package/templates/hono/CLAUDE.md +27 -27
- package/templates/hono/docs/architecture.md +24 -24
- package/templates/hono/docs/deployment/cloudflare.md +18 -18
- package/templates/hono/docs/deployment/docker.md +13 -13
- package/templates/hono/docs/deployment/index.md +19 -19
- package/templates/hono/docs/deployment/railway.md +32 -32
- package/templates/hono/docs/deployment/vercel.md +29 -29
- package/templates/hono/docs/guides/conventions.md +57 -57
- package/templates/hono/docs/guides/env-setup.md +47 -47
- package/templates/hono/docs/guides/getting-started.md +27 -27
- package/templates/hono/docs/library/hono/error-handling.md +11 -11
- package/templates/hono/docs/library/hono/index.md +4 -4
- package/templates/hono/docs/library/hono/middleware.md +18 -18
- package/templates/hono/docs/library/hono/rpc.md +7 -7
- package/templates/hono/docs/library/hono/validation.md +6 -6
- package/templates/hono/docs/library/prisma/cloudflare-d1.md +29 -29
- package/templates/hono/docs/library/prisma/config.md +16 -16
- package/templates/hono/docs/library/prisma/index.md +32 -32
- package/templates/hono/docs/library/t3-env/index.md +22 -22
- package/templates/hono/docs/library/zod/index.md +31 -31
- package/templates/nextjs/CLAUDE.md +51 -51
- package/templates/nextjs/docs/design.md +183 -183
- package/templates/nextjs/docs/guides/conventions.md +86 -86
- package/templates/nextjs/docs/guides/getting-started.md +28 -28
- package/templates/nextjs/docs/guides/routes.md +32 -32
- package/templates/nextjs/docs/library/better-auth/index.md +70 -70
- package/templates/nextjs/docs/library/nextjs/app-router.md +43 -43
- package/templates/nextjs/docs/library/nextjs/caching.md +73 -73
- package/templates/nextjs/docs/library/nextjs/index.md +51 -51
- package/templates/nextjs/docs/library/nextjs/middleware.md +41 -41
- package/templates/nextjs/docs/library/nextjs/route-handlers.md +31 -31
- package/templates/nextjs/docs/library/nextjs/server-actions.md +34 -34
- package/templates/nextjs/docs/library/prisma/cloudflare-d1.md +20 -20
- package/templates/nextjs/docs/library/prisma/config.md +18 -18
- package/templates/nextjs/docs/library/prisma/crud.md +17 -17
- package/templates/nextjs/docs/library/prisma/index.md +18 -18
- package/templates/nextjs/docs/library/prisma/relations.md +16 -16
- package/templates/nextjs/docs/library/prisma/schema.md +23 -23
- package/templates/nextjs/docs/library/prisma/setup.md +6 -6
- package/templates/nextjs/docs/library/prisma/transactions.md +10 -10
- package/templates/nextjs/docs/library/tanstack-query/index.md +6 -6
- package/templates/nextjs/docs/library/tanstack-query/invalidation.md +20 -20
- package/templates/nextjs/docs/library/tanstack-query/optimistic-updates.md +4 -4
- package/templates/nextjs/docs/library/tanstack-query/use-mutation.md +15 -15
- package/templates/nextjs/docs/library/tanstack-query/use-query.md +22 -22
- package/templates/nextjs/docs/library/zod/complex-types.md +11 -11
- package/templates/nextjs/docs/library/zod/index.md +8 -8
- package/templates/nextjs/docs/library/zod/transforms.md +11 -11
- package/templates/nextjs/docs/library/zod/validation.md +9 -9
- package/templates/npx/CLAUDE.md +37 -37
- package/templates/npx/docs/library/commander/index.md +12 -12
- package/templates/npx/docs/library/fs-extra/index.md +9 -9
- package/templates/npx/docs/library/prompts/index.md +3 -3
- package/templates/npx/docs/references/patterns.md +12 -12
- package/templates/tanstack-start/CLAUDE.md +53 -53
- package/templates/tanstack-start/docs/architecture.md +128 -128
- package/templates/tanstack-start/docs/design.md +169 -169
- package/templates/tanstack-start/docs/guides/conventions.md +43 -43
- package/templates/tanstack-start/docs/guides/env-setup.md +35 -35
- package/templates/tanstack-start/docs/guides/getting-started.md +19 -19
- package/templates/tanstack-start/docs/guides/hooks.md +45 -45
- package/templates/tanstack-start/docs/guides/routes.md +54 -54
- package/templates/tanstack-start/docs/guides/services.md +45 -45
- package/templates/tanstack-start/docs/library/better-auth/index.md +68 -68
- package/templates/tanstack-start/docs/library/prisma/cloudflare-d1.md +19 -19
- package/templates/tanstack-start/docs/library/prisma/config.md +16 -16
- package/templates/tanstack-start/docs/library/prisma/crud.md +17 -17
- package/templates/tanstack-start/docs/library/prisma/index.md +17 -17
- package/templates/tanstack-start/docs/library/prisma/relations.md +16 -16
- package/templates/tanstack-start/docs/library/prisma/schema.md +23 -23
- package/templates/tanstack-start/docs/library/prisma/setup.md +6 -6
- package/templates/tanstack-start/docs/library/prisma/transactions.md +10 -10
- package/templates/tanstack-start/docs/library/t3-env/index.md +21 -160
- package/templates/tanstack-start/docs/library/tanstack-query/index.md +6 -6
- package/templates/tanstack-start/docs/library/tanstack-query/invalidation.md +19 -19
- package/templates/tanstack-start/docs/library/tanstack-query/optimistic-updates.md +4 -4
- package/templates/tanstack-start/docs/library/tanstack-query/use-mutation.md +14 -14
- package/templates/tanstack-start/docs/library/tanstack-query/use-query.md +21 -21
- package/templates/tanstack-start/docs/library/tanstack-router/error-handling.md +9 -9
- package/templates/tanstack-start/docs/library/tanstack-router/hooks.md +11 -11
- package/templates/tanstack-start/docs/library/tanstack-router/index.md +18 -18
- package/templates/tanstack-start/docs/library/tanstack-router/navigation.md +17 -17
- package/templates/tanstack-start/docs/library/tanstack-router/route-context.md +5 -5
- package/templates/tanstack-start/docs/library/tanstack-router/search-params.md +10 -10
- package/templates/tanstack-start/docs/library/tanstack-start/auth-patterns.md +8 -8
- package/templates/tanstack-start/docs/library/tanstack-start/index.md +15 -15
- package/templates/tanstack-start/docs/library/tanstack-start/middleware.md +9 -9
- package/templates/tanstack-start/docs/library/tanstack-start/routing.md +6 -6
- package/templates/tanstack-start/docs/library/tanstack-start/server-functions.md +18 -18
- package/templates/tanstack-start/docs/library/tanstack-start/setup.md +4 -4
- package/templates/tanstack-start/docs/library/zod/complex-types.md +11 -11
- package/templates/tanstack-start/docs/library/zod/index.md +8 -8
- package/templates/tanstack-start/docs/library/zod/transforms.md +11 -11
- package/templates/tanstack-start/docs/library/zod/validation.md +9 -9
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Architecture
|
|
2
2
|
|
|
3
|
-
> TanStack Start
|
|
3
|
+
> TanStack Start Application Architecture
|
|
4
4
|
|
|
5
5
|
<instructions>
|
|
6
6
|
@../guides/conventions.md
|
|
@@ -13,14 +13,14 @@
|
|
|
13
13
|
|
|
14
14
|
<forbidden>
|
|
15
15
|
|
|
16
|
-
|
|
|
17
|
-
|
|
18
|
-
|
|
|
19
|
-
| **Route Export** | `export const IndexRoute`, `const Route` (export
|
|
20
|
-
| **API** | `/api`
|
|
21
|
-
|
|
|
22
|
-
|
|
|
23
|
-
| **Barrel Export** | `functions/index.ts`
|
|
16
|
+
| Category | Forbidden |
|
|
17
|
+
|----------|-----------|
|
|
18
|
+
| **Routes** | Flat file routes (`routes/users.tsx`) |
|
|
19
|
+
| **Route Export** | `export const IndexRoute`, `const Route` (without export) |
|
|
20
|
+
| **API** | Creating `/api` routes (use Server Functions) |
|
|
21
|
+
| **Layers** | Skipping Service Layer, direct DB access from Routes |
|
|
22
|
+
| **Validation** | Manual validation inside handler, scattered auth logic |
|
|
23
|
+
| **Barrel Export** | Creating `functions/index.ts` (Tree Shaking fails, server libraries pollute Client bundle) |
|
|
24
24
|
|
|
25
25
|
</forbidden>
|
|
26
26
|
|
|
@@ -28,20 +28,20 @@
|
|
|
28
28
|
|
|
29
29
|
<required>
|
|
30
30
|
|
|
31
|
-
|
|
|
32
|
-
|
|
33
|
-
|
|
|
34
|
-
| **Route Export** | `export const Route = createFileRoute(...)`
|
|
35
|
-
|
|
|
36
|
-
| **Route Group** |
|
|
37
|
-
|
|
|
38
|
-
| **beforeLoad** |
|
|
39
|
-
| **loader** |
|
|
40
|
-
| **Server Fn** | `createServerFn`
|
|
41
|
-
|
|
|
42
|
-
|
|
|
43
|
-
|
|
|
44
|
-
|
|
|
31
|
+
| Category | Required |
|
|
32
|
+
|----------|----------|
|
|
33
|
+
| **Route Structure** | Create folder per page (`routes/users/index.tsx`) |
|
|
34
|
+
| **Route Export** | `export const Route = createFileRoute(...)` required |
|
|
35
|
+
| **Layer Structure** | Routes → Server Functions → Services → Database |
|
|
36
|
+
| **Route Group** | List pages → `(main)/`, create/edit → outside |
|
|
37
|
+
| **Page Separation** | 100+ lines → `-components`, 200+ lines → `-sections` |
|
|
38
|
+
| **beforeLoad** | Auth check, Context passing, redirects |
|
|
39
|
+
| **loader** | Data loading (runs in parallel after beforeLoad) |
|
|
40
|
+
| **Server Fn** | Use `createServerFn` by default |
|
|
41
|
+
| **Validation** | `inputValidator` (POST/PUT/PATCH), Zod schemas |
|
|
42
|
+
| **Auth** | `middleware` (authMiddleware) |
|
|
43
|
+
| **Error Handling** | `errorComponent` (route), `notFoundComponent` (404) |
|
|
44
|
+
| **Type Safety** | TypeScript strict, Prisma types |
|
|
45
45
|
|
|
46
46
|
</required>
|
|
47
47
|
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
│ TanStack Start Server │
|
|
65
65
|
│ ┌────────────────────────────────────────────────────────────┐ │
|
|
66
66
|
│ │ Server Functions │ │
|
|
67
|
-
│ │ routes/-functions/ →
|
|
67
|
+
│ │ routes/-functions/ → Page-specific | functions/ → Global │ │
|
|
68
68
|
│ └────────────────────────────┬───────────────────────────────┘ │
|
|
69
69
|
│ ┌────────────────────────────▼───────────────────────────────┐ │
|
|
70
70
|
│ │ Services Layer │ │
|
|
@@ -86,32 +86,32 @@
|
|
|
86
86
|
|
|
87
87
|
<route_export_rule>
|
|
88
88
|
|
|
89
|
-
## Route Export
|
|
89
|
+
## Route Export Rules
|
|
90
90
|
|
|
91
|
-
> ⚠️ **`export const Route`
|
|
91
|
+
> ⚠️ **`export const Route` Required**
|
|
92
92
|
>
|
|
93
|
-
> TanStack Router
|
|
93
|
+
> TanStack Router requires all route files to export **exactly the name `Route`**.
|
|
94
94
|
>
|
|
95
|
-
> `tsr generate`
|
|
95
|
+
> The `tsr generate` and `tsr watch` commands automatically generate and update routes.
|
|
96
96
|
|
|
97
|
-
| ❌
|
|
98
|
-
|
|
97
|
+
| ❌ Forbidden | ✅ Required |
|
|
98
|
+
|--------------|-------------|
|
|
99
99
|
| `const Route = createFileRoute(...)` | `export const Route = createFileRoute(...)` |
|
|
100
100
|
| `export const IndexRoute = ...` | `export const Route = ...` |
|
|
101
101
|
| `export default createFileRoute(...)` | `export const Route = createFileRoute(...)` |
|
|
102
102
|
|
|
103
103
|
```typescript
|
|
104
|
-
// ❌
|
|
104
|
+
// ❌ Forbidden: No export
|
|
105
105
|
const Route = createFileRoute('/users')({
|
|
106
106
|
component: UsersPage,
|
|
107
107
|
})
|
|
108
108
|
|
|
109
|
-
// ❌
|
|
109
|
+
// ❌ Forbidden: Different name
|
|
110
110
|
export const UsersRoute = createFileRoute('/users')({
|
|
111
111
|
component: UsersPage,
|
|
112
112
|
})
|
|
113
113
|
|
|
114
|
-
// ✅
|
|
114
|
+
// ✅ Required: Export with exactly 'Route' name
|
|
115
115
|
export const Route = createFileRoute('/users')({
|
|
116
116
|
component: UsersPage,
|
|
117
117
|
})
|
|
@@ -127,59 +127,59 @@ export const Route = createFileRoute('/users')({
|
|
|
127
127
|
|
|
128
128
|
### 1. Routes Layer
|
|
129
129
|
|
|
130
|
-
> ⚠️
|
|
130
|
+
> ⚠️ **Create Folder Per Page (Required)**
|
|
131
131
|
>
|
|
132
|
-
>
|
|
132
|
+
> All pages **must use folder structure**. Flat file approach (`routes/users.tsx`) is forbidden.
|
|
133
133
|
>
|
|
134
|
-
>
|
|
134
|
+
> **Reason:** To systematically manage page-specific resources like -components/, -functions/, -hooks/.
|
|
135
135
|
>
|
|
136
|
-
> | ❌
|
|
137
|
-
>
|
|
136
|
+
> | ❌ Forbidden | ✅ Required |
|
|
137
|
+
> |--------------|-------------|
|
|
138
138
|
> | `routes/users.tsx` | `routes/users/index.tsx` |
|
|
139
139
|
> | `routes/posts.tsx` | `routes/posts/(main)/index.tsx` |
|
|
140
140
|
|
|
141
141
|
```
|
|
142
142
|
routes/<route-name>/
|
|
143
|
-
├── (main)/ #
|
|
144
|
-
│ ├── index.tsx #
|
|
145
|
-
│ ├── -components/ #
|
|
146
|
-
│ ├── -sections/ # UI
|
|
147
|
-
│ ├── -tabs/ #
|
|
148
|
-
│ ├── -hooks/ #
|
|
149
|
-
│ └── -utils/ #
|
|
150
|
-
├── new/ #
|
|
143
|
+
├── (main)/ # Route group (list page)
|
|
144
|
+
│ ├── index.tsx # Page component
|
|
145
|
+
│ ├── -components/ # Page-specific components
|
|
146
|
+
│ ├── -sections/ # UI section separation (200+ lines)
|
|
147
|
+
│ ├── -tabs/ # Tab content separation
|
|
148
|
+
│ ├── -hooks/ # Page-specific hooks
|
|
149
|
+
│ └── -utils/ # Constants, helpers
|
|
150
|
+
├── new/ # Create page (outside route group)
|
|
151
151
|
│ └── index.tsx
|
|
152
|
-
├── route.tsx #
|
|
153
|
-
└── -functions/ #
|
|
152
|
+
├── route.tsx # Route config (loader, beforeLoad)
|
|
153
|
+
└── -functions/ # Page-specific server functions
|
|
154
154
|
```
|
|
155
155
|
|
|
156
|
-
|
|
|
157
|
-
|
|
158
|
-
| **Route Group** | `(main)/` |
|
|
159
|
-
| **-components/** | 100-200
|
|
160
|
-
| **-sections/** | 200
|
|
161
|
-
| **-tabs/** |
|
|
162
|
-
| **route.tsx** |
|
|
156
|
+
| Pattern | Location | Purpose |
|
|
157
|
+
|---------|----------|---------|
|
|
158
|
+
| **Route Group** | `(main)/` | List pages, excluded from URL |
|
|
159
|
+
| **-components/** | 100-200 lines | Separate page-specific components |
|
|
160
|
+
| **-sections/** | 200+ lines | Logical section separation |
|
|
161
|
+
| **-tabs/** | Tab UI | Tab content separation |
|
|
162
|
+
| **route.tsx** | Layout | Shared layout for child routes |
|
|
163
163
|
|
|
164
|
-
#### Layout Routes
|
|
164
|
+
#### Layout Routes Pattern
|
|
165
165
|
|
|
166
|
-
> ⚠️ **route.tsx
|
|
166
|
+
> ⚠️ **Use route.tsx for Layouts**
|
|
167
167
|
>
|
|
168
|
-
> `route.tsx
|
|
169
|
-
> `index.tsx
|
|
168
|
+
> `route.tsx` serves as a shared layout for child routes.
|
|
169
|
+
> `index.tsx` must be wrapped with Route Group `()`.
|
|
170
170
|
>
|
|
171
|
-
>
|
|
171
|
+
> **Required:** `route.tsx` must export a `component`.
|
|
172
172
|
>
|
|
173
|
-
> | ❌
|
|
174
|
-
>
|
|
173
|
+
> | ❌ Forbidden | ✅ Required |
|
|
174
|
+
> |--------------|-------------|
|
|
175
175
|
> | `export const Route = createFileRoute(...)({})` | `export const Route = createFileRoute(...)({ component: ... })` |
|
|
176
176
|
|
|
177
177
|
```
|
|
178
178
|
routes/
|
|
179
179
|
├── (auth)/
|
|
180
|
-
│ ├── route.tsx #
|
|
180
|
+
│ ├── route.tsx # Layout (<Outlet />)
|
|
181
181
|
│ ├── (main)/
|
|
182
|
-
│ │ └── index.tsx # /auth (
|
|
182
|
+
│ │ └── index.tsx # /auth (list/main)
|
|
183
183
|
│ ├── login/
|
|
184
184
|
│ │ └── index.tsx # /auth/login
|
|
185
185
|
│ └── register/
|
|
@@ -187,13 +187,13 @@ routes/
|
|
|
187
187
|
```
|
|
188
188
|
|
|
189
189
|
```typescript
|
|
190
|
-
// ❌
|
|
190
|
+
// ❌ Forbidden: No component
|
|
191
191
|
export const Route = createFileRoute('/(auth)')({
|
|
192
192
|
beforeLoad: async () => ({ user: await getUser() }),
|
|
193
193
|
})
|
|
194
194
|
|
|
195
|
-
// ✅
|
|
196
|
-
// routes/(auth)/route.tsx -
|
|
195
|
+
// ✅ Required: Component must be included
|
|
196
|
+
// routes/(auth)/route.tsx - Layout
|
|
197
197
|
export const Route = createFileRoute('/(auth)')({
|
|
198
198
|
component: () => (
|
|
199
199
|
<div className="auth-container">
|
|
@@ -202,7 +202,7 @@ export const Route = createFileRoute('/(auth)')({
|
|
|
202
202
|
),
|
|
203
203
|
})
|
|
204
204
|
|
|
205
|
-
// routes/(auth)/(main)/index.tsx -
|
|
205
|
+
// routes/(auth)/(main)/index.tsx - Main page
|
|
206
206
|
export const Route = createFileRoute('/(auth)/')({
|
|
207
207
|
component: AuthMainPage,
|
|
208
208
|
})
|
|
@@ -217,38 +217,38 @@ export const Route = createFileRoute('/(auth)/login')({
|
|
|
217
217
|
|
|
218
218
|
```
|
|
219
219
|
services/<domain>/
|
|
220
|
-
├── index.ts #
|
|
221
|
-
├── schemas.ts # Zod
|
|
222
|
-
├── queries.ts # GET
|
|
220
|
+
├── index.ts # Entry point (re-export)
|
|
221
|
+
├── schemas.ts # Zod schemas
|
|
222
|
+
├── queries.ts # GET requests
|
|
223
223
|
└── mutations.ts # POST/PUT/PATCH
|
|
224
224
|
```
|
|
225
225
|
|
|
226
226
|
### 3. Server Functions Layer
|
|
227
227
|
|
|
228
228
|
```
|
|
229
|
-
functions/ #
|
|
230
|
-
├── <function-name>.ts #
|
|
229
|
+
functions/ # Global (reusable)
|
|
230
|
+
├── <function-name>.ts # One per file
|
|
231
231
|
└── middlewares/
|
|
232
232
|
└── <middleware-name>.ts
|
|
233
233
|
|
|
234
|
-
routes/<route>/-functions/ #
|
|
234
|
+
routes/<route>/-functions/ # Page-specific
|
|
235
235
|
└── <function-name>.ts
|
|
236
236
|
```
|
|
237
237
|
|
|
238
|
-
> ⚠️
|
|
238
|
+
> ⚠️ **Do Not Create `functions/index.ts`**
|
|
239
239
|
>
|
|
240
|
-
>
|
|
240
|
+
> Do not create `index.ts` (barrel export) file in `functions/` folder.
|
|
241
241
|
>
|
|
242
|
-
>
|
|
243
|
-
> 1. **Tree Shaking
|
|
244
|
-
> 2. **Client
|
|
242
|
+
> **Issues:**
|
|
243
|
+
> 1. **Tree Shaking Fails** - Bundler includes unused functions
|
|
244
|
+
> 2. **Client Bundle Pollution** - Server-only libraries like `pg`, `prisma` get imported to client, causing build errors
|
|
245
245
|
>
|
|
246
246
|
> ```typescript
|
|
247
|
-
> // ❌ functions/index.ts
|
|
247
|
+
> // ❌ Do not create functions/index.ts
|
|
248
248
|
> export * from './get-users'
|
|
249
|
-
> export * from './create-post' // pg import →
|
|
249
|
+
> export * from './create-post' // pg import → client build fails
|
|
250
250
|
>
|
|
251
|
-
> // ✅
|
|
251
|
+
> // ✅ Import directly from individual files
|
|
252
252
|
> import { getUsers } from '@/functions/get-users'
|
|
253
253
|
> import { createPost } from '@/functions/create-post'
|
|
254
254
|
> ```
|
|
@@ -278,30 +278,30 @@ if (process.env.NODE_ENV !== 'production') {
|
|
|
278
278
|
|
|
279
279
|
### beforeLoad vs loader
|
|
280
280
|
|
|
281
|
-
|
|
|
281
|
+
| Item | beforeLoad | loader |
|
|
282
282
|
|------|-----------|--------|
|
|
283
|
-
|
|
|
284
|
-
|
|
|
285
|
-
|
|
|
286
|
-
|
|
|
283
|
+
| **Execution Order** | Sequential (outermost → innermost) | Parallel (after beforeLoad completes) |
|
|
284
|
+
| **Purpose** | Auth, Context passing, redirects | Data loading |
|
|
285
|
+
| **Blocking** | Blocks all loaders | Runs in parallel with other loaders |
|
|
286
|
+
| **Performance Impact** | ⚠️ High | ✅ Low |
|
|
287
287
|
|
|
288
288
|
```
|
|
289
|
-
1. Parent beforeLoad (
|
|
290
|
-
2. Child beforeLoad (
|
|
291
|
-
3. All loaders (
|
|
289
|
+
1. Parent beforeLoad (sequential) ──┐
|
|
290
|
+
2. Child beforeLoad (sequential) ──┼→ After completion
|
|
291
|
+
3. All loaders (parallel) ───────────┘
|
|
292
292
|
```
|
|
293
293
|
|
|
294
|
-
###
|
|
294
|
+
### Code Patterns
|
|
295
295
|
|
|
296
296
|
```typescript
|
|
297
|
-
// ✅ beforeLoad:
|
|
297
|
+
// ✅ beforeLoad: Auth & Context
|
|
298
298
|
beforeLoad: async () => {
|
|
299
299
|
const user = await getUser()
|
|
300
300
|
if (!user) throw redirect({ to: '/login' })
|
|
301
301
|
return { user }
|
|
302
302
|
}
|
|
303
303
|
|
|
304
|
-
// ✅ loader:
|
|
304
|
+
// ✅ loader: Data Loading
|
|
305
305
|
loader: async () => {
|
|
306
306
|
const [users, roles] = await Promise.all([
|
|
307
307
|
getUsers(),
|
|
@@ -311,10 +311,10 @@ loader: async () => {
|
|
|
311
311
|
}
|
|
312
312
|
```
|
|
313
313
|
|
|
314
|
-
| ❌
|
|
315
|
-
|
|
316
|
-
|
|
|
317
|
-
|
|
|
314
|
+
| ❌ Avoid | ✅ Recommended |
|
|
315
|
+
|----------|----------------|
|
|
316
|
+
| Data loading in beforeLoad | Data loading in loader |
|
|
317
|
+
| Blocking loaders | Parallel execution |
|
|
318
318
|
|
|
319
319
|
</route_lifecycle>
|
|
320
320
|
|
|
@@ -324,14 +324,14 @@ loader: async () => {
|
|
|
324
324
|
|
|
325
325
|
## Context Management
|
|
326
326
|
|
|
327
|
-
|
|
|
328
|
-
|
|
329
|
-
|
|
|
330
|
-
|
|
|
331
|
-
|
|
|
327
|
+
| Step | File | Action |
|
|
328
|
+
|------|------|--------|
|
|
329
|
+
| **Create** | `__root.tsx` | `createRootRouteWithContext` |
|
|
330
|
+
| **Extend** | `route.tsx` | Extend Context in `beforeLoad` |
|
|
331
|
+
| **Use** | `component` | `useRouteContext()` |
|
|
332
332
|
|
|
333
333
|
```typescript
|
|
334
|
-
// 1. Root: Context
|
|
334
|
+
// 1. Root: Define Context
|
|
335
335
|
interface RouterContext {
|
|
336
336
|
user: User | null
|
|
337
337
|
}
|
|
@@ -340,16 +340,16 @@ export const Route = createRootRouteWithContext<RouterContext>()({
|
|
|
340
340
|
beforeLoad: async () => ({ user: await getUser() }),
|
|
341
341
|
})
|
|
342
342
|
|
|
343
|
-
// 2.
|
|
343
|
+
// 2. Extend: Extend Context
|
|
344
344
|
beforeLoad: async ({ context }) => ({
|
|
345
345
|
...context,
|
|
346
346
|
permissions: await getPermissions(context.user.id),
|
|
347
347
|
})
|
|
348
348
|
|
|
349
|
-
// 3.
|
|
349
|
+
// 3. Use: Component
|
|
350
350
|
const { user, permissions } = useRouteContext({ from: '/dashboard' })
|
|
351
351
|
|
|
352
|
-
// 4.
|
|
352
|
+
// 4. Use: Loader
|
|
353
353
|
loader: async ({ context }) => {
|
|
354
354
|
if (!context.permissions.includes('users:read')) {
|
|
355
355
|
throw new Error('Unauthorized')
|
|
@@ -366,7 +366,7 @@ loader: async ({ context }) => {
|
|
|
366
366
|
|
|
367
367
|
## Data Flow
|
|
368
368
|
|
|
369
|
-
### Query Flow (
|
|
369
|
+
### Query Flow (Read)
|
|
370
370
|
|
|
371
371
|
```
|
|
372
372
|
Page → useQuery → Server Function → Prisma → Database
|
|
@@ -387,7 +387,7 @@ export const getUsers = createServerFn()
|
|
|
387
387
|
.handler(async () => prisma.user.findMany())
|
|
388
388
|
```
|
|
389
389
|
|
|
390
|
-
### Mutation Flow (
|
|
390
|
+
### Mutation Flow (Write)
|
|
391
391
|
|
|
392
392
|
```
|
|
393
393
|
Form → useMutation → Server Function
|
|
@@ -423,17 +423,17 @@ export const createUser = createServerFn({ method: 'POST' })
|
|
|
423
423
|
|
|
424
424
|
## Server Functions (Advanced)
|
|
425
425
|
|
|
426
|
-
### Server Functions
|
|
426
|
+
### Server Functions Types
|
|
427
427
|
|
|
428
|
-
|
|
|
429
|
-
|
|
430
|
-
| **createServerFn** |
|
|
431
|
-
| createClientOnlyFn |
|
|
432
|
-
| createIsomorphicFn |
|
|
428
|
+
| Type | Execution | Use Cases |
|
|
429
|
+
|------|-----------|-----------|
|
|
430
|
+
| **createServerFn** | Server | DB access, secrets, server logic (default) |
|
|
431
|
+
| createClientOnlyFn | Client | localStorage, window |
|
|
432
|
+
| createIsomorphicFn | Both | Environment-specific implementations |
|
|
433
433
|
|
|
434
|
-
|
|
434
|
+
**Default Rule**: Use `createServerFn` unless specified otherwise
|
|
435
435
|
|
|
436
|
-
### Middleware
|
|
436
|
+
### Middleware Pattern
|
|
437
437
|
|
|
438
438
|
```typescript
|
|
439
439
|
// 1. authMiddleware
|
|
@@ -444,7 +444,7 @@ export const authMiddleware = createMiddleware()
|
|
|
444
444
|
return next({ context: { ...context, user: session.user } })
|
|
445
445
|
})
|
|
446
446
|
|
|
447
|
-
// 2.
|
|
447
|
+
// 2. Usage
|
|
448
448
|
export const createPost = createServerFn({ method: 'POST' })
|
|
449
449
|
.middleware([authMiddleware])
|
|
450
450
|
.inputValidator(createPostSchema)
|
|
@@ -455,7 +455,7 @@ export const createPost = createServerFn({ method: 'POST' })
|
|
|
455
455
|
})
|
|
456
456
|
```
|
|
457
457
|
|
|
458
|
-
|
|
458
|
+
**Execution Order**: Middleware → inputValidator → handler
|
|
459
459
|
|
|
460
460
|
</server_functions_advanced>
|
|
461
461
|
|
|
@@ -465,11 +465,11 @@ export const createPost = createServerFn({ method: 'POST' })
|
|
|
465
465
|
|
|
466
466
|
## Error Handling
|
|
467
467
|
|
|
468
|
-
|
|
|
469
|
-
|
|
470
|
-
| **errorComponent** |
|
|
468
|
+
| Component | Scope | Location | Required |
|
|
469
|
+
|-----------|-------|----------|----------|
|
|
470
|
+
| **errorComponent** | Route errors | Each route | ✅ |
|
|
471
471
|
| **notFoundComponent** | 404 | __root.tsx | ✅ |
|
|
472
|
-
| **pendingComponent** |
|
|
472
|
+
| **pendingComponent** | Loading | Each route | Optional |
|
|
473
473
|
|
|
474
474
|
```typescript
|
|
475
475
|
// __root.tsx
|
|
@@ -478,29 +478,29 @@ export const Route = createRootRoute({
|
|
|
478
478
|
notFoundComponent: () => <div>404 Not Found</div>,
|
|
479
479
|
})
|
|
480
480
|
|
|
481
|
-
// Route
|
|
481
|
+
// Route level
|
|
482
482
|
export const Route = createFileRoute('/dashboard')({
|
|
483
483
|
errorComponent: ({ error }) => <div>{error.message}</div>,
|
|
484
484
|
})
|
|
485
485
|
|
|
486
|
-
// Loader
|
|
486
|
+
// Loader errors
|
|
487
487
|
loader: async () => {
|
|
488
488
|
try {
|
|
489
489
|
return { users: await getUsers() }
|
|
490
490
|
} catch (error) {
|
|
491
|
-
throw new Error('
|
|
491
|
+
throw new Error('Failed to load data')
|
|
492
492
|
}
|
|
493
493
|
}
|
|
494
494
|
|
|
495
|
-
// Server Function
|
|
495
|
+
// Server Function errors
|
|
496
496
|
.handler(async ({ data }) => {
|
|
497
497
|
try {
|
|
498
498
|
return await prisma.user.create({ data })
|
|
499
499
|
} catch (error) {
|
|
500
500
|
if (error.code === 'P2002') {
|
|
501
|
-
throw new Error('
|
|
501
|
+
throw new Error('Email already exists')
|
|
502
502
|
}
|
|
503
|
-
throw new Error('
|
|
503
|
+
throw new Error('Failed to create user')
|
|
504
504
|
}
|
|
505
505
|
})
|
|
506
506
|
```
|