tanstack-start-routes

$npx mdskill add netlify/swar-templates/tanstack-start-routes

Pages live in src/routes and export Route from createFileRoute. Keep the existing root shell, metadata, providers, and styles. Run dev/build to generate src/routeTree.gen.ts after adding or moving routes; don't edit that generated file.

SKILL.md

.github/skills/tanstack-start-routesView on GitHub ↗
---
name: tanstack-start-routes
description: Create and manage routes in TanStack Start using file-based routing. Use when adding new pages, configuring layouts, setting up nested routes, or working with route parameters.
license: Apache-2.0
metadata:
  author: tanstack
  version: "1.0"
---

# File routes and layouts

Pages live in src/routes and export Route from createFileRoute. Keep the existing root shell, metadata, providers, and styles. Run dev/build to generate src/routeTree.gen.ts after adding or moving routes; don't edit that generated file.

## Root document

This complete shell keeps document markup outside route errors and renders matched content through children. A route component used as a layout must render Outlet for its child routes.

```tsx file=src/routes/__root.tsx
import { createRootRoute, HeadContent, Link, Scripts } from '@tanstack/react-router'
import type { ReactNode } from 'react'
import '../styles.css'

export const Route = createRootRoute({
  head: () => ({ meta: [
    { charSet: 'utf-8' },
    { name: 'viewport', content: 'width=device-width, initial-scale=1' },
    { title: 'Example site' },
    { name: 'description', content: 'A small example site.' },
  ] }),
  shellComponent: RootDocument,
  notFoundComponent: () => <main><h1>Page not found</h1><Link to="/">Go home</Link></main>,
})

function RootDocument({ children }: { children: ReactNode }) {
  return <html lang="en"><head><HeadContent /></head><body>{children}<Scripts /></body></html>
}
```

Register a safe defaultErrorComponent in the router as shown in the project-setup skill. A root route's errorComponent alone does not supply the fallback used by child SSR loader failures.

## Page and pathless layout

Each file block below is a complete module. File paths are relative to a generated app; create them before linking to their routes.

```tsx file=src/routes/about.tsx
import { createFileRoute, Link } from '@tanstack/react-router'

export const Route = createFileRoute('/about')({
  head: () => ({ meta: [{ title: 'About | Example site' }, { name: 'description', content: 'About this example.' }] }),
  component: () => <main><h1>About</h1><Link to="/">Home</Link></main>,
})
```

```tsx file=src/routes/_workspace.tsx
import { createFileRoute, Outlet } from '@tanstack/react-router'

export const Route = createFileRoute('/_workspace')({
  component: () => <section aria-label="Workspace"><Outlet /></section>,
})
```

```tsx file=src/routes/_workspace.notes.tsx
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/_workspace/notes')({
  component: () => <main><h1>Notes</h1></main>,
})
```

## Names and URLs

| File | URL / behavior |
| --- | --- |
| index.tsx | / |
| about.tsx | /about |
| items.$itemId.tsx | /items/:itemId |
| posts.tsx | /posts layout, needs Outlet |
| posts.index.tsx | /posts index; file route ID /posts/ |
| posts.$postId.tsx | /posts/:postId inside the posts layout |
| _workspace.tsx | pathless layout; not an auth boundary |
| _workspace.notes.tsx | /notes inside that layout |
| files.$.tsx | /files/*; read params._splat |
| api.hello.ts | /api/hello using server.handlers |

Flat and directory routes can be mixed. A directory's route.tsx supplies its layout. Route groups in parentheses organize files without adding URL segments. A trailing underscore opts a route out of normal parent nesting. Consult generated route IDs rather than inventing paths.

Use params.parse/params.stringify only when the installed router supports them and runtime-check converted values. For ordinary string parameters, Route.useParams() and explicit loader validation are enough. Use throw notFound() for a missing record, not a generic Error. Keep generic unexpected failures on the error path. Add route-specific head() titles/descriptions when adapting content pages.

More from netlify/swar-templates

SkillDescription
content-collectionsUse content-collections for type-safe content management with markdown files. Use when building blogs, documentation sites, or any content-driven pages with frontmatter and markdown.
netlify-forms-tanstackHandle Netlify Forms in TanStack Start. Use when implementing contact forms, signup forms, or any form submission handling on Netlify-hosted TanStack Start sites.
netlify-identity-tanstack-startUpgrade a stock TanStack Start project to use Netlify Identity for authentication via the @netlify/identity package. Use this skill whenever the user wants to add Netlify Identity auth to a TanStack Start app, integrate login/signup into TanStack Start, protect routes or server functions with Netlify Identity, add role-based access control, or wire up Netlify Identity webhooks. Also use when the user mentions '@netlify/identity', 'nf_jwt', or asks about auth for TanStack Start on Netlify. Covers SSR pages, SPA pages, API routes, server functions, middleware, route guards, role-based access, identity webhooks, and client-side auth state.
tanstack-start-api-routesCreate API routes (server routes) in TanStack Start for handling HTTP requests. Use when building REST APIs, webhooks, or any HTTP endpoint that returns data rather than rendering a page.
tanstack-start-loadersLoad data for TanStack Start routes using beforeLoad and loader functions. Use when fetching data for pages, implementing route guards, or setting up route context. IMPORTANT - Loaders should call server functions for data access.
tanstack-start-project-setupSet up and configure TanStack Start projects. Use when creating new projects, configuring the router, setting up TanStack Query integration, or configuring build settings.
tanstack-start-server-functionsCreate validated TanStack Start server functions and composable middleware for server-only logic called from loaders or components.
tanstack-start-typesafe-routingImplement type-safe navigation and links in TanStack Start. Use when creating links, navigating programmatically, working with search params, or accessing route parameters with full TypeScript support.