@tanstack/router-core 1.167.3 → 1.167.5

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/bin/intent.js ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env node
2
+ // Auto-generated by @tanstack/intent setup
3
+ // Exposes the intent end-user CLI for consumers of this library.
4
+ // Commit this file, then add to your package.json:
5
+ // "bin": { "intent": "./bin/intent.js" }
6
+ try {
7
+ await import('@tanstack/intent/intent-library')
8
+ } catch (e) {
9
+ const isModuleNotFound =
10
+ e?.code === 'ERR_MODULE_NOT_FOUND' || e?.code === 'MODULE_NOT_FOUND'
11
+ const missingIntentLibrary =
12
+ typeof e?.message === 'string' && e.message.includes('@tanstack/intent')
13
+
14
+ if (isModuleNotFound && missingIntentLibrary) {
15
+ console.error('@tanstack/intent is not installed.')
16
+ console.error('')
17
+ console.error('Install it as a dev dependency:')
18
+ console.error(' npm add -D @tanstack/intent')
19
+ console.error('')
20
+ console.error('Or run directly:')
21
+ console.error(' npx @tanstack/intent@latest list')
22
+ process.exit(1)
23
+ }
24
+ throw e
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/router-core",
3
- "version": "1.167.3",
3
+ "version": "1.167.5",
4
4
  "description": "Modern and scalable routing for React applications",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -138,7 +138,10 @@
138
138
  "sideEffects": false,
139
139
  "files": [
140
140
  "dist",
141
- "src"
141
+ "src",
142
+ "skills",
143
+ "bin",
144
+ "!skills/_artifacts"
142
145
  ],
143
146
  "engines": {
144
147
  "node": ">=20.19"
@@ -153,9 +156,13 @@
153
156
  "@tanstack/history": "1.161.6"
154
157
  },
155
158
  "devDependencies": {
156
- "esbuild": "^0.25.0",
159
+ "@tanstack/intent": "^0.0.14",
160
+ "esbuild": "^0.27.4",
157
161
  "vite": "*"
158
162
  },
163
+ "bin": {
164
+ "intent": "./bin/intent.js"
165
+ },
159
166
  "scripts": {
160
167
  "clean": "rimraf ./dist && rimraf ./coverage",
161
168
  "test:eslint": "eslint ./src",
@@ -0,0 +1,139 @@
1
+ ---
2
+ name: router-core
3
+ description: >-
4
+ Framework-agnostic core concepts for TanStack Router: route trees,
5
+ createRouter, createRoute, createRootRoute, createRootRouteWithContext,
6
+ addChildren, Register type declaration, route matching, route sorting,
7
+ file naming conventions. Entry point for all router skills.
8
+ type: core
9
+ library: tanstack-router
10
+ library_version: '1.166.2'
11
+ ---
12
+
13
+ # TanStack Router Core
14
+
15
+ TanStack Router is a type-safe router for React and Solid with built-in SWR caching, JSON-first search params, file-based route generation, and end-to-end type inference. The core is framework-agnostic; React and Solid bindings layer on top.
16
+
17
+ > **CRITICAL**: TanStack Router types are FULLY INFERRED. Never cast, never annotate inferred values. This is the #1 AI agent mistake.
18
+
19
+ > **CRITICAL**: TanStack Router is CLIENT-FIRST. Loaders run on the client by default, NOT server-only like Remix/Next.js. Do not confuse TanStack Router APIs with Next.js or React Router.
20
+
21
+ ## Sub-Skills
22
+
23
+ | Task | Sub-Skill |
24
+ | -------------------------------------------------- | ---------------------------------------------------------------------------- |
25
+ | Validate, read, write, transform search params | [router-core/search-params/SKILL.md](./search-params/SKILL.md) |
26
+ | Dynamic segments, splats, optional params | [router-core/path-params/SKILL.md](./path-params/SKILL.md) |
27
+ | Link, useNavigate, preloading, blocking | [router-core/navigation/SKILL.md](./navigation/SKILL.md) |
28
+ | Route loaders, SWR caching, context, deferred data | [router-core/data-loading/SKILL.md](./data-loading/SKILL.md) |
29
+ | Auth guards, RBAC, beforeLoad redirects | [router-core/auth-and-guards/SKILL.md](./auth-and-guards/SKILL.md) |
30
+ | Automatic and manual code splitting | [router-core/code-splitting/SKILL.md](./code-splitting/SKILL.md) |
31
+ | 404 handling, error boundaries, notFound() | [router-core/not-found-and-errors/SKILL.md](./not-found-and-errors/SKILL.md) |
32
+ | Inference, Register, from narrowing, TS perf | [router-core/type-safety/SKILL.md](./type-safety/SKILL.md) |
33
+ | Streaming/non-streaming SSR, hydration, head mgmt | [router-core/ssr/SKILL.md](./ssr/SKILL.md) |
34
+
35
+ ## Quick Decision Tree
36
+
37
+ ```
38
+ Need to add/read/write URL query parameters?
39
+ → router-core/search-params
40
+
41
+ Need dynamic URL segments like /posts/$postId?
42
+ → router-core/path-params
43
+
44
+ Need to create links or navigate programmatically?
45
+ → router-core/navigation
46
+
47
+ Need to fetch data for a route?
48
+ Is it client-side only or client+server?
49
+ → router-core/data-loading
50
+ Using TanStack Query as external cache?
51
+ → compositions/router-query (separate skill)
52
+
53
+ Need to protect routes behind auth?
54
+ → router-core/auth-and-guards
55
+
56
+ Need to reduce bundle size per route?
57
+ → router-core/code-splitting
58
+
59
+ Need custom 404 or error handling?
60
+ → router-core/not-found-and-errors
61
+
62
+ Having TypeScript issues or performance problems?
63
+ → router-core/type-safety
64
+
65
+ Need server-side rendering?
66
+ → router-core/ssr
67
+ ```
68
+
69
+ ## Minimal Working Example
70
+
71
+ ```tsx
72
+ // src/routes/__root.tsx
73
+ import { createRootRoute, Outlet } from '@tanstack/react-router'
74
+
75
+ export const Route = createRootRoute({
76
+ component: () => <Outlet />,
77
+ })
78
+ ```
79
+
80
+ ```tsx
81
+ // src/routes/index.tsx
82
+ import { createFileRoute } from '@tanstack/react-router'
83
+
84
+ export const Route = createFileRoute('/')({
85
+ component: () => <h1>Home</h1>,
86
+ })
87
+ ```
88
+
89
+ ```tsx
90
+ // src/router.tsx
91
+ import { createRouter } from '@tanstack/react-router'
92
+ import { routeTree } from './routeTree.gen'
93
+
94
+ const router = createRouter({ routeTree })
95
+
96
+ // REQUIRED for type safety — without this, Link/useNavigate have no autocomplete
97
+ declare module '@tanstack/react-router' {
98
+ interface Register {
99
+ router: typeof router
100
+ }
101
+ }
102
+
103
+ export default router
104
+ ```
105
+
106
+ ```tsx
107
+ // src/main.tsx
108
+ import { RouterProvider } from '@tanstack/react-router'
109
+ import router from './router'
110
+
111
+ function App() {
112
+ return <RouterProvider router={router} />
113
+ }
114
+ ```
115
+
116
+ ## Common Mistakes
117
+
118
+ ### HIGH: createFileRoute path string must match the file path
119
+
120
+ The Vite plugin manages the path string in `createFileRoute`. Do not change it manually — it must match the file's location under `src/routes/`:
121
+
122
+ ```tsx
123
+ // File: src/routes/posts/$postId.tsx
124
+ export const Route = createFileRoute('/posts/$postId')({
125
+ // ✅ matches file path
126
+ component: PostPage,
127
+ })
128
+
129
+ export const Route = createFileRoute('/post/$postId')({
130
+ // ❌ silent mismatch
131
+ component: PostPage,
132
+ })
133
+ ```
134
+
135
+ The plugin auto-generates this string. If you rename a route file, the plugin updates it. Never edit the path string by hand.
136
+
137
+ ## Version Note
138
+
139
+ This skill targets `@tanstack/router-core` v1.166.2 and `@tanstack/react-router` v1.166.2. APIs are stable. Splat routes use `$` (not `*`); the `*` compat alias will be removed in v2.
@@ -0,0 +1,458 @@
1
+ ---
2
+ name: router-core/auth-and-guards
3
+ description: >-
4
+ Route protection with beforeLoad, redirect()/throw redirect(),
5
+ isRedirect helper, authenticated layout routes (_authenticated),
6
+ non-redirect auth (inline login), RBAC with roles and permissions,
7
+ auth provider integration (Auth0, Clerk, Supabase), router context
8
+ for auth state.
9
+ type: sub-skill
10
+ library: tanstack-router
11
+ library_version: '1.166.2'
12
+ requires:
13
+ - router-core
14
+ - router-core/data-loading
15
+ sources:
16
+ - TanStack/router:docs/router/guide/authenticated-routes.md
17
+ - TanStack/router:docs/router/how-to/setup-authentication.md
18
+ - TanStack/router:docs/router/how-to/setup-auth-providers.md
19
+ - TanStack/router:docs/router/how-to/setup-rbac.md
20
+ ---
21
+
22
+ # Auth and Guards
23
+
24
+ ## Setup
25
+
26
+ Protect routes with `beforeLoad` + `redirect()` in a pathless layout route (`_authenticated`):
27
+
28
+ ```tsx
29
+ // src/routes/_authenticated.tsx
30
+ import { createFileRoute, redirect } from '@tanstack/react-router'
31
+
32
+ export const Route = createFileRoute('/_authenticated')({
33
+ beforeLoad: ({ context, location }) => {
34
+ if (!context.auth.isAuthenticated) {
35
+ throw redirect({
36
+ to: '/login',
37
+ search: {
38
+ redirect: location.href,
39
+ },
40
+ })
41
+ }
42
+ },
43
+ // component defaults to Outlet — no need to declare it
44
+ })
45
+ ```
46
+
47
+ Any route file placed under `src/routes/_authenticated/` is automatically protected:
48
+
49
+ ```tsx
50
+ // src/routes/_authenticated/dashboard.tsx
51
+ import { createFileRoute } from '@tanstack/react-router'
52
+
53
+ export const Route = createFileRoute('/_authenticated/dashboard')({
54
+ component: DashboardComponent,
55
+ })
56
+
57
+ function DashboardComponent() {
58
+ const { auth } = Route.useRouteContext()
59
+ return <div>Welcome, {auth.user?.username}</div>
60
+ }
61
+ ```
62
+
63
+ ## Core Patterns
64
+
65
+ ### Router Context for Auth State
66
+
67
+ Auth state flows into the router via `createRootRouteWithContext` and `RouterProvider`'s `context` prop:
68
+
69
+ ```tsx
70
+ // src/routes/__root.tsx
71
+ import { createRootRouteWithContext, Outlet } from '@tanstack/react-router'
72
+
73
+ interface AuthState {
74
+ isAuthenticated: boolean
75
+ user: { id: string; username: string; email: string } | null
76
+ login: (username: string, password: string) => Promise<void>
77
+ logout: () => void
78
+ }
79
+
80
+ interface MyRouterContext {
81
+ auth: AuthState
82
+ }
83
+
84
+ export const Route = createRootRouteWithContext<MyRouterContext>()({
85
+ component: () => <Outlet />,
86
+ })
87
+ ```
88
+
89
+ ```tsx
90
+ // src/router.tsx
91
+ import { createRouter } from '@tanstack/react-router'
92
+ import { routeTree } from './routeTree.gen'
93
+
94
+ export const router = createRouter({
95
+ routeTree,
96
+ context: {
97
+ auth: undefined!, // placeholder — filled by RouterProvider context prop
98
+ },
99
+ })
100
+
101
+ declare module '@tanstack/react-router' {
102
+ interface Register {
103
+ router: typeof router
104
+ }
105
+ }
106
+ ```
107
+
108
+ ```tsx
109
+ // src/App.tsx
110
+ import { RouterProvider } from '@tanstack/react-router'
111
+ import { AuthProvider, useAuth } from './auth'
112
+ import { router } from './router'
113
+
114
+ function InnerApp() {
115
+ const auth = useAuth()
116
+ // context prop injects live auth state WITHOUT recreating the router
117
+ return <RouterProvider router={router} context={{ auth }} />
118
+ }
119
+
120
+ function App() {
121
+ return (
122
+ <AuthProvider>
123
+ <InnerApp />
124
+ </AuthProvider>
125
+ )
126
+ }
127
+ ```
128
+
129
+ The router is created once with a placeholder. `RouterProvider`'s `context` prop injects the live auth state on each render — this avoids recreating the router on auth changes (which would reset caches and rebuild the route tree).
130
+
131
+ ### Redirect-Based Auth with Redirect-Back
132
+
133
+ Save the current location in search params so you can redirect back after login:
134
+
135
+ ```tsx
136
+ // src/routes/_authenticated.tsx
137
+ import { createFileRoute, redirect } from '@tanstack/react-router'
138
+
139
+ export const Route = createFileRoute('/_authenticated')({
140
+ beforeLoad: ({ context, location }) => {
141
+ if (!context.auth.isAuthenticated) {
142
+ throw redirect({
143
+ to: '/login',
144
+ search: { redirect: location.href },
145
+ })
146
+ }
147
+ },
148
+ })
149
+ ```
150
+
151
+ ```tsx
152
+ // src/routes/login.tsx
153
+ import { createFileRoute, redirect } from '@tanstack/react-router'
154
+ import { useState, type FormEvent } from 'react'
155
+
156
+ // Validate redirect target to prevent open redirect attacks
157
+ function sanitizeRedirect(url: unknown): string {
158
+ if (typeof url !== 'string' || !url.startsWith('/') || url.startsWith('//')) {
159
+ return '/'
160
+ }
161
+ return url
162
+ }
163
+
164
+ export const Route = createFileRoute('/login')({
165
+ validateSearch: (search) => ({
166
+ redirect: sanitizeRedirect(search.redirect),
167
+ }),
168
+ beforeLoad: ({ context, search }) => {
169
+ if (context.auth.isAuthenticated) {
170
+ throw redirect({ to: search.redirect })
171
+ }
172
+ },
173
+ component: LoginComponent,
174
+ })
175
+
176
+ function LoginComponent() {
177
+ const { auth } = Route.useRouteContext()
178
+ const search = Route.useSearch()
179
+ const navigate = Route.useNavigate()
180
+ const [username, setUsername] = useState('')
181
+ const [password, setPassword] = useState('')
182
+ const [error, setError] = useState('')
183
+
184
+ const handleSubmit = async (e: FormEvent) => {
185
+ e.preventDefault()
186
+ try {
187
+ await auth.login(username, password)
188
+ navigate({ to: search.redirect })
189
+ } catch {
190
+ setError('Invalid credentials')
191
+ }
192
+ }
193
+
194
+ return (
195
+ <form onSubmit={handleSubmit}>
196
+ {error && <div>{error}</div>}
197
+ <input value={username} onChange={(e) => setUsername(e.target.value)} />
198
+ <input
199
+ type="password"
200
+ value={password}
201
+ onChange={(e) => setPassword(e.target.value)}
202
+ />
203
+ <button type="submit">Sign In</button>
204
+ </form>
205
+ )
206
+ }
207
+ ```
208
+
209
+ ### Non-Redirect Auth (Inline Login)
210
+
211
+ Instead of redirecting, show a login form in place of the `Outlet`:
212
+
213
+ ```tsx
214
+ // src/routes/_authenticated.tsx
215
+ import { createFileRoute, Outlet } from '@tanstack/react-router'
216
+
217
+ export const Route = createFileRoute('/_authenticated')({
218
+ component: AuthenticatedLayout,
219
+ })
220
+
221
+ function AuthenticatedLayout() {
222
+ const { auth } = Route.useRouteContext()
223
+
224
+ if (!auth.isAuthenticated) {
225
+ return <LoginForm />
226
+ }
227
+
228
+ return <Outlet />
229
+ }
230
+ ```
231
+
232
+ This keeps the URL unchanged — the user stays on the same page and sees a login form instead of protected content. After authentication, `<Outlet />` renders and child routes appear.
233
+
234
+ ### RBAC with Roles and Permissions
235
+
236
+ Extend auth state with role/permission helpers, then check in `beforeLoad`:
237
+
238
+ ```tsx
239
+ // src/auth.tsx
240
+ interface User {
241
+ id: string
242
+ username: string
243
+ email: string
244
+ roles: string[]
245
+ permissions: string[]
246
+ }
247
+
248
+ interface AuthState {
249
+ isAuthenticated: boolean
250
+ user: User | null
251
+ hasRole: (role: string) => boolean
252
+ hasAnyRole: (roles: string[]) => boolean
253
+ hasPermission: (permission: string) => boolean
254
+ hasAnyPermission: (permissions: string[]) => boolean
255
+ login: (username: string, password: string) => Promise<void>
256
+ logout: () => void
257
+ }
258
+ ```
259
+
260
+ Admin-only layout route:
261
+
262
+ ```tsx
263
+ // src/routes/_authenticated/_admin.tsx
264
+ import { createFileRoute, redirect } from '@tanstack/react-router'
265
+
266
+ export const Route = createFileRoute('/_authenticated/_admin')({
267
+ beforeLoad: ({ context, location }) => {
268
+ if (!context.auth.hasRole('admin')) {
269
+ throw redirect({
270
+ to: '/unauthorized',
271
+ search: { redirect: location.href },
272
+ })
273
+ }
274
+ },
275
+ })
276
+ ```
277
+
278
+ Multi-role access:
279
+
280
+ ```tsx
281
+ // src/routes/_authenticated/_moderator.tsx
282
+ import { createFileRoute, redirect } from '@tanstack/react-router'
283
+
284
+ export const Route = createFileRoute('/_authenticated/_moderator')({
285
+ beforeLoad: ({ context, location }) => {
286
+ if (!context.auth.hasAnyRole(['admin', 'moderator'])) {
287
+ throw redirect({
288
+ to: '/unauthorized',
289
+ search: { redirect: location.href },
290
+ })
291
+ }
292
+ },
293
+ })
294
+ ```
295
+
296
+ Permission-based:
297
+
298
+ ```tsx
299
+ // src/routes/_authenticated/_users.tsx
300
+ import { createFileRoute, redirect } from '@tanstack/react-router'
301
+
302
+ export const Route = createFileRoute('/_authenticated/_users')({
303
+ beforeLoad: ({ context, location }) => {
304
+ if (!context.auth.hasAnyPermission(['users:read', 'users:write'])) {
305
+ throw redirect({
306
+ to: '/unauthorized',
307
+ search: { redirect: location.href },
308
+ })
309
+ }
310
+ },
311
+ })
312
+ ```
313
+
314
+ Page-level permission check (nested under an already-role-protected layout):
315
+
316
+ ```tsx
317
+ // src/routes/_authenticated/_users/manage.tsx
318
+ import { createFileRoute } from '@tanstack/react-router'
319
+
320
+ export const Route = createFileRoute('/_authenticated/_users/manage')({
321
+ beforeLoad: ({ context }) => {
322
+ if (!context.auth.hasPermission('users:write')) {
323
+ throw new Error('Write permission required')
324
+ }
325
+ },
326
+ component: UserManagement,
327
+ })
328
+
329
+ function UserManagement() {
330
+ const { auth } = Route.useRouteContext()
331
+ const canDelete = auth.hasPermission('users:delete')
332
+
333
+ return (
334
+ <div>
335
+ <h1>User Management</h1>
336
+ {canDelete && <button>Delete User</button>}
337
+ </div>
338
+ )
339
+ }
340
+ ```
341
+
342
+ ### Handling Auth Check Failures (isRedirect)
343
+
344
+ When `beforeLoad` has a try/catch, redirects (which work by throwing) can get swallowed. Use `isRedirect` to re-throw:
345
+
346
+ ```tsx
347
+ import { createFileRoute, redirect, isRedirect } from '@tanstack/react-router'
348
+
349
+ export const Route = createFileRoute('/_authenticated')({
350
+ beforeLoad: async ({ context, location }) => {
351
+ try {
352
+ const user = await verifySession(context.auth)
353
+ if (!user) {
354
+ throw redirect({
355
+ to: '/login',
356
+ search: { redirect: location.href },
357
+ })
358
+ }
359
+ return { user }
360
+ } catch (error) {
361
+ if (isRedirect(error)) throw error // re-throw redirect, don't swallow it
362
+ // Actual error — redirect to login
363
+ throw redirect({
364
+ to: '/login',
365
+ search: { redirect: location.href },
366
+ })
367
+ }
368
+ },
369
+ })
370
+ ```
371
+
372
+ ## Common Mistakes
373
+
374
+ ### HIGH: Auth check in component instead of beforeLoad
375
+
376
+ Component-level auth checks cause a **flash of protected content** before the redirect:
377
+
378
+ ```tsx
379
+ // WRONG — protected content renders briefly before redirect
380
+ export const Route = createFileRoute('/_authenticated/dashboard')({
381
+ component: () => {
382
+ const auth = useAuth()
383
+ if (!auth.isAuthenticated) return <Navigate to="/login" />
384
+ return <Dashboard />
385
+ },
386
+ })
387
+
388
+ // CORRECT — beforeLoad runs before any rendering
389
+ export const Route = createFileRoute('/_authenticated/dashboard')({
390
+ beforeLoad: ({ context }) => {
391
+ if (!context.auth.isAuthenticated) {
392
+ throw redirect({ to: '/login' })
393
+ }
394
+ },
395
+ component: Dashboard,
396
+ })
397
+ ```
398
+
399
+ `beforeLoad` runs before any component rendering and before the loader. It completely prevents the flash.
400
+
401
+ ### HIGH: Not re-throwing redirects in try/catch
402
+
403
+ `redirect()` works by throwing. If `beforeLoad` has a try/catch, the redirect gets swallowed:
404
+
405
+ ```tsx
406
+ // WRONG — redirect is caught and swallowed
407
+ beforeLoad: async ({ context }) => {
408
+ try {
409
+ await validateSession(context.auth)
410
+ } catch (e) {
411
+ console.error(e) // swallows the redirect!
412
+ }
413
+ }
414
+
415
+ // CORRECT — use isRedirect to distinguish intentional redirects from errors
416
+ import { isRedirect } from '@tanstack/react-router'
417
+
418
+ beforeLoad: async ({ context }) => {
419
+ try {
420
+ await validateSession(context.auth)
421
+ } catch (e) {
422
+ if (isRedirect(e)) throw e
423
+ console.error(e)
424
+ }
425
+ }
426
+ ```
427
+
428
+ ### MEDIUM: Conditionally rendering root route component
429
+
430
+ The root route always renders regardless of auth state. You cannot conditionally render its component:
431
+
432
+ ```tsx
433
+ // WRONG — root route always renders, this doesn't protect anything
434
+ export const Route = createRootRoute({
435
+ component: () => {
436
+ if (!isAuthenticated()) return <Login />
437
+ return <Outlet />
438
+ },
439
+ })
440
+
441
+ // CORRECT — use a pathless layout route for auth boundaries
442
+ // src/routes/_authenticated.tsx
443
+ export const Route = createFileRoute('/_authenticated')({
444
+ beforeLoad: ({ context }) => {
445
+ if (!context.auth.isAuthenticated) {
446
+ throw redirect({ to: '/login' })
447
+ }
448
+ },
449
+ })
450
+ ```
451
+
452
+ Place protected routes as children of the `_authenticated` layout route. Public routes (login, home, etc.) live outside it.
453
+
454
+ ---
455
+
456
+ ## Cross-References
457
+
458
+ - See also: **router-core/data-loading/SKILL.md** — `beforeLoad` runs before `loader`; auth context flows into loader via route context