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