@tanstack/react-router 1.48.4 → 1.49.0
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/dist/cjs/Matches.cjs +3 -1
- package/dist/cjs/Matches.cjs.map +1 -1
- package/dist/cjs/Matches.d.cts +5 -4
- package/dist/cjs/fileRoute.cjs.map +1 -1
- package/dist/cjs/fileRoute.d.cts +4 -4
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +3 -1
- package/dist/cjs/route.cjs +1 -1
- package/dist/cjs/route.cjs.map +1 -1
- package/dist/cjs/route.d.cts +73 -59
- package/dist/cjs/router.cjs +90 -57
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/cjs/router.d.cts +22 -9
- package/dist/cjs/transformer.cjs +39 -0
- package/dist/cjs/transformer.cjs.map +1 -0
- package/dist/cjs/transformer.d.cts +5 -0
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +1 -0
- package/dist/esm/Matches.d.ts +5 -4
- package/dist/esm/Matches.js +3 -1
- package/dist/esm/Matches.js.map +1 -1
- package/dist/esm/fileRoute.d.ts +4 -4
- package/dist/esm/fileRoute.js.map +1 -1
- package/dist/esm/index.d.ts +3 -1
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/route.d.ts +73 -59
- package/dist/esm/route.js +1 -1
- package/dist/esm/route.js.map +1 -1
- package/dist/esm/router.d.ts +22 -9
- package/dist/esm/router.js +91 -58
- package/dist/esm/router.js.map +1 -1
- package/dist/esm/transformer.d.ts +5 -0
- package/dist/esm/transformer.js +39 -0
- package/dist/esm/transformer.js.map +1 -0
- package/dist/esm/utils.d.ts +1 -0
- package/dist/esm/utils.js.map +1 -1
- package/package.json +2 -2
- package/src/Matches.tsx +11 -7
- package/src/fileRoute.ts +26 -21
- package/src/index.tsx +5 -1
- package/src/route.ts +356 -186
- package/src/router.ts +144 -75
- package/src/transformer.ts +49 -0
- package/src/utils.ts +5 -0
package/dist/cjs/route.d.cts
CHANGED
|
@@ -5,7 +5,7 @@ import { NavigateOptions, ParsePathParams, ToMaskOptions } from './link.cjs';
|
|
|
5
5
|
import { ParsedLocation } from './location.cjs';
|
|
6
6
|
import { RouteById, RouteIds, RoutePaths } from './routeInfo.cjs';
|
|
7
7
|
import { AnyRouter, RegisteredRouter, Router } from './router.cjs';
|
|
8
|
-
import { Assign, Expand, NoInfer, PickRequired } from './utils.cjs';
|
|
8
|
+
import { Assign, Constrain, Expand, NoInfer, PickRequired } from './utils.cjs';
|
|
9
9
|
import { BuildLocationFn, NavigateFn } from './RouterProvider.cjs';
|
|
10
10
|
import { NotFoundError } from './not-found.cjs';
|
|
11
11
|
import { LazyRoute } from './fileRoute.cjs';
|
|
@@ -32,7 +32,7 @@ export type RoutePathOptionsIntersection<TCustomId, TPath> = {
|
|
|
32
32
|
path: TPath;
|
|
33
33
|
id: TCustomId;
|
|
34
34
|
};
|
|
35
|
-
export type RouteOptions<TParentRoute extends AnyRoute = AnyRoute, TCustomId extends string = string, TPath extends string = string, TSearchValidator extends AnySearchValidator = DefaultSearchValidator, TParams = AnyPathParams, TAllParams = TParams,
|
|
35
|
+
export type RouteOptions<TParentRoute extends AnyRoute = AnyRoute, TCustomId extends string = string, TPath extends string = string, TSearchValidator extends AnySearchValidator = DefaultSearchValidator, TParams = AnyPathParams, TAllParams = TParams, TLoaderDeps extends Record<string, any> = {}, TLoaderDataReturn = {}, TLoaderData = ResolveLoaderData<TLoaderDataReturn>, TRouterContext = {}, TRouteContextFn = AnyContext, TBeforeLoadFn = AnyContext> = BaseRouteOptions<TParentRoute, TCustomId, TPath, TSearchValidator, TParams, TLoaderDeps, TLoaderDataReturn, TRouterContext, TRouteContextFn, TBeforeLoadFn> & UpdatableRouteOptions<NoInfer<TParentRoute>, NoInfer<TCustomId>, NoInfer<TAllParams>, NoInfer<TSearchValidator>, NoInfer<TLoaderData>, NoInfer<TLoaderDeps>, NoInfer<TRouterContext>, NoInfer<TRouteContextFn>, NoInfer<TBeforeLoadFn>>;
|
|
36
36
|
export type ParseParamsFn<TPath extends string, TParams> = (rawParams: Record<ParsePathParams<TPath>, string>) => TParams extends Record<ParsePathParams<TPath>, any> ? TParams : Record<ParsePathParams<TPath>, any>;
|
|
37
37
|
export type StringifyParamsFn<TPath extends string, TParams> = (params: TParams) => Record<ParsePathParams<TPath>, string>;
|
|
38
38
|
export type ParamsOptions<TPath extends string, TParams> = {
|
|
@@ -49,24 +49,26 @@ export type ParamsOptions<TPath extends string, TParams> = {
|
|
|
49
49
|
*/
|
|
50
50
|
stringifyParams?: StringifyParamsFn<TPath, TParams>;
|
|
51
51
|
};
|
|
52
|
-
export interface FullSearchSchemaOption<
|
|
53
|
-
search:
|
|
52
|
+
export interface FullSearchSchemaOption<in out TParentRoute extends AnyRoute, in out TSearchValidator extends AnySearchValidator> {
|
|
53
|
+
search: Expand<ResolveFullSearchSchema<TParentRoute, TSearchValidator>>;
|
|
54
54
|
}
|
|
55
|
-
export type
|
|
55
|
+
export type RouteContextFn<in out TParentRoute extends AnyRoute, in out TSearchValidator extends AnySearchValidator, in out TParams, in out TRouterContext> = (ctx: RouteContextOptions<TParentRoute, TSearchValidator, TParams, TRouterContext>) => any;
|
|
56
|
+
export type BeforeLoadFn<in out TParentRoute extends AnyRoute, in out TSearchValidator extends AnySearchValidator, in out TParams, in out TRouterContext, in out TRouteContextFn> = (ctx: BeforeLoadContextOptions<TParentRoute, TSearchValidator, TParams, TRouterContext, TRouteContextFn>) => any;
|
|
57
|
+
export type FileBaseRouteOptions<TParentRoute extends AnyRoute = AnyRoute, TPath extends string = string, TSearchValidator extends AnySearchValidator = undefined, TParams = {}, TLoaderDeps extends Record<string, any> = {}, TLoaderDataReturn = {}, TRouterContext = {}, TRouteContextFn = AnyContext, TBeforeLoadFn = AnyContext> = ParamsOptions<TPath, TParams> & {
|
|
56
58
|
validateSearch?: TSearchValidator;
|
|
57
|
-
shouldReload?: boolean | ((match: LoaderFnContext<
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
59
|
+
shouldReload?: boolean | ((match: LoaderFnContext<TParentRoute, TParams, TLoaderDeps, TRouterContext, TRouteContextFn, TBeforeLoadFn>) => any);
|
|
60
|
+
context?: Constrain<TRouteContextFn, RouteContextFn<TParentRoute, TSearchValidator, TParams, TRouterContext>>;
|
|
61
|
+
beforeLoad?: Constrain<TBeforeLoadFn, BeforeLoadFn<TParentRoute, TSearchValidator, TParams, TRouterContext, TRouteContextFn>>;
|
|
62
|
+
loaderDeps?: (opts: FullSearchSchemaOption<TParentRoute, TSearchValidator>) => TLoaderDeps;
|
|
63
|
+
loader?: (ctx: LoaderFnContext<TParentRoute, TParams, TLoaderDeps, TRouterContext, TRouteContextFn, TBeforeLoadFn>) => TLoaderDataReturn | Promise<TLoaderDataReturn>;
|
|
64
|
+
};
|
|
65
|
+
export type BaseRouteOptions<TParentRoute extends AnyRoute = AnyRoute, TCustomId extends string = string, TPath extends string = string, TSearchValidator extends AnySearchValidator = undefined, TParams = {}, TLoaderDeps extends Record<string, any> = {}, TLoaderDataReturn = {}, TRouterContext = {}, TRouteContextFn = AnyContext, TBeforeLoadFn = AnyContext> = RoutePathOptions<TCustomId, TPath> & FileBaseRouteOptions<TParentRoute, TPath, TSearchValidator, TParams, TLoaderDeps, TLoaderDataReturn, TRouterContext, TRouteContextFn, TBeforeLoadFn> & {
|
|
63
66
|
getParentRoute: () => TParentRoute;
|
|
64
67
|
};
|
|
65
|
-
export interface
|
|
68
|
+
export interface ContextOptions<in out TParentRoute extends AnyRoute, in out TSearchValidator extends AnySearchValidator, in out TParams> extends FullSearchSchemaOption<TParentRoute, TSearchValidator> {
|
|
66
69
|
abortController: AbortController;
|
|
67
70
|
preload: boolean;
|
|
68
|
-
params: Expand<
|
|
69
|
-
context: TParentAllContext;
|
|
71
|
+
params: Expand<ResolveAllParamsFromParent<TParentRoute, TParams>>;
|
|
70
72
|
location: ParsedLocation;
|
|
71
73
|
/**
|
|
72
74
|
* @deprecated Use `throw redirect({ to: '/somewhere' })` instead
|
|
@@ -75,7 +77,13 @@ export interface BeforeLoadContext<TFullSearchSchema, TAllParams, TParentAllCont
|
|
|
75
77
|
buildLocation: BuildLocationFn;
|
|
76
78
|
cause: 'preload' | 'enter' | 'stay';
|
|
77
79
|
}
|
|
78
|
-
export
|
|
80
|
+
export interface RouteContextOptions<in out TParentRoute extends AnyRoute, in out TSearchValidator extends AnySearchValidator, in out TParams, in out TRouterContext> extends ContextOptions<TParentRoute, TSearchValidator, TParams> {
|
|
81
|
+
context: Expand<RouteContextParameter<TParentRoute, TRouterContext>>;
|
|
82
|
+
}
|
|
83
|
+
export interface BeforeLoadContextOptions<in out TParentRoute extends AnyRoute, in out TSearchValidator extends AnySearchValidator, in out TParams, in out TRouterContext, in out TRouteContextFn> extends ContextOptions<TParentRoute, TSearchValidator, TParams> {
|
|
84
|
+
context: Expand<BeforeLoadContextParameter<TParentRoute, TRouterContext, TRouteContextFn>>;
|
|
85
|
+
}
|
|
86
|
+
export type UpdatableRouteOptions<TParentRoute extends AnyRoute, TRouteId, TAllParams, TSearchValidator extends AnySearchValidator, TLoaderData, TLoaderDeps, TRouterContext, TRouteContextFn, TBeforeLoadFn> = {
|
|
79
87
|
caseSensitive?: boolean;
|
|
80
88
|
wrapInSuspense?: boolean;
|
|
81
89
|
component?: RouteComponent;
|
|
@@ -86,18 +94,19 @@ export type UpdatableRouteOptions<TParentRoute extends AnyRoute, TRouteId, TAllP
|
|
|
86
94
|
pendingMinMs?: number;
|
|
87
95
|
staleTime?: number;
|
|
88
96
|
gcTime?: number;
|
|
97
|
+
preload?: boolean;
|
|
89
98
|
preloadStaleTime?: number;
|
|
90
99
|
preloadGcTime?: number;
|
|
91
100
|
preSearchFilters?: Array<SearchFilter<ResolveFullSearchSchema<TParentRoute, TSearchValidator>>>;
|
|
92
101
|
postSearchFilters?: Array<SearchFilter<ResolveFullSearchSchema<TParentRoute, TSearchValidator>>>;
|
|
93
102
|
onCatch?: (error: Error, errorInfo: React.ErrorInfo) => void;
|
|
94
103
|
onError?: (err: any) => void;
|
|
95
|
-
onEnter?: (match: RouteMatch<TRouteId, TAllParams, ResolveFullSearchSchema<TParentRoute, TSearchValidator>, TLoaderData,
|
|
96
|
-
onStay?: (match: RouteMatch<TRouteId, TAllParams, ResolveFullSearchSchema<TParentRoute, TSearchValidator>, TLoaderData,
|
|
97
|
-
onLeave?: (match: RouteMatch<TRouteId, TAllParams, ResolveFullSearchSchema<TParentRoute, TSearchValidator>, TLoaderData,
|
|
104
|
+
onEnter?: (match: RouteMatch<TRouteId, TAllParams, ResolveFullSearchSchema<TParentRoute, TSearchValidator>, TLoaderData, ResolveAllContext<TParentRoute, TRouterContext, TRouteContextFn, TBeforeLoadFn>, TLoaderDeps>) => void;
|
|
105
|
+
onStay?: (match: RouteMatch<TRouteId, TAllParams, ResolveFullSearchSchema<TParentRoute, TSearchValidator>, TLoaderData, ResolveAllContext<TParentRoute, TRouterContext, TRouteContextFn, TBeforeLoadFn>, TLoaderDeps>) => void;
|
|
106
|
+
onLeave?: (match: RouteMatch<TRouteId, TAllParams, ResolveFullSearchSchema<TParentRoute, TSearchValidator>, TLoaderData, ResolveAllContext<TParentRoute, TRouterContext, TRouteContextFn, TBeforeLoadFn>, TLoaderDeps>) => void;
|
|
98
107
|
meta?: (ctx: {
|
|
99
|
-
matches: Array<RouteMatch<TRouteId, TAllParams, ResolveFullSearchSchema<TParentRoute, TSearchValidator>, TLoaderData,
|
|
100
|
-
match: RouteMatch<TRouteId, TAllParams, ResolveFullSearchSchema<TParentRoute, TSearchValidator>, TLoaderData,
|
|
108
|
+
matches: Array<RouteMatch<TRouteId, TAllParams, ResolveFullSearchSchema<TParentRoute, TSearchValidator>, TLoaderData, ResolveAllContext<TParentRoute, TRouterContext, TRouteContextFn, TBeforeLoadFn>, TLoaderDeps>>;
|
|
109
|
+
match: RouteMatch<TRouteId, TAllParams, ResolveFullSearchSchema<TParentRoute, TSearchValidator>, TLoaderData, ResolveAllContext<TParentRoute, TRouterContext, TRouteContextFn, TBeforeLoadFn>, TLoaderDeps>;
|
|
101
110
|
params: TAllParams;
|
|
102
111
|
loaderData: TLoaderData;
|
|
103
112
|
}) => Array<React.JSX.IntrinsicElements['meta']>;
|
|
@@ -157,13 +166,13 @@ export type SearchValidatorFn<TInput, TOutput> = (input: TInput) => TOutput;
|
|
|
157
166
|
export type SearchValidator<TInput, TOutput> = SearchValidatorObj<TInput, TOutput> | SearchValidatorFn<TInput, TOutput> | SearchValidatorAdapter<TInput, TOutput> | undefined;
|
|
158
167
|
export type AnySearchValidator = SearchValidator<any, any>;
|
|
159
168
|
export type DefaultSearchValidator = SearchValidator<Record<string, unknown>, AnySearchSchema>;
|
|
160
|
-
export type RouteLoaderFn<in out
|
|
161
|
-
export interface LoaderFnContext<in out
|
|
169
|
+
export type RouteLoaderFn<in out TParentRoute extends AnyRoute = AnyRoute, in out TParams = {}, in out TLoaderDeps = {}, in out TRouterContext = {}, in out TRouteContextFn = AnyContext, in out TBeforeLoadFn = AnyContext, TLoaderData = undefined> = (match: LoaderFnContext<TParentRoute, TParams, TLoaderDeps, TRouterContext, TRouteContextFn, TBeforeLoadFn>) => TLoaderData | Promise<TLoaderData>;
|
|
170
|
+
export interface LoaderFnContext<in out TParentRoute extends AnyRoute = AnyRoute, in out TParams = {}, in out TLoaderDeps = {}, in out TRouterContext = {}, in out TRouteContextFn = AnyContext, in out TBeforeLoadFn = AnyContext> {
|
|
162
171
|
abortController: AbortController;
|
|
163
172
|
preload: boolean;
|
|
164
|
-
params: Expand<
|
|
173
|
+
params: Expand<ResolveAllParamsFromParent<TParentRoute, TParams>>;
|
|
165
174
|
deps: TLoaderDeps;
|
|
166
|
-
context:
|
|
175
|
+
context: Expand<ResolveAllContext<TParentRoute, TRouterContext, TRouteContextFn, TBeforeLoadFn>>;
|
|
167
176
|
location: ParsedLocation;
|
|
168
177
|
/**
|
|
169
178
|
* @deprecated Use `throw redirect({ to: '/somewhere' })` instead
|
|
@@ -192,7 +201,7 @@ export type InferAllParams<TRoute> = TRoute extends {
|
|
|
192
201
|
allParams: infer TAllParams;
|
|
193
202
|
};
|
|
194
203
|
} ? TAllParams : {};
|
|
195
|
-
export type InferAllContext<TRoute> = TRoute extends {
|
|
204
|
+
export type InferAllContext<TRoute> = unknown extends TRoute ? TRoute : TRoute extends {
|
|
196
205
|
types: {
|
|
197
206
|
allContext: infer TAllContext;
|
|
198
207
|
};
|
|
@@ -203,16 +212,24 @@ export type ResolveSearchSchemaFn<TSearchValidator extends AnySearchValidator> =
|
|
|
203
212
|
export type ResolveSearchSchema<TSearchValidator extends AnySearchValidator> = unknown extends TSearchValidator ? TSearchValidator : TSearchValidator extends AnySearchValidatorAdapter ? TSearchValidator['types']['output'] : TSearchValidator extends AnySearchValidatorObj ? ResolveSearchSchemaFn<TSearchValidator['parse']> : ResolveSearchSchemaFn<TSearchValidator>;
|
|
204
213
|
export type ResolveFullSearchSchema<TParentRoute extends AnyRoute, TSearchValidator extends AnySearchValidator> = unknown extends TParentRoute ? ResolveSearchSchema<TSearchValidator> : Assign<InferFullSearchSchema<TParentRoute>, ResolveSearchSchema<TSearchValidator>>;
|
|
205
214
|
export type ResolveFullSearchSchemaInput<TParentRoute extends AnyRoute, TSearchValidator extends AnySearchValidator> = Assign<InferFullSearchSchemaInput<TParentRoute>, ResolveSearchSchemaInput<TSearchValidator>>;
|
|
206
|
-
export type
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
export type
|
|
215
|
+
export type LooseReturnType<T> = T extends (...args: Array<any>) => infer TReturn ? TReturn : never;
|
|
216
|
+
export type LooseAsyncReturnType<T> = T extends (...args: Array<any>) => infer TReturn ? TReturn extends Promise<infer TReturn> ? TReturn : TReturn : never;
|
|
217
|
+
export type ContextReturnType<TContextFn> = unknown extends TContextFn ? TContextFn : LooseReturnType<TContextFn> extends never ? AnyContext : LooseReturnType<TContextFn>;
|
|
218
|
+
export type ContextAsyncReturnType<TContextFn> = unknown extends TContextFn ? TContextFn : LooseAsyncReturnType<TContextFn> extends never ? AnyContext : LooseAsyncReturnType<TContextFn>;
|
|
219
|
+
export type RouteContextParameter<TParentRoute extends AnyRoute, TRouterContext> = unknown extends TParentRoute ? TRouterContext : Assign<TRouterContext, InferAllContext<TParentRoute>>;
|
|
220
|
+
export type ResolveRouteContext<TRouteContextFn, TBeforeLoadFn> = Assign<ContextReturnType<TRouteContextFn>, ContextAsyncReturnType<TBeforeLoadFn>>;
|
|
221
|
+
export type BeforeLoadContextParameter<TParentRoute extends AnyRoute, TRouterContext, TRouteContextFn> = Assign<RouteContextParameter<TParentRoute, TRouterContext>, ContextReturnType<TRouteContextFn>>;
|
|
222
|
+
export type ResolveAllContext<TParentRoute extends AnyRoute, TRouterContext, TRouteContextFn, TBeforeLoadFn> = Assign<BeforeLoadContextParameter<TParentRoute, TRouterContext, TRouteContextFn>, ContextAsyncReturnType<TBeforeLoadFn>>;
|
|
210
223
|
export type ResolveLoaderData<TLoaderDataReturn> = [TLoaderDataReturn] extends [
|
|
211
224
|
never
|
|
212
225
|
] ? undefined : TLoaderDataReturn;
|
|
213
226
|
export interface AnyRoute extends Route<any, any, any, any, any, any, any, any, any, any, any, any, any, any, any> {
|
|
214
227
|
}
|
|
215
|
-
export type AnyRouteWithContext<TContext> =
|
|
228
|
+
export type AnyRouteWithContext<TContext> = AnyRoute & {
|
|
229
|
+
types: {
|
|
230
|
+
allContext: TContext;
|
|
231
|
+
};
|
|
232
|
+
};
|
|
216
233
|
export type ResolveAllParamsFromParent<TParentRoute extends AnyRoute, TParams> = Assign<InferAllParams<TParentRoute>, TParams>;
|
|
217
234
|
export type RouteConstraints = {
|
|
218
235
|
TParentRoute: AnyRoute;
|
|
@@ -261,9 +278,9 @@ export declare class RouteApi<TId extends RouteIds<RegisteredRouter['routeTree']
|
|
|
261
278
|
useNavigate: () => UseNavigateResult<TRoute["fullPath"]>;
|
|
262
279
|
notFound: (opts?: NotFoundError) => NotFoundError;
|
|
263
280
|
}
|
|
264
|
-
export declare class Route<in out TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute, in out TPath extends RouteConstraints['TPath'] = '/', in out TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<TParentRoute, TPath>, in out TCustomId extends RouteConstraints['TCustomId'] = string, in out TId extends RouteConstraints['TId'] = ResolveId<TParentRoute, TCustomId, TPath>, in out TSearchValidator extends AnySearchValidator = DefaultSearchValidator, in out TParams = Record<ParsePathParams<TPath>, string>, in out TAllParams = ResolveAllParamsFromParent<TParentRoute, TParams>,
|
|
281
|
+
export declare class Route<in out TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute, in out TPath extends RouteConstraints['TPath'] = '/', in out TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<TParentRoute, TPath>, in out TCustomId extends RouteConstraints['TCustomId'] = string, in out TId extends RouteConstraints['TId'] = ResolveId<TParentRoute, TCustomId, TPath>, in out TSearchValidator extends AnySearchValidator = DefaultSearchValidator, in out TParams = Record<ParsePathParams<TPath>, string>, in out TAllParams = ResolveAllParamsFromParent<TParentRoute, TParams>, in out TRouterContext = AnyContext, in out TRouteContextFn = AnyContext, in out TBeforeLoadFn = AnyContext, in out TLoaderDeps extends Record<string, any> = {}, TLoaderDataReturn = {}, in out TLoaderData = ResolveLoaderData<TLoaderDataReturn>, in out TChildren = unknown> {
|
|
265
282
|
isRoot: TParentRoute extends Route<any> ? true : false;
|
|
266
|
-
options: RouteOptions<TParentRoute, TCustomId, TPath, TSearchValidator, TParams, TAllParams,
|
|
283
|
+
options: RouteOptions<TParentRoute, TCustomId, TPath, TSearchValidator, TParams, TAllParams, TLoaderDeps, TLoaderDataReturn, TLoaderData, TRouterContext, TRouteContextFn, TBeforeLoadFn>;
|
|
267
284
|
parentRoute: TParentRoute;
|
|
268
285
|
id: TId;
|
|
269
286
|
path: TPath;
|
|
@@ -278,7 +295,7 @@ export declare class Route<in out TParentRoute extends RouteConstraints['TParent
|
|
|
278
295
|
/**
|
|
279
296
|
* @deprecated Use the `createRoute` function instead.
|
|
280
297
|
*/
|
|
281
|
-
constructor(options?: RouteOptions<TParentRoute, TCustomId, TPath, TSearchValidator, TParams, TAllParams,
|
|
298
|
+
constructor(options?: RouteOptions<TParentRoute, TCustomId, TPath, TSearchValidator, TParams, TAllParams, TLoaderDeps, TLoaderDataReturn, TLoaderData, TRouterContext, TRouteContextFn, TBeforeLoadFn>);
|
|
282
299
|
types: {
|
|
283
300
|
parentRoute: TParentRoute;
|
|
284
301
|
path: TPath;
|
|
@@ -293,8 +310,11 @@ export declare class Route<in out TParentRoute extends RouteConstraints['TParent
|
|
|
293
310
|
fullSearchSchemaInput: ResolveFullSearchSchemaInput<TParentRoute, TSearchValidator>;
|
|
294
311
|
params: TParams;
|
|
295
312
|
allParams: TAllParams;
|
|
296
|
-
|
|
297
|
-
|
|
313
|
+
routerContext: TRouterContext;
|
|
314
|
+
routeContext: ResolveRouteContext<TRouteContextFn, TBeforeLoadFn>;
|
|
315
|
+
routeContextFn: TRouteContextFn;
|
|
316
|
+
beforeLoadFn: TBeforeLoadFn;
|
|
317
|
+
allContext: ResolveAllContext<TParentRoute, TRouterContext, TRouteContextFn, TBeforeLoadFn>;
|
|
298
318
|
children: TChildren;
|
|
299
319
|
loaderData: TLoaderData;
|
|
300
320
|
loaderDeps: TLoaderDeps;
|
|
@@ -302,17 +322,17 @@ export declare class Route<in out TParentRoute extends RouteConstraints['TParent
|
|
|
302
322
|
init: (opts: {
|
|
303
323
|
originalIndex: number;
|
|
304
324
|
}) => void;
|
|
305
|
-
addChildren<const TNewChildren extends Record<string, AnyRoute> | ReadonlyArray<AnyRoute>>(children: TNewChildren): Route<TParentRoute, TPath, TFullPath, TCustomId, TId, TSearchValidator, TParams, TAllParams,
|
|
325
|
+
addChildren<const TNewChildren extends Record<string, AnyRoute> | ReadonlyArray<AnyRoute>>(children: TNewChildren): Route<TParentRoute, TPath, TFullPath, TCustomId, TId, TSearchValidator, TParams, TAllParams, TRouterContext, TRouteContextFn, TBeforeLoadFn, TLoaderDeps, TLoaderDataReturn, TLoaderData, TNewChildren>;
|
|
306
326
|
updateLoader: <TNewLoaderData = unknown>(options: {
|
|
307
|
-
loader: RouteLoaderFn<
|
|
308
|
-
}) => Route<TParentRoute, TPath, TFullPath, TCustomId, TId, TSearchValidator, TParams, TAllParams,
|
|
309
|
-
update: (options: UpdatableRouteOptions<TParentRoute, TCustomId, TAllParams, TSearchValidator, TLoaderData,
|
|
327
|
+
loader: RouteLoaderFn<TParentRoute, TParams, TLoaderDeps, TRouterContext, TRouteContextFn, TBeforeLoadFn, TNewLoaderData>;
|
|
328
|
+
}) => Route<TParentRoute, TPath, TFullPath, TCustomId, TId, TSearchValidator, TParams, TAllParams, TRouterContext, TRouteContextFn, TBeforeLoadFn, TLoaderDeps, TNewLoaderData, TChildren>;
|
|
329
|
+
update: (options: UpdatableRouteOptions<TParentRoute, TCustomId, TAllParams, TSearchValidator, TLoaderData, TLoaderDeps, TRouterContext, TRouteContextFn, TBeforeLoadFn>) => this;
|
|
310
330
|
lazy: (lazyFn: () => Promise<LazyRoute<any>>) => this;
|
|
311
331
|
useMatch: <TRouter extends AnyRouter = AnyRouter, TRouteTree extends AnyRoute = TRouter["routeTree"], TRouteMatch = MakeRouteMatch<TRouteTree, TId>, TSelected = TRouteMatch>(opts?: {
|
|
312
332
|
select?: (match: TRouteMatch) => TSelected;
|
|
313
333
|
}) => TSelected;
|
|
314
|
-
useRouteContext: <TSelected = Expand<
|
|
315
|
-
select?: (search: Expand<
|
|
334
|
+
useRouteContext: <TSelected = Expand<Assign<Assign<RouteContextParameter<TParentRoute, TRouterContext>, ContextReturnType<TRouteContextFn>>, ContextAsyncReturnType<TBeforeLoadFn>>>>(opts?: {
|
|
335
|
+
select?: (search: Expand<ResolveAllContext<TParentRoute, TRouterContext, TRouteContextFn, TBeforeLoadFn>>) => TSelected;
|
|
316
336
|
}) => TSelected;
|
|
317
337
|
useSearch: <TSelected = Expand<ResolveFullSearchSchema<TParentRoute, TSearchValidator>>>(opts?: {
|
|
318
338
|
select?: (search: Expand<ResolveFullSearchSchema<TParentRoute, TSearchValidator>>) => TSelected;
|
|
@@ -328,25 +348,22 @@ export declare class Route<in out TParentRoute extends RouteConstraints['TParent
|
|
|
328
348
|
}) => TSelected;
|
|
329
349
|
useNavigate: () => UseNavigateResult<TFullPath>;
|
|
330
350
|
}
|
|
331
|
-
export declare function createRoute<TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute, TPath extends RouteConstraints['TPath'] = '/', TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<TParentRoute, TPath>, TCustomId extends RouteConstraints['TCustomId'] = string, TId extends RouteConstraints['TId'] = ResolveId<TParentRoute, TCustomId, TPath>, TSearchValidator extends AnySearchValidator = DefaultSearchValidator, TParams = Record<ParsePathParams<TPath>, string>, TAllParams = ResolveAllParamsFromParent<TParentRoute, TParams>,
|
|
351
|
+
export declare function createRoute<TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute, TPath extends RouteConstraints['TPath'] = '/', TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<TParentRoute, TPath>, TCustomId extends RouteConstraints['TCustomId'] = string, TId extends RouteConstraints['TId'] = ResolveId<TParentRoute, TCustomId, TPath>, TSearchValidator extends AnySearchValidator = DefaultSearchValidator, TParams = Record<ParsePathParams<TPath>, string>, TAllParams = ResolveAllParamsFromParent<TParentRoute, TParams>, TRouteContextFn = AnyContext, TBeforeLoadFn = AnyContext, TLoaderDeps extends Record<string, any> = {}, TLoaderDataReturn = {}, TLoaderData = ResolveLoaderData<TLoaderDataReturn>, TChildren = unknown>(options: RouteOptions<TParentRoute, TCustomId, TPath, TSearchValidator, TParams, TAllParams, TLoaderDeps, TLoaderDataReturn, TLoaderData, AnyContext, TRouteContextFn, TBeforeLoadFn>): Route<TParentRoute, TPath, TFullPath, TCustomId, TId, TSearchValidator, TParams, TAllParams, AnyContext, TRouteContextFn, TBeforeLoadFn, TLoaderDeps, TLoaderDataReturn, TLoaderData, TChildren>;
|
|
332
352
|
export type AnyRootRoute = RootRoute<any, any, any, any, any, any, any, any>;
|
|
333
|
-
export type RootRouteOptions<TSearchValidator extends AnySearchValidator = DefaultSearchValidator,
|
|
353
|
+
export type RootRouteOptions<TSearchValidator extends AnySearchValidator = DefaultSearchValidator, TRouterContext = {}, TRouteContextFn = AnyContext, TBeforeLoadFn = AnyContext, TLoaderDeps extends Record<string, any> = {}, TLoaderDataReturn = {}, TLoaderData = ResolveLoaderData<TLoaderDataReturn>> = Omit<RouteOptions<any, // TParentRoute
|
|
334
354
|
RootRouteId, // TCustomId
|
|
335
355
|
'', // TPath
|
|
336
356
|
TSearchValidator, {}, // TParams
|
|
337
357
|
{}, // TAllParams
|
|
338
|
-
TRouteContextReturn, // TRouteContextReturn
|
|
339
|
-
TRouteContext, // TRouteContext
|
|
340
|
-
TRouterContext, // TParentAllContext
|
|
341
|
-
Assign<TRouterContext, TRouteContext>, // TAllContext
|
|
342
358
|
TLoaderDeps, TLoaderDataReturn, // TLoaderDataReturn,
|
|
343
|
-
TLoaderData
|
|
344
|
-
|
|
359
|
+
TLoaderData, // TLoaderData,
|
|
360
|
+
TRouterContext, TRouteContextFn, TBeforeLoadFn>, 'path' | 'id' | 'getParentRoute' | 'caseSensitive' | 'parseParams' | 'stringifyParams' | 'params'>;
|
|
361
|
+
export declare function createRootRouteWithContext<TRouterContext extends {}>(): <TRouteContextFn = AnyContext, TBeforeLoadFn = AnyContext, TSearchValidator extends AnySearchValidator = DefaultSearchValidator, TLoaderDeps extends Record<string, any> = {}, TLoaderDataReturn = {}, TLoaderData = ResolveLoaderData<TLoaderDataReturn>>(options?: RootRouteOptions<TSearchValidator, TRouterContext, TRouteContextFn, TBeforeLoadFn, TLoaderDeps, TLoaderDataReturn, TLoaderData>) => RootRoute<TSearchValidator, TRouterContext, TRouteContextFn, TBeforeLoadFn, TLoaderDeps, TLoaderData, ResolveLoaderData<TLoaderData>, unknown>;
|
|
345
362
|
/**
|
|
346
363
|
* @deprecated Use the `createRootRouteWithContext` function instead.
|
|
347
364
|
*/
|
|
348
365
|
export declare const rootRouteWithContext: typeof createRootRouteWithContext;
|
|
349
|
-
export declare class RootRoute<in out TSearchValidator extends AnySearchValidator = DefaultSearchValidator,
|
|
366
|
+
export declare class RootRoute<in out TSearchValidator extends AnySearchValidator = DefaultSearchValidator, in out TRouterContext = {}, in out TRouteContextFn = AnyContext, in out TBeforeLoadFn = AnyContext, TLoaderDeps extends Record<string, any> = {}, TLoaderDataReturn = {}, in out TLoaderData = ResolveLoaderData<TLoaderDataReturn>, TChildren = unknown> extends Route<any, // TParentRoute
|
|
350
367
|
'/', // TPath
|
|
351
368
|
'/', // TFullPath
|
|
352
369
|
string, // TCustomId
|
|
@@ -354,17 +371,14 @@ RootRouteId, // TId
|
|
|
354
371
|
TSearchValidator, // TSearchValidator
|
|
355
372
|
{}, // TParams
|
|
356
373
|
{}, // TAllParams
|
|
357
|
-
|
|
358
|
-
TRouteContext, // TRouteContext
|
|
359
|
-
Assign<TRouterContext, TRouteContext>, // TAllContext
|
|
360
|
-
TLoaderDeps, TLoaderDataReturn, TLoaderData, TChildren> {
|
|
374
|
+
TRouterContext, TRouteContextFn, TBeforeLoadFn, TLoaderDeps, TLoaderDataReturn, TLoaderData, TChildren> {
|
|
361
375
|
/**
|
|
362
376
|
* @deprecated `RootRoute` is now an internal implementation detail. Use `createRootRoute()` instead.
|
|
363
377
|
*/
|
|
364
|
-
constructor(options?: RootRouteOptions<TSearchValidator,
|
|
365
|
-
addChildren<const TNewChildren extends Record<string, AnyRoute> | ReadonlyArray<AnyRoute>>(children: TNewChildren): RootRoute<TSearchValidator,
|
|
378
|
+
constructor(options?: RootRouteOptions<TSearchValidator, TRouterContext, TRouteContextFn, TBeforeLoadFn, TLoaderDeps, TLoaderDataReturn, TLoaderData>);
|
|
379
|
+
addChildren<const TNewChildren extends Record<string, AnyRoute> | ReadonlyArray<AnyRoute>>(children: TNewChildren): RootRoute<TSearchValidator, TRouterContext, TRouteContextFn, TBeforeLoadFn, TLoaderDeps, TLoaderDataReturn, TLoaderData, TNewChildren>;
|
|
366
380
|
}
|
|
367
|
-
export declare function createRootRoute<TSearchValidator extends AnySearchValidator = DefaultSearchValidator,
|
|
381
|
+
export declare function createRootRoute<TSearchValidator extends AnySearchValidator = DefaultSearchValidator, TRouterContext = {}, TRouteContextFn = AnyContext, TBeforeLoadFn = AnyContext, TLoaderDeps extends Record<string, any> = {}, TLoaderDataReturn = {}, TLoaderData = ResolveLoaderData<TLoaderDataReturn>>(options?: RootRouteOptions<TSearchValidator, TRouterContext, TRouteContextFn, TBeforeLoadFn, TLoaderDeps, TLoaderDataReturn, TLoaderData>): RootRoute<TSearchValidator, TRouterContext, TRouteContextFn, TBeforeLoadFn, TLoaderDeps, TLoaderDataReturn, TLoaderData, unknown>;
|
|
368
382
|
export type ResolveFullPath<TParentRoute extends AnyRoute, TPath extends string, TPrefixed = RoutePrefix<TParentRoute['fullPath'], TPath>> = TPrefixed extends RootRouteId ? '/' : TPrefixed;
|
|
369
383
|
type RoutePrefix<TPrefix extends string, TPath extends string> = string extends TPath ? RootRouteId : TPath extends string ? TPrefix extends RootRouteId ? TPath extends '/' ? '/' : `/${TrimPath<TPath>}` : `${TPrefix}/${TPath}` extends '/' ? '/' : `/${TrimPathLeft<`${TrimPathRight<TPrefix>}/${TrimPath<TPath>}`>}` : never;
|
|
370
384
|
export type TrimPath<T extends string> = '' extends T ? '' : TrimPathRight<TrimPathLeft<T>>;
|
|
@@ -411,7 +425,7 @@ export type AsyncRouteComponent<TProps> = SyncRouteComponent<TProps> & {
|
|
|
411
425
|
export type RouteComponent<TProps = any> = AsyncRouteComponent<TProps>;
|
|
412
426
|
export type ErrorRouteComponent = RouteComponent<ErrorComponentProps>;
|
|
413
427
|
export type NotFoundRouteComponent = SyncRouteComponent<NotFoundRouteProps>;
|
|
414
|
-
export declare class NotFoundRoute<TParentRoute extends AnyRootRoute,
|
|
415
|
-
constructor(options: Omit<RouteOptions<TParentRoute, string, string, TSearchValidator, {}, {},
|
|
428
|
+
export declare class NotFoundRoute<TParentRoute extends AnyRootRoute, TRouterContext = AnyContext, TRouteContextFn = AnyContext, TBeforeLoadFn = AnyContext, TSearchValidator extends AnySearchValidator = DefaultSearchValidator, TLoaderDeps extends Record<string, any> = {}, TLoaderDataReturn = {}, TLoaderData = ResolveLoaderData<TLoaderDataReturn>, TChildren = unknown> extends Route<TParentRoute, '/404', '/404', '404', '404', TSearchValidator, {}, {}, TRouterContext, TRouteContextFn, TBeforeLoadFn, TLoaderDeps, TLoaderDataReturn, TLoaderData, TChildren> {
|
|
429
|
+
constructor(options: Omit<RouteOptions<TParentRoute, string, string, TSearchValidator, {}, {}, TLoaderDeps, TLoaderDataReturn, TLoaderData, TRouterContext, TRouteContextFn, TBeforeLoadFn>, 'caseSensitive' | 'parseParams' | 'stringifyParams' | 'path' | 'id' | 'params'>);
|
|
416
430
|
}
|
|
417
431
|
export {};
|
package/dist/cjs/router.cjs
CHANGED
|
@@ -10,6 +10,7 @@ const utils = require("./utils.cjs");
|
|
|
10
10
|
const path = require("./path.cjs");
|
|
11
11
|
const redirects = require("./redirects.cjs");
|
|
12
12
|
const notFound = require("./not-found.cjs");
|
|
13
|
+
const transformer = require("./transformer.cjs");
|
|
13
14
|
const componentTypes = [
|
|
14
15
|
"component",
|
|
15
16
|
"errorComponent",
|
|
@@ -210,12 +211,12 @@ class Router {
|
|
|
210
211
|
});
|
|
211
212
|
return resolvedPath;
|
|
212
213
|
};
|
|
213
|
-
this.matchRoutes = (
|
|
214
|
+
this.matchRoutes = (next, opts) => {
|
|
214
215
|
let routeParams = {};
|
|
215
216
|
const foundRoute = this.flatRoutes.find((route) => {
|
|
216
217
|
const matchedParams = path.matchPathname(
|
|
217
218
|
this.basepath,
|
|
218
|
-
path.trimPathRight(pathname),
|
|
219
|
+
path.trimPathRight(next.pathname),
|
|
219
220
|
{
|
|
220
221
|
to: route.fullPath,
|
|
221
222
|
caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive,
|
|
@@ -235,7 +236,7 @@ class Router {
|
|
|
235
236
|
// If we found a route, and it's not an index route and we have left over path
|
|
236
237
|
foundRoute ? foundRoute.path !== "/" && routeParams["**"] : (
|
|
237
238
|
// Or if we didn't find a route and we have left over path
|
|
238
|
-
path.trimPathRight(pathname)
|
|
239
|
+
path.trimPathRight(next.pathname)
|
|
239
240
|
)
|
|
240
241
|
) {
|
|
241
242
|
if (this.options.notFoundRoute) {
|
|
@@ -284,10 +285,10 @@ class Router {
|
|
|
284
285
|
});
|
|
285
286
|
const matches = [];
|
|
286
287
|
matchedRoutes.forEach((route, index) => {
|
|
287
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
288
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
288
289
|
const parentMatch = matches[index - 1];
|
|
289
290
|
const [preMatchSearch, searchError] = (() => {
|
|
290
|
-
const parentSearch = (parentMatch == null ? void 0 : parentMatch.search) ??
|
|
291
|
+
const parentSearch = (parentMatch == null ? void 0 : parentMatch.search) ?? next.search;
|
|
291
292
|
try {
|
|
292
293
|
const validator = typeof route.options.validateSearch === "object" ? route.options.validateSearch.parse : route.options.validateSearch;
|
|
293
294
|
const search = (validator == null ? void 0 : validator(parentSearch)) ?? {};
|
|
@@ -345,8 +346,9 @@ class Router {
|
|
|
345
346
|
isFetching: false,
|
|
346
347
|
error: void 0,
|
|
347
348
|
paramsError: parseErrors[index],
|
|
348
|
-
|
|
349
|
-
|
|
349
|
+
__routeContext: {},
|
|
350
|
+
__beforeLoadContext: {},
|
|
351
|
+
context: {},
|
|
350
352
|
abortController: new AbortController(),
|
|
351
353
|
fetchCount: 0,
|
|
352
354
|
cause,
|
|
@@ -375,6 +377,30 @@ class Router {
|
|
|
375
377
|
}
|
|
376
378
|
match.search = utils.replaceEqualDeep(match.search, preMatchSearch);
|
|
377
379
|
match.searchError = searchError;
|
|
380
|
+
const parentMatchId = parentMatch == null ? void 0 : parentMatch.id;
|
|
381
|
+
const parentContext = !parentMatchId ? this.options.context ?? {} : parentMatch.context ?? this.options.context ?? {};
|
|
382
|
+
match.context = {
|
|
383
|
+
...parentContext,
|
|
384
|
+
...match.__routeContext,
|
|
385
|
+
...match.__beforeLoadContext
|
|
386
|
+
};
|
|
387
|
+
const contextFnContext = {
|
|
388
|
+
search: match.search,
|
|
389
|
+
params: match.params,
|
|
390
|
+
context: match.context,
|
|
391
|
+
location: next,
|
|
392
|
+
navigate: (opts2) => this.navigate({ ...opts2, _fromLocation: next }),
|
|
393
|
+
buildLocation: this.buildLocation,
|
|
394
|
+
cause: match.cause,
|
|
395
|
+
abortController: match.abortController,
|
|
396
|
+
preload: !!match.preload
|
|
397
|
+
};
|
|
398
|
+
match.__routeContext = ((_l = (_k = route.options).context) == null ? void 0 : _l.call(_k, contextFnContext)) ?? {};
|
|
399
|
+
match.context = {
|
|
400
|
+
...parentContext,
|
|
401
|
+
...match.__routeContext,
|
|
402
|
+
...match.__beforeLoadContext
|
|
403
|
+
};
|
|
378
404
|
matches.push(match);
|
|
379
405
|
});
|
|
380
406
|
return matches;
|
|
@@ -394,10 +420,10 @@ class Router {
|
|
|
394
420
|
this.buildLocation = (opts) => {
|
|
395
421
|
const build = (dest = {}, matches) => {
|
|
396
422
|
var _a, _b, _c;
|
|
397
|
-
const fromMatches = dest._fromLocation != null ? this.matchRoutes(
|
|
398
|
-
dest._fromLocation
|
|
399
|
-
dest.fromSearch || dest._fromLocation.search
|
|
400
|
-
) : this.state.matches;
|
|
423
|
+
const fromMatches = dest._fromLocation != null ? this.matchRoutes({
|
|
424
|
+
...dest._fromLocation,
|
|
425
|
+
search: dest.fromSearch || dest._fromLocation.search
|
|
426
|
+
}) : this.state.matches;
|
|
401
427
|
const fromMatch = dest.from != null ? fromMatches.find(
|
|
402
428
|
(d) => path.matchPathname(this.basepath, path.trimPathRight(d.pathname), {
|
|
403
429
|
to: dest.from,
|
|
@@ -489,8 +515,8 @@ class Router {
|
|
|
489
515
|
maskedNext = build(maskedDest);
|
|
490
516
|
}
|
|
491
517
|
}
|
|
492
|
-
const nextMatches = this.matchRoutes(next
|
|
493
|
-
const maskedMatches = maskedNext ? this.matchRoutes(maskedNext
|
|
518
|
+
const nextMatches = this.matchRoutes(next);
|
|
519
|
+
const maskedMatches = maskedNext ? this.matchRoutes(maskedNext) : void 0;
|
|
494
520
|
const maskedFinal = maskedNext ? build(maskedDest, maskedMatches) : void 0;
|
|
495
521
|
const final = build(dest, nextMatches);
|
|
496
522
|
if (maskedFinal) {
|
|
@@ -568,6 +594,13 @@ class Router {
|
|
|
568
594
|
ignoreBlocker,
|
|
569
595
|
...rest
|
|
570
596
|
} = {}) => {
|
|
597
|
+
const href = rest.href;
|
|
598
|
+
if (href) {
|
|
599
|
+
const parsed = history.parseHref(href, {});
|
|
600
|
+
rest.to = parsed.pathname;
|
|
601
|
+
rest.search = this.options.parseSearch(parsed.search);
|
|
602
|
+
rest.hash = parsed.hash;
|
|
603
|
+
}
|
|
571
604
|
const location = this.buildLocation(rest);
|
|
572
605
|
return this.commitLocation({
|
|
573
606
|
...location,
|
|
@@ -577,7 +610,7 @@ class Router {
|
|
|
577
610
|
ignoreBlocker
|
|
578
611
|
});
|
|
579
612
|
};
|
|
580
|
-
this.navigate = ({
|
|
613
|
+
this.navigate = ({ to, __isRedirect, ...rest }) => {
|
|
581
614
|
const toString = String(to);
|
|
582
615
|
let isExternal;
|
|
583
616
|
try {
|
|
@@ -591,7 +624,6 @@ class Router {
|
|
|
591
624
|
);
|
|
592
625
|
return this.buildAndCommitLocation({
|
|
593
626
|
...rest,
|
|
594
|
-
from,
|
|
595
627
|
to
|
|
596
628
|
// to: toString,
|
|
597
629
|
});
|
|
@@ -604,7 +636,8 @@ class Router {
|
|
|
604
636
|
}));
|
|
605
637
|
let redirect;
|
|
606
638
|
let notFound$1;
|
|
607
|
-
|
|
639
|
+
let loadPromise;
|
|
640
|
+
loadPromise = new Promise((resolve) => {
|
|
608
641
|
this.startReactTransition(async () => {
|
|
609
642
|
var _a;
|
|
610
643
|
try {
|
|
@@ -614,7 +647,7 @@ class Router {
|
|
|
614
647
|
this.cancelMatches();
|
|
615
648
|
let pendingMatches;
|
|
616
649
|
this.__store.batch(() => {
|
|
617
|
-
pendingMatches = this.matchRoutes(next
|
|
650
|
+
pendingMatches = this.matchRoutes(next);
|
|
618
651
|
this.__store.setState((s) => ({
|
|
619
652
|
...s,
|
|
620
653
|
status: "pending",
|
|
@@ -834,6 +867,7 @@ class Router {
|
|
|
834
867
|
};
|
|
835
868
|
for (const [index, { id: matchId, routeId }] of matches.entries()) {
|
|
836
869
|
const existingMatch = this.getMatch(matchId);
|
|
870
|
+
const parentMatchId = (_a = matches[index - 1]) == null ? void 0 : _a.id;
|
|
837
871
|
if (
|
|
838
872
|
// If we are in the middle of a load, either of these will be present
|
|
839
873
|
// (not to be confused with `loadPromise`, which is always defined)
|
|
@@ -852,13 +886,6 @@ class Router {
|
|
|
852
886
|
}));
|
|
853
887
|
const route = this.looseRoutesById[routeId];
|
|
854
888
|
const abortController = new AbortController();
|
|
855
|
-
const parentMatchId = (_a = matches[index - 1]) == null ? void 0 : _a.id;
|
|
856
|
-
const getParentContext = () => {
|
|
857
|
-
if (!parentMatchId) {
|
|
858
|
-
return this.options.context ?? {};
|
|
859
|
-
}
|
|
860
|
-
return this.getMatch(parentMatchId).context ?? this.options.context ?? {};
|
|
861
|
-
};
|
|
862
889
|
const pendingMs = route.options.pendingMs ?? this.options.defaultPendingMs;
|
|
863
890
|
const shouldPending = !!(onReady && !this.isServer && !preload && (route.options.loader || route.options.beforeLoad) && typeof pendingMs === "number" && pendingMs !== Infinity && (route.options.pendingComponent ?? this.options.defaultPendingComponent));
|
|
864
891
|
let pendingTimeout;
|
|
@@ -877,47 +904,54 @@ class Router {
|
|
|
877
904
|
if (searchError) {
|
|
878
905
|
handleSerialError(index, searchError, "VALIDATE_SEARCH");
|
|
879
906
|
}
|
|
880
|
-
const
|
|
907
|
+
const getParentMatchContext = () => parentMatchId ? this.getMatch(parentMatchId).context : this.options.context ?? {};
|
|
881
908
|
updateMatch(matchId, (prev) => ({
|
|
882
909
|
...prev,
|
|
883
910
|
isFetching: "beforeLoad",
|
|
884
911
|
fetchCount: prev.fetchCount + 1,
|
|
885
|
-
routeContext: utils.replaceEqualDeep(
|
|
886
|
-
prev.routeContext,
|
|
887
|
-
parentContext
|
|
888
|
-
),
|
|
889
|
-
context: utils.replaceEqualDeep(prev.context, parentContext),
|
|
890
912
|
abortController,
|
|
891
|
-
pendingTimeout
|
|
913
|
+
pendingTimeout,
|
|
914
|
+
context: {
|
|
915
|
+
...getParentMatchContext(),
|
|
916
|
+
...prev.__routeContext,
|
|
917
|
+
...prev.__beforeLoadContext
|
|
918
|
+
}
|
|
892
919
|
}));
|
|
893
|
-
const { search, params,
|
|
920
|
+
const { search, params, context, cause } = this.getMatch(matchId);
|
|
894
921
|
const beforeLoadFnContext = {
|
|
895
922
|
search,
|
|
896
923
|
abortController,
|
|
897
924
|
params,
|
|
898
925
|
preload: !!preload,
|
|
899
|
-
context
|
|
926
|
+
context,
|
|
900
927
|
location,
|
|
901
928
|
navigate: (opts) => this.navigate({ ...opts, _fromLocation: location }),
|
|
902
929
|
buildLocation: this.buildLocation,
|
|
903
930
|
cause: preload ? "preload" : cause
|
|
904
931
|
};
|
|
905
|
-
|
|
932
|
+
let beforeLoadContext = await ((_c = (_b = route.options).beforeLoad) == null ? void 0 : _c.call(_b, beforeLoadFnContext)) ?? {};
|
|
933
|
+
if (this.serializeLoaderData) {
|
|
934
|
+
beforeLoadContext = this.serializeLoaderData(
|
|
935
|
+
"__beforeLoadContext",
|
|
936
|
+
beforeLoadContext,
|
|
937
|
+
{
|
|
938
|
+
router: this,
|
|
939
|
+
match: this.getMatch(matchId)
|
|
940
|
+
}
|
|
941
|
+
);
|
|
942
|
+
}
|
|
906
943
|
if (redirects.isRedirect(beforeLoadContext) || notFound.isNotFound(beforeLoadContext)) {
|
|
907
944
|
handleSerialError(index, beforeLoadContext, "BEFORE_LOAD");
|
|
908
945
|
}
|
|
909
946
|
updateMatch(matchId, (prev) => {
|
|
910
|
-
const routeContext2 = {
|
|
911
|
-
...prev.routeContext,
|
|
912
|
-
...beforeLoadContext
|
|
913
|
-
};
|
|
914
947
|
return {
|
|
915
948
|
...prev,
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
949
|
+
__beforeLoadContext: beforeLoadContext,
|
|
950
|
+
context: {
|
|
951
|
+
...getParentMatchContext(),
|
|
952
|
+
...prev.__routeContext,
|
|
953
|
+
...beforeLoadContext
|
|
954
|
+
},
|
|
921
955
|
abortController
|
|
922
956
|
};
|
|
923
957
|
});
|
|
@@ -1010,10 +1044,14 @@ class Router {
|
|
|
1010
1044
|
await route._lazyPromise;
|
|
1011
1045
|
let loaderData = await ((_b2 = (_a2 = route.options).loader) == null ? void 0 : _b2.call(_a2, getLoaderContext()));
|
|
1012
1046
|
if (this.serializeLoaderData) {
|
|
1013
|
-
loaderData = this.serializeLoaderData(
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1047
|
+
loaderData = this.serializeLoaderData(
|
|
1048
|
+
"loaderData",
|
|
1049
|
+
loaderData,
|
|
1050
|
+
{
|
|
1051
|
+
router: this,
|
|
1052
|
+
match: this.getMatch(matchId)
|
|
1053
|
+
}
|
|
1054
|
+
);
|
|
1017
1055
|
}
|
|
1018
1056
|
handleRedirectAndNotFound(
|
|
1019
1057
|
this.getMatch(matchId),
|
|
@@ -1065,7 +1103,8 @@ class Router {
|
|
|
1065
1103
|
}
|
|
1066
1104
|
};
|
|
1067
1105
|
const { status, invalid } = this.getMatch(matchId);
|
|
1068
|
-
if (
|
|
1106
|
+
if (preload && route.options.preload === false) {
|
|
1107
|
+
} else if (status === "success" && (invalid || (shouldReload ?? age > staleAge))) {
|
|
1069
1108
|
;
|
|
1070
1109
|
(async () => {
|
|
1071
1110
|
try {
|
|
@@ -1147,7 +1186,7 @@ class Router {
|
|
|
1147
1186
|
};
|
|
1148
1187
|
this.preloadRoute = async (opts) => {
|
|
1149
1188
|
const next = this.buildLocation(opts);
|
|
1150
|
-
let matches = this.matchRoutes(next
|
|
1189
|
+
let matches = this.matchRoutes(next, {
|
|
1151
1190
|
throwOnError: true,
|
|
1152
1191
|
preload: true
|
|
1153
1192
|
});
|
|
@@ -1265,10 +1304,7 @@ class Router {
|
|
|
1265
1304
|
this.dehydratedData = ctx.payload;
|
|
1266
1305
|
(_c = (_b = this.options).hydrate) == null ? void 0 : _c.call(_b, ctx.payload);
|
|
1267
1306
|
const dehydratedState = ctx.router.state;
|
|
1268
|
-
const matches = this.matchRoutes(
|
|
1269
|
-
this.state.location.pathname,
|
|
1270
|
-
this.state.location.search
|
|
1271
|
-
).map((match) => {
|
|
1307
|
+
const matches = this.matchRoutes(this.state.location).map((match) => {
|
|
1272
1308
|
const dehydratedMatch = dehydratedState.dehydratedMatches.find(
|
|
1273
1309
|
(d) => d.id === match.id
|
|
1274
1310
|
);
|
|
@@ -1368,10 +1404,7 @@ class Router {
|
|
|
1368
1404
|
notFoundMode: options.notFoundMode ?? "fuzzy",
|
|
1369
1405
|
stringifySearch: options.stringifySearch ?? searchParams.defaultStringifySearch,
|
|
1370
1406
|
parseSearch: options.parseSearch ?? searchParams.defaultParseSearch,
|
|
1371
|
-
transformer: options.transformer ??
|
|
1372
|
-
parse: JSON.parse,
|
|
1373
|
-
stringify: JSON.stringify
|
|
1374
|
-
}
|
|
1407
|
+
transformer: options.transformer ?? transformer.defaultTransformer
|
|
1375
1408
|
});
|
|
1376
1409
|
if (typeof document !== "undefined") {
|
|
1377
1410
|
window.__TSR__ROUTER__ = this;
|