Open History to see the last commit
1import { createRoute } from './route'
2β
3import { useMatch } from './useMatch'
4import { useLoaderDeps } from './useLoaderDeps'
5import { useLoaderData } from './useLoaderData'
6import { useSearch } from './useSearch'
7import { useParams } from './useParams'
8import { useNavigate } from './useNavigate'
9import { useRouter } from './useRouter'
10import { useRouteContext } from './useRouteContext'
11import type { UseParamsRoute } from './useParams'
12import type { UseMatchRoute } from './useMatch'
13import type { UseSearchRoute } from './useSearch'
14import type {
15 AnyContext,
16 AnyRoute,
17 AnyRouter,
18 Constrain,
19 ConstrainLiteral,
20 FileBaseRouteOptions,
21 FileRoutesByPath,
22 LazyRouteOptions,
23 Register,
24 RegisteredRouter,
25 ResolveParams,
26 Route,
27 RouteById,
28 RouteConstraints,
29 RouteIds,
30 RouteLoaderEntry,
31 UpdatableRouteOptions,
32 UseNavigateResult,
33} from '@tanstack/router-core'
34import type { UseLoaderDepsRoute } from './useLoaderDeps'
35import type { UseLoaderDataRoute } from './useLoaderData'
36import type { UseRouteContextRoute } from './useRouteContext'
37β
38export function createFileRoute<
39 TFilePath extends keyof FileRoutesByPath,
40 TParentRoute extends AnyRoute = FileRoutesByPath[TFilePath]['parentRoute'],
41 TId extends RouteConstraints['TId'] = FileRoutesByPath[TFilePath]['id'],
42 TPath extends RouteConstraints['TPath'] = FileRoutesByPath[TFilePath]['path'],
43 TFullPath extends RouteConstraints['TFullPath'] =
44 FileRoutesByPath[TFilePath]['fullPath'],
45>(
46 // eslint-disable-next-line unused-imports/no-unused-vars
47 path?: TFilePath,
48): FileRoute<TFilePath, TParentRoute, TId, TPath, TFullPath>['createRoute'] {
49 return (options) => {
50 const route = createRoute(options as any)
51 ;(route as any).isRoot = false
52 return route as any
53 }
54}
55β
56/**
57 @deprecated It's no longer recommended to use the `FileRoute` class directly.
58 Instead, use `createFileRoute('/path/to/file')(options)` to create a file route.
59*/
60export class FileRoute<
61 TFilePath extends keyof FileRoutesByPath,
62 TParentRoute extends AnyRoute = FileRoutesByPath[TFilePath]['parentRoute'],
63 TId extends RouteConstraints['TId'] = FileRoutesByPath[TFilePath]['id'],
64 TPath extends RouteConstraints['TPath'] = FileRoutesByPath[TFilePath]['path'],
65 TFullPath extends RouteConstraints['TFullPath'] =
66 FileRoutesByPath[TFilePath]['fullPath'],
67> {
68 silent?: boolean
69β
70 constructor(
71 public path?: TFilePath,
72 _opts?: { silent: boolean },
73 ) {
74 this.silent = _opts?.silent
75 }
76β
77 createRoute = <
78 TRegister = Register,
79 TSearchValidator = undefined,
80 TParams = ResolveParams<TPath>,
81 TRouteContextFn = AnyContext,
82 TBeforeLoadFn = AnyContext,
83 TLoaderDeps extends Record<string, any> = {},
84 TLoaderFn = undefined,
85 TChildren = unknown,
86 TSSR = unknown,
87 const TMiddlewares = unknown,
88 THandlers = undefined,
89 >(
90 options?: FileBaseRouteOptions<
91 TRegister,
92 TParentRoute,
93 TId,
94 TPath,
95 TSearchValidator,
96 TParams,
97 TLoaderDeps,
98 TLoaderFn,
99 AnyContext,
100 TRouteContextFn,
101 TBeforeLoadFn,
102 AnyContext,
103 TSSR,
104 TMiddlewares,
105 THandlers
106 > &
107 UpdatableRouteOptions<
108 TParentRoute,
109 TId,
110 TFullPath,
111 TParams,
112 TSearchValidator,
113 TLoaderFn,
114 TLoaderDeps,
115 AnyContext,
116 TRouteContextFn,
117 TBeforeLoadFn
118 >,
119 ): Route<
120 TRegister,
121 TParentRoute,
122 TPath,
123 TFullPath,
124 TFilePath,
125 TId,
126 TSearchValidator,
127 TParams,
128 AnyContext,
129 TRouteContextFn,
130 TBeforeLoadFn,
131 TLoaderDeps,
132 TLoaderFn,
133 TChildren,
134 unknown,
135 TSSR,
136 TMiddlewares,
137 THandlers
138 > => {
139 if (process.env.NODE_ENV !== 'production') {
140 if (!this.silent) {
141 console.warn(
142 'Warning: FileRoute is deprecated and will be removed in the next major version. Use the createFileRoute(path)(options) function instead.',
143 )
144 }
145 }
146 const route = createRoute(options as any)
147 ;(route as any).isRoot = false
148 return route as any
149 }
150}
151β
152/**
153 @deprecated It's recommended not to split loaders into separate files.
154 Instead, place the loader function in the main route file, inside the
155 `createFileRoute('/path/to/file)(options)` options.
156*/
157export function FileRouteLoader<
158 TFilePath extends keyof FileRoutesByPath,
159 TRoute extends FileRoutesByPath[TFilePath]['preLoaderRoute'],
160>(
161 _path: TFilePath,
162): <TLoaderFn>(
163 loaderFn: Constrain<
164 TLoaderFn,
165 RouteLoaderEntry<
166 Register,
167 TRoute['parentRoute'],
168 TRoute['types']['id'],
169 TRoute['types']['params'],
170 TRoute['types']['loaderDeps'],
171 TRoute['types']['routerContext'],
172 TRoute['types']['routeContextFn'],
173 TRoute['types']['beforeLoadFn']
174 >
175 >,
176) => TLoaderFn {
177 if (process.env.NODE_ENV !== 'production') {
178 console.warn(
179 `Warning: FileRouteLoader is deprecated and will be removed in the next major version. Please place the loader function in the main route file, inside the \`createFileRoute('/path/to/file')(options)\` options`,
180 )
181 }
182 return (loaderFn) => loaderFn as any
183}
184β
185declare module '@tanstack/router-core' {
186 export interface LazyRoute<in out TRoute extends AnyRoute> {
187 useMatch: UseMatchRoute<TRoute['id']>
188 useRouteContext: UseRouteContextRoute<TRoute['id']>
189 useSearch: UseSearchRoute<TRoute['id']>
190 useParams: UseParamsRoute<TRoute['id']>
191 useLoaderDeps: UseLoaderDepsRoute<TRoute['id']>
192 useLoaderData: UseLoaderDataRoute<TRoute['id']>
193 useNavigate: () => UseNavigateResult<TRoute['fullPath']>
194 }
195}
196β
197export class LazyRoute<TRoute extends AnyRoute> {
198 options: {
199 id: string
200 } & LazyRouteOptions
201β
202 constructor(
203 opts: {
204 id: string
205 } & LazyRouteOptions,
206 ) {
207 this.options = opts
208 }
209β
210 useMatch: UseMatchRoute<TRoute['id']> = (opts) => {
211 return useMatch({ ...opts, from: this.options.id } as any) as any
212 }
213β
214 useRouteContext: UseRouteContextRoute<TRoute['id']> = (opts) => {
215 return useRouteContext({ ...(opts as any), from: this.options.id })
216 }
217β
218 useSearch: UseSearchRoute<TRoute['id']> = (opts) => {
219 return useSearch({ ...opts, from: this.options.id } as any)
220 }
221β
222 useParams: UseParamsRoute<TRoute['id']> = (opts) => {
223 return useParams({ ...opts, from: this.options.id } as any)
224 }
225β
226 useLoaderDeps: UseLoaderDepsRoute<TRoute['id']> = (opts) => {
227 return useLoaderDeps({ ...opts, from: this.options.id } as any)
228 }
229β
230 useLoaderData: UseLoaderDataRoute<TRoute['id']> = (opts) => {
231 return useLoaderData({ ...opts, from: this.options.id } as any)
232 }
233β
234 useNavigate = (): UseNavigateResult<TRoute['fullPath']> => {
235 const router = useRouter()
236 return useNavigate({ from: router.routesById[this.options.id].fullPath })
237 }
238}
239β
240export function createLazyRoute<
241 TRouter extends AnyRouter = RegisteredRouter,
242 TId extends string = string,
243 TRoute extends AnyRoute = RouteById<TRouter['routeTree'], TId>,
244>(id: ConstrainLiteral<TId, RouteIds<TRouter['routeTree']>>) {
245 return (opts: LazyRouteOptions) => {
246 return new LazyRoute<TRoute>({
247 id: id,
248 ...opts,
249 })
250 }
251}
252export function createLazyFileRoute<
253 TFilePath extends keyof FileRoutesByPath,
254 TRoute extends FileRoutesByPath[TFilePath]['preLoaderRoute'],
255>(id: TFilePath): (opts: LazyRouteOptions) => LazyRoute<TRoute> {
256 if (typeof id === 'object') {
257 return new LazyRoute<TRoute>(id) as any
258 }
259β
260 return (opts: LazyRouteOptions) => new LazyRoute<TRoute>({ id, ...opts })
261}