@@ -15,14 +15,98 @@ You are an expert in TanStack Start routing and modern React project organizatio
1515• Use bracket notation like ` [id].tsx ` for dynamic routes
1616• Use ` _layout.tsx ` files for shared layouts across route groups
1717
18- ## Route File Structure
18+ ## Route File Structure and Patterns
1919
2020• Export ` loader ` functions for data fetching before route renders
2121• Export ` action ` functions for handling form submissions and mutations
2222• Export ` meta ` objects for page metadata (title, description, etc.)
2323• Keep route components focused on layout and data orchestration
2424• Extract complex logic into custom hooks or utility functions
2525
26+ ### Loader and Action Typing
27+
28+ • Always type loader and action returns explicitly for better type safety
29+ • Use Zod schemas for validating loader parameters and action inputs
30+ • Leverage ` useLoaderData() ` with proper typing for accessing loader data
31+
32+ ``` typescript
33+ // ✅ Good: Properly typed loader with validation
34+ export const Route = createFileRoute (' /users/$id' )({
35+ loader : async ({ params }): Promise <User > => {
36+ const userIdSchema = z .object ({ id: z .string () })
37+ const { id } = userIdSchema .parse (params )
38+ return fetchUser (id )
39+ },
40+ component: UserDetail ,
41+ })
42+
43+ function UserDetail() {
44+ const user = Route .useLoaderData () // Automatically typed as User
45+ return <div >{user.name }< / div >
46+ }
47+ ```
48+
49+ ### Loader-First Data Flow
50+
51+ • Prefer loaders over React Query for initial page data to avoid redundant fetching
52+ • Use React Query only for data that needs frequent updates or client-side caching
53+ • Implement loader-first pattern to ensure data is available before component renders
54+
55+ ``` typescript
56+ // ✅ Good: Loader-first approach
57+ export const Route = createFileRoute (' /dashboard' )({
58+ loader : async (): Promise <DashboardData > => {
59+ // Fetch initial data in loader
60+ return {
61+ user: await fetchUser (),
62+ metrics: await fetchMetrics (),
63+ }
64+ },
65+ component: Dashboard ,
66+ })
67+
68+ // ❌ Avoid: Redundant React Query when loader data is sufficient
69+ function Dashboard() {
70+ const data = Route .useLoaderData ()
71+ // Don't use React Query here if loader data is sufficient
72+ return <DashboardView data = {data} />
73+ }
74+ ```
75+
76+ ### Error and Loading Boundaries
77+
78+ • Use ErrorBoundary for handling route-level errors gracefully
79+ • Implement PendingBoundary for loading states during navigation
80+ • Provide meaningful error messages and recovery options
81+
82+ ```typescript
83+ // ✅ Good: Route with error and pending boundaries
84+ export const Route = createFileRoute(' /users/$id' )({
85+ loader : async ({ params }) => {
86+ try {
87+ return await fetchUser (params .id )
88+ } catch (error ) {
89+ throw new Error (` Failed to load user: ${error .message } ` )
90+ }
91+ },
92+ errorComponent : ({ error }) => (
93+ < div className = " text-center p-6" >
94+ < h2 className = " text-xl font-semibold text-red-600 mb-2" > Error < / h2 >
95+ < p className = " text-gray-600" > {error.message }< / p >
96+ < Link to = " /users" className = " text-blue-600 hover:underline mt-4 inline-block" >
97+ Back to Users
98+ < / Link >
99+ < / div >
100+ ),
101+ pendingComponent : () => (
102+ < div className = " flex items-center justify-center p-6" >
103+ < div className = " animate-spin rounded-full h-8 w-8 border-b-2 border-primary" > </div >
104+ < / div >
105+ ),
106+ component: UserDetail ,
107+ })
108+ ` ` `
109+
26110## Component Organization
27111
28112• Keep shared UI components in ` src / components `
@@ -45,7 +129,27 @@ You are an expert in TanStack Start routing and modern React project organizatio
45129• Prefer named exports for utilities and hooks
46130• Use default exports for React components
47131• Implement proper module boundaries between features
48- • Use path mapping for clean import statements when necessary
132+ • Use path mapping ( ` @/ ` ) for clean import statements - configured in tsconfig.json
133+
134+ ### Import Aliasing Best Practices
135+
136+ • Use ` @/ components ` for importing UI components
137+ • Use ` @/ lib ` for utilities, schemas, and helper functions
138+ • Use ` @/ hooks ` for custom React hooks
139+ • Use ` @/ contexts ` for React Context providers
140+ • Always prefer aliased imports over relative paths for better maintainability
141+
142+ ` ` ` typescript
143+ // ✅ Good: Clean aliased imports
144+ import { Button } from ' @/components/ui/button'
145+ import { userSchema } from ' @/lib/schemas'
146+ import { useLocalStorage } from ' @/hooks/useLocalStorage'
147+ import { UserProvider } from ' @/contexts/UserContext'
148+
149+ // ❌ Avoid: Relative path imports for shared code
150+ import { Button } from ' ../../../components/ui/button'
151+ import { userSchema } from ' ../../lib/schemas'
152+ ` ` `
49153
50154## File Naming Conventions
51155
0 commit comments