tanstack start
via PatrickJS/awesome-cursorrules
Full-stack React framework with type-safe server functions, file-based routing, and streaming via TanStack Router + Vinxi.
What is tanstack start?
TanStack Start combines TanStack Router with Vinxi (Vite + Nitro) to build full-stack React applications with end-to-end type safety. Use it when you need server functions, SSR, streaming with Suspense, and multi-platform deployment without REST endpoints.
- Define server functions with `createServerFn()` for type-safe server-side logic and data fetching
- Use file-based routing and loaders to fetch data before rendering routes
- Stream non-critical data with `defer()` and `Suspense` for faster initial page loads
- Deploy to Node.js, Vercel, Netlify, Bun, or Cloudflare Pages via configurable presets
- Handle mutations with `useMutation()` integrated with server functions
- Create API routes for webhooks and raw HTTP endpoints alongside React routes
Applies to
File patterns this rule matches.
Rule definition (reference)
Source of truth, from the repository.
You are an expert in TanStack Start, TanStack Router, React, TypeScript, and full-stack type-safe web applications.
Core Principles
- TanStack Start = TanStack Router + Vinxi (Vite + Nitro) for full-stack React
createServerFnis the primary way to run server-side logic with end-to-end type safety- All TanStack Router conventions apply — file-based routing, loaders, search params, etc.
- Server functions replace REST endpoints for most use cases
- Streaming + Suspense are first-class — use
defer()for non-critical data
app.config.ts
import { defineConfig } from '@tanstack/start/config'
import tsConfigPaths from 'vite-tsconfig-paths'
export default defineConfig({
vite: { plugins: [tsConfigPaths()] },
server: {
preset: 'node-server', // or: 'vercel', 'netlify', 'bun', 'cloudflare-pages'
},
})
Root Route HTML Shell
// src/routes/__root.tsx
export const Route = createRootRoute({
component: () => (
<html lang="en">
<head />
<body>
<Outlet />
<ScrollRestoration />
<Scripts />
</body>
</html>
),
})
Server Functions
// src/server/functions/posts.ts
export const getPost = createServerFn()
.validator(z.object({ id: z.string() }))
.handler(async ({ data }) => {
const post = await db.post.findUnique({ where: { id: data.id } })
if (!post) throw new Error('Post not found')
return post
})
export const createPost = createServerFn()
.validator(z.object({ title: z.string().min(1), body: z.string() }))
.handler(async ({ data }) => db.post.create({ data }))
Using Server Functions in Routes
export const Route = createFileRoute('/posts/$postId')({
loader: ({ params }) => getPost({ data: { id: params.postId } }),
component: PostDetail,
})
Mutations with Server Functions
const mutation = useMutation({
mutationFn: (input: { title: string; body: string }) => createPost({ data: input }),
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['posts'] }),
})
API Routes (for webhooks / raw HTTP)
// src/routes/api/webhook.ts
export const Route = createAPIFileRoute('/api/webhook')({
POST: async ({ request }) => {
const body = await request.json()
return Response.json({ received: true })
},
})
Streaming with defer()
export const Route = createFileRoute('/posts/$postId')({
loader: async ({ params }) => {
const post = await getPost({ data: { id: params.postId } }) // awaited = critical
const comments = getComments({ data: { postId: params.postId } }) // not awaited
return { post, comments: defer(comments) }
},
component: PostDetail,
})
function PostDetail() {
const { post, comments } = Route.useLoaderData()
return (
<div>
<h1>{post.title}</h1>
<Suspense fallback={<CommentsSkeleton />}>
<Await promise={comments}>{(c) => <CommentsList comments={c} />}</Await>
</Suspense>
</div>
)
}
Environment Variables
- Access server-only vars via
process.envinside server functions only - Use
import.meta.env.VITE_*for client-exposed variables - Never access
process.envin client components
Deployment Targets
Configure server.preset in app.config.ts:
node-server— default Node.jsvercel— Vercel serverless/edgenetlify— Netlify Functionsbun— Bun runtimecloudflare-pages— Cloudflare Pages + Workers
Related rules
Full-stack React framework with server functions, streaming, and end-to-end type safety.
Expert guidance for building desktop apps with Tauri, Svelte, and TypeScript.
Best practices and conventions for building Temporal.io workflows and activities in Python.
TensorFlow and deep learning best practices for building, training, and deploying neural networks
Create standardized TestRail test cases with clear structure, preconditions, and expected results.
Safely discover and verify reusable AI artifacts before building or installing them.
