@tanstack/router-core 0.0.1-beta.174 → 0.0.1-beta.176
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/build/cjs/index.js +1 -0
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/route.js.map +1 -1
- package/build/cjs/router.js +57 -36
- package/build/cjs/router.js.map +1 -1
- package/build/esm/index.js +56 -36
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +158 -191
- package/build/types/defer.d.ts +19 -0
- package/build/types/fileRoute.d.ts +35 -0
- package/build/types/history.d.ts +31 -0
- package/build/types/index.d.ts +12 -780
- package/build/types/link.d.ts +92 -0
- package/build/types/path.d.ts +16 -0
- package/build/types/qss.d.ts +2 -0
- package/build/types/route.d.ts +234 -0
- package/build/types/routeInfo.d.ts +22 -0
- package/build/types/router.d.ts +251 -0
- package/build/types/scroll-restoration.d.ts +6 -0
- package/build/types/searchParams.d.ts +5 -0
- package/build/types/utils.d.ts +53 -0
- package/build/umd/index.development.js +57 -36
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +2 -2
- package/build/umd/index.production.js.map +1 -1
- package/package.json +3 -3
- package/src/route.ts +12 -16
- package/src/router.ts +100 -55
package/build/cjs/index.js
CHANGED
|
@@ -64,6 +64,7 @@ exports.PathParamError = router.PathParamError;
|
|
|
64
64
|
exports.Router = router.Router;
|
|
65
65
|
exports.SearchParamError = router.SearchParamError;
|
|
66
66
|
exports.componentTypes = router.componentTypes;
|
|
67
|
+
exports.isMatchInvalid = router.isMatchInvalid;
|
|
67
68
|
exports.isRedirect = router.isRedirect;
|
|
68
69
|
exports.lazyFn = router.lazyFn;
|
|
69
70
|
exports.redirect = router.redirect;
|
package/build/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/build/cjs/route.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.js","sources":["../../src/route.ts"],"sourcesContent":["import { ParsePathParams } from './link'\nimport { AnyRouter, Router, RouteMatch, RegisteredRouter } from './router'\nimport { IsAny, NoInfer, PickRequired, UnionToIntersection } from './utils'\nimport invariant from 'tiny-invariant'\nimport { joinPaths, trimPath } from './path'\n\nexport const rootRouteId = '__root__' as const\nexport type RootRouteId = typeof rootRouteId\nexport type AnyPathParams = {}\nexport type AnySearchSchema = {}\nexport type AnyContext = {}\nexport interface RouteMeta {}\nexport interface RouteContext {}\nexport interface RegisterRouteComponent<\n TLoader = unknown,\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> {\n // RouteComponent: unknown // This is registered by the framework\n}\nexport interface RegisterErrorRouteComponent<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> {\n // ErrorRouteComponent: unknown // This is registered by the framework\n}\nexport interface RegisterPendingRouteComponent<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> {\n // PendingRouteComponent: unknown // This is registered by the framework\n}\nexport interface RegisterRouteProps<\n TLoader = unknown,\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> {\n // RouteProps: unknown // This is registered by the framework\n}\nexport interface RegisterErrorRouteProps<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> {\n // ErrorRouteProps: unknown // This is registered by the framework\n}\n\nexport interface RegisterPendingRouteProps<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> {\n // PendingRouteProps: unknown // This is registered by the framework\n}\n\nexport type RegisteredRouteComponent<\n TLoader = unknown,\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> = RegisterRouteComponent<\n TLoader,\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n> extends {\n RouteComponent: infer T\n}\n ? T\n : () => unknown\n\nexport type RegisteredErrorRouteComponent<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> = RegisterErrorRouteComponent<\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n> extends {\n ErrorRouteComponent: infer T\n}\n ? T\n : () => unknown\n\nexport type RegisteredPendingRouteComponent<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> = RegisterPendingRouteComponent<\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n> extends {\n PendingRouteComponent: infer T\n}\n ? T\n : () => unknown\n\nexport type RegisteredRouteProps<\n TLoader = unknown,\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> = RegisterRouteProps<\n TLoader,\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n> extends {\n RouteProps: infer T\n}\n ? T\n : {}\n\nexport type RegisteredErrorRouteProps<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> = RegisterRouteProps<\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n> extends {\n ErrorRouteProps: infer T\n}\n ? T\n : {}\n\nexport type RegisteredPendingRouteProps<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> = RegisterRouteProps<\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n> extends {\n PendingRouteProps: infer T\n}\n ? T\n : {}\n\nexport type PreloadableObj = { preload?: () => Promise<void> }\n\nexport type RoutePathOptions<TCustomId, TPath> =\n | {\n path: TPath\n }\n | {\n id: TCustomId\n }\n\nexport type RoutePathOptionsIntersection<TCustomId, TPath> =\n UnionToIntersection<RoutePathOptions<TCustomId, TPath>>\n\nexport type MetaOptions = keyof PickRequired<RouteMeta> extends never\n ? {\n meta?: RouteMeta\n }\n : {\n meta: RouteMeta\n }\n\nexport type AnyRouteProps = RegisteredRouteProps<any, any, any, any, any>\n\nexport type RouteOptions<\n TParentRoute extends AnyRoute = AnyRoute,\n TCustomId extends string = string,\n TPath extends string = string,\n TLoader = unknown,\n TParentSearchSchema extends AnySearchSchema = {},\n TSearchSchema extends AnySearchSchema = {},\n TFullSearchSchema extends AnySearchSchema = TSearchSchema,\n TParams extends AnyPathParams = AnyPathParams,\n TAllParams extends AnyPathParams = TParams,\n TParentContext extends AnyContext = AnyContext,\n TAllParentContext extends AnyContext = AnyContext,\n TRouteContext extends RouteContext = RouteContext,\n TAllContext extends AnyContext = AnyContext,\n> = BaseRouteOptions<\n TParentRoute,\n TCustomId,\n TPath,\n TLoader,\n TParentSearchSchema,\n TSearchSchema,\n TFullSearchSchema,\n TParams,\n TAllParams,\n TParentContext,\n TAllParentContext,\n TRouteContext,\n TAllContext\n> &\n UpdatableRouteOptions<\n TLoader,\n TSearchSchema,\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n >\n\nexport type ParamsFallback<\n TPath extends string,\n TParams,\n> = unknown extends TParams ? Record<ParsePathParams<TPath>, string> : TParams\n\ntype Prefix<T extends string, U extends string> = U extends `${T}${infer _}`\n ? U\n : never\n\nexport type BaseRouteOptions<\n TParentRoute extends AnyRoute = AnyRoute,\n TCustomId extends string = string,\n TPath extends string = string,\n TLoader = unknown,\n TParentSearchSchema extends AnySearchSchema = {},\n TSearchSchema extends AnySearchSchema = {},\n TFullSearchSchema extends AnySearchSchema = TSearchSchema,\n TParams extends AnyPathParams = {},\n TAllParams = ParamsFallback<TPath, TParams>,\n TParentContext extends AnyContext = AnyContext,\n TAllParentContext extends AnyContext = AnyContext,\n TRouteContext extends RouteContext = RouteContext,\n TAllContext extends AnyContext = AnyContext,\n> = RoutePathOptions<TCustomId, TPath> & {\n layoutLimit?: string\n getParentRoute: () => TParentRoute\n validateSearch?: SearchSchemaValidator<TSearchSchema>\n loader?: LoaderFn<\n TLoader,\n TSearchSchema,\n TFullSearchSchema,\n TAllParams,\n NoInfer<TRouteContext>,\n TAllContext\n >\n} & ([TLoader] extends [never]\n ? {\n loader: 'Loaders must return a type other than never. If you are throwing a redirect() and not returning anything, return a redirect() instead.'\n }\n : {}) &\n (\n | {\n // Both or none\n parseParams?: (\n rawParams: IsAny<TPath, any, Record<ParsePathParams<TPath>, string>>,\n ) => TParams extends Record<ParsePathParams<TPath>, any>\n ? TParams\n : 'parseParams must return an object'\n stringifyParams?: (\n params: NoInfer<ParamsFallback<TPath, TParams>>,\n ) => Record<ParsePathParams<TPath>, string>\n }\n | {\n stringifyParams?: never\n parseParams?: never\n }\n ) &\n (keyof PickRequired<RouteContext> extends never\n ? {\n getContext?: GetContextFn<\n TParentRoute,\n TAllParams,\n TFullSearchSchema,\n TParentContext,\n TAllParentContext,\n TRouteContext\n >\n }\n : {\n getContext: GetContextFn<\n TParentRoute,\n TAllParams,\n TFullSearchSchema,\n TParentContext,\n TAllParentContext,\n TRouteContext\n >\n })\n\ntype GetContextFn<\n TParentRoute,\n TAllParams,\n TFullSearchSchema,\n TParentContext,\n TAllParentContext,\n TRouteContext,\n> = (\n opts: {\n params: TAllParams\n search: TFullSearchSchema\n } & (TParentRoute extends undefined\n ? {\n context?: TAllParentContext\n parentContext?: TParentContext\n }\n : {\n context: TAllParentContext\n parentContext: TParentContext\n }),\n) => TRouteContext\n\nexport type UpdatableRouteOptions<\n TLoader,\n TSearchSchema extends AnySearchSchema,\n TFullSearchSchema extends AnySearchSchema,\n TAllParams extends AnyPathParams,\n TRouteContext extends AnyContext,\n TAllContext extends AnyContext,\n> = MetaOptions & {\n key?: null | false | GetKeyFn<TFullSearchSchema, TAllParams>\n // If true, this route will be matched as case-sensitive\n caseSensitive?: boolean\n // If true, this route will be forcefully wrapped in a suspense boundary\n wrapInSuspense?: boolean\n // The content to be rendered when the route is matched. If no component is provided, defaults to `<Outlet />`\n component?: RegisteredRouteComponent<\n TLoader,\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n >\n // The content to be rendered when the route encounters an error\n errorComponent?: RegisteredErrorRouteComponent<\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n > //\n // If supported by your framework, the content to be rendered as the fallback content until the route is ready to render\n pendingComponent?: RegisteredPendingRouteComponent<\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n >\n // Filter functions that can manipulate search params *before* they are passed to links and navigate\n // calls that match this route.\n preSearchFilters?: SearchFilter<TFullSearchSchema>[]\n // Filter functions that can manipulate search params *after* they are passed to links and navigate\n // calls that match this route.\n postSearchFilters?: SearchFilter<TFullSearchSchema>[]\n // If set, preload matches of this route will be considered fresh for this many milliseconds.\n preloadMaxAge?: number\n // If set, a match of this route will be considered fresh for this many milliseconds.\n maxAge?: number\n // If set, a match of this route that becomes inactive (or unused) will be garbage collected after this many milliseconds\n gcMaxAge?: number\n // This async function is called before a route is loaded.\n // If an error is thrown here, the route's loader will not be called.\n // If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onError` function.\n // If thrown during a preload event, the error will be logged to the console.\n beforeLoad?: (\n opts: LoaderContext<\n TSearchSchema,\n TFullSearchSchema,\n TAllParams,\n NoInfer<TRouteContext>,\n TAllContext\n >,\n ) => Promise<void> | void\n onError?: (err: any) => void\n // This function is called\n // when moving from an inactive state to an active one. Likewise, when moving from\n // an active to an inactive state, the return function (if provided) is called.\n onLoaded?: (matchContext: {\n params: TAllParams\n search: TFullSearchSchema\n }) =>\n | void\n | undefined\n | ((match: { params: TAllParams; search: TFullSearchSchema }) => void)\n // This function is called when the route remains active from one transition to the next.\n onTransition?: (match: {\n params: TAllParams\n search: TFullSearchSchema\n }) => void\n}\n\nexport type ParseParamsOption<TPath extends string, TParams> = ParseParamsFn<\n TPath,\n TParams\n>\n\nexport type ParseParamsFn<TPath extends string, TParams> = (\n rawParams: IsAny<TPath, any, Record<ParsePathParams<TPath>, string>>,\n) => TParams extends Record<ParsePathParams<TPath>, any>\n ? TParams\n : 'parseParams must return an object'\n\nexport type ParseParamsObj<TPath extends string, TParams> = {\n parse?: ParseParamsFn<TPath, TParams>\n}\n\n// The parse type here allows a zod schema to be passed directly to the validator\nexport type SearchSchemaValidator<TReturn> =\n | SearchSchemaValidatorObj<TReturn>\n | SearchSchemaValidatorFn<TReturn>\n\nexport type SearchSchemaValidatorObj<TReturn> = {\n parse?: SearchSchemaValidatorFn<TReturn>\n}\n\nexport type SearchSchemaValidatorFn<TReturn> = (\n searchObj: Record<string, unknown>,\n) => TReturn\n\nexport type DefinedPathParamWarning =\n 'Path params cannot be redefined by child routes!'\n\nexport type ParentParams<TParentParams> = AnyPathParams extends TParentParams\n ? {}\n : {\n [Key in keyof TParentParams]?: DefinedPathParamWarning\n }\n\nexport type LoaderFn<\n TLoader = unknown,\n TSearchSchema extends AnySearchSchema = {},\n TFullSearchSchema extends AnySearchSchema = {},\n TAllParams = {},\n TContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> = (\n match: LoaderContext<\n TSearchSchema,\n TFullSearchSchema,\n TAllParams,\n TContext,\n TAllContext\n > & {\n parentMatchPromise?: Promise<void>\n },\n) => Promise<TLoader> | TLoader\n\nexport type GetKeyFn<\n TFullSearchSchema extends AnySearchSchema = {},\n TAllParams = {},\n> = (loaderContext: { params: TAllParams; search: TFullSearchSchema }) => any\n\nexport interface LoaderContext<\n TSearchSchema extends AnySearchSchema = {},\n TFullSearchSchema extends AnySearchSchema = {},\n TAllParams = {},\n TContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> {\n params: TAllParams\n routeSearch: TSearchSchema\n search: TFullSearchSchema\n abortController: AbortController\n preload: boolean\n routeContext: TContext\n context: TAllContext\n}\n\nexport type UnloaderFn<TPath extends string> = (\n routeMatch: RouteMatch<any, Route>,\n) => void\n\nexport type SearchFilter<T, U = T> = (prev: T) => U\n\nexport type ResolveId<\n TParentRoute,\n TCustomId extends string,\n TPath extends string,\n> = TParentRoute extends { id: infer TParentId extends string }\n ? RoutePrefix<TParentId, string extends TCustomId ? TPath : TCustomId>\n : RootRouteId\n\nexport type InferFullSearchSchema<TRoute> = TRoute extends {\n isRoot: true\n types: {\n searchSchema: infer TSearchSchema\n }\n}\n ? TSearchSchema\n : TRoute extends {\n types: {\n fullSearchSchema: infer TFullSearchSchema\n }\n }\n ? TFullSearchSchema\n : {}\n\nexport type ResolveFullSearchSchema<TParentRoute, TSearchSchema> =\n InferFullSearchSchema<TParentRoute> & TSearchSchema\n\nexport interface AnyRoute\n extends Route<\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any\n > {}\n\nexport type MergeParamsFromParent<T, U> = IsAny<T, U, T & U>\n\nexport type UseLoaderResult<T> = T\n// T extends Record<PropertyKey, infer U>\n// ? {\n// [K in keyof T]: UseLoaderResultPromise<T[K]>\n// }\n// : UseLoaderResultPromise<T>\n\n// export type UseLoaderResultPromise<T> = T extends Promise<infer U>\n// ? StreamedPromise<U>\n// : T\n\nexport type StreamedPromise<T> = {\n promise: Promise<T>\n status: 'resolved' | 'pending'\n data: T\n resolve: (value: T) => void\n}\n\nexport type RouteConstraints = {\n TParentRoute: AnyRoute\n TPath: string\n TFullPath: string\n TCustomId: string\n TId: string\n TSearchSchema: AnySearchSchema\n TFullSearchSchema: AnySearchSchema\n TParams: Record<string, any>\n TAllParams: Record<string, any>\n TParentContext: AnyContext\n TAllParentContext: AnyContext\n TRouteContext: RouteContext\n TAllContext: AnyContext\n TRouterContext: AnyContext\n TChildren: unknown\n TRouteTree: AnyRoute\n}\n\nexport class Route<\n TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute,\n TPath extends RouteConstraints['TPath'] = '/',\n TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<\n TParentRoute,\n TPath\n >,\n TCustomId extends RouteConstraints['TCustomId'] = string,\n TId extends RouteConstraints['TId'] = ResolveId<\n TParentRoute,\n TCustomId,\n TPath\n >,\n TLoader = unknown,\n TSearchSchema extends RouteConstraints['TSearchSchema'] = {},\n TFullSearchSchema extends RouteConstraints['TFullSearchSchema'] = ResolveFullSearchSchema<\n TParentRoute,\n TSearchSchema\n >,\n TParams extends RouteConstraints['TParams'] = Record<\n ParsePathParams<TPath>,\n string\n >,\n TAllParams extends RouteConstraints['TAllParams'] = MergeParamsFromParent<\n TParentRoute['types']['allParams'],\n TParams\n >,\n TParentContext extends RouteConstraints['TParentContext'] = TParentRoute['types']['routeContext'],\n TAllParentContext extends RouteConstraints['TAllParentContext'] = TParentRoute['types']['context'],\n TRouteContext extends RouteConstraints['TRouteContext'] = RouteContext,\n TAllContext extends RouteConstraints['TAllContext'] = MergeParamsFromParent<\n TParentRoute['types']['context'],\n TRouteContext\n >,\n TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,\n TChildren extends RouteConstraints['TChildren'] = unknown,\n TRouteTree extends RouteConstraints['TRouteTree'] = AnyRoute,\n> {\n types!: {\n parentRoute: TParentRoute\n path: TPath\n to: TrimPathRight<TFullPath>\n fullPath: TFullPath\n customId: TCustomId\n id: TId\n loader: TLoader\n searchSchema: TSearchSchema\n fullSearchSchema: TFullSearchSchema\n params: TParams\n allParams: TAllParams\n parentContext: TParentContext\n allParentContext: TAllParentContext\n routeContext: TRouteContext\n context: TAllContext\n children: TChildren\n routeTree: TRouteTree\n routerContext: TRouterContext\n }\n isRoot: TParentRoute extends Route<any> ? true : false\n options: RouteOptions<\n TParentRoute,\n TCustomId,\n TPath,\n TLoader,\n InferFullSearchSchema<TParentRoute>,\n TSearchSchema,\n TFullSearchSchema,\n TParams,\n TAllParams,\n TParentContext,\n TAllParentContext,\n TRouteContext,\n TAllContext\n > &\n UpdatableRouteOptions<\n TLoader,\n TSearchSchema,\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n >\n\n // Set up in this.init()\n parentRoute!: TParentRoute\n id!: TId\n // customId!: TCustomId\n path!: TPath\n fullPath!: TFullPath\n to!: TrimPathRight<TFullPath>\n\n // Optional\n children?: TChildren\n originalIndex?: number\n router?: AnyRouter\n rank!: number\n\n constructor(\n options: RouteOptions<\n TParentRoute,\n TCustomId,\n TPath,\n TLoader,\n InferFullSearchSchema<TParentRoute>,\n TSearchSchema,\n TFullSearchSchema,\n TParams,\n TAllParams,\n TParentContext,\n TAllParentContext,\n TRouteContext,\n TAllContext\n > &\n UpdatableRouteOptions<\n TLoader,\n TSearchSchema,\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n >,\n ) {\n this.options = (options as any) || {}\n this.isRoot = !options?.getParentRoute as any\n Route.__onInit(this)\n }\n\n init = (opts: { originalIndex: number; router: AnyRouter }) => {\n this.originalIndex = opts.originalIndex\n this.router = opts.router\n\n const options = this.options as RouteOptions<\n TParentRoute,\n TCustomId,\n TPath,\n InferFullSearchSchema<TParentRoute>,\n TSearchSchema,\n TParams\n > &\n RoutePathOptionsIntersection<TCustomId, TPath>\n\n const isRoot = !options?.path && !options?.id\n\n this.parentRoute = this.options?.getParentRoute?.()\n\n if (isRoot) {\n this.path = rootRouteId as TPath\n } else {\n invariant(\n this.parentRoute,\n `Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a Route instance.`,\n )\n }\n\n let path: undefined | string = isRoot ? rootRouteId : options.path\n\n // If the path is anything other than an index path, trim it up\n if (path && path !== '/') {\n path = trimPath(path)\n }\n\n const customId = options?.id || path\n\n // Strip the parentId prefix from the first level of children\n let id = isRoot\n ? rootRouteId\n : joinPaths([\n (this.parentRoute.id as any) === rootRouteId\n ? ''\n : this.parentRoute.id,\n customId,\n ])\n\n if (path === rootRouteId) {\n path = '/'\n }\n\n if (id !== rootRouteId) {\n id = joinPaths(['/', id])\n }\n\n const fullPath =\n id === rootRouteId ? '/' : joinPaths([this.parentRoute.fullPath, path])\n\n this.path = path as TPath\n this.id = id as TId\n // this.customId = customId as TCustomId\n this.fullPath = fullPath as TFullPath\n this.to = fullPath as TrimPathRight<TFullPath>\n }\n\n addChildren = <TNewChildren extends AnyRoute[]>(\n children: TNewChildren,\n ): Route<\n TParentRoute,\n TPath,\n TFullPath,\n TCustomId,\n TId,\n TLoader,\n TSearchSchema,\n TFullSearchSchema,\n TParams,\n TAllParams,\n TParentContext,\n TAllParentContext,\n TRouteContext,\n TAllContext,\n TRouterContext,\n TNewChildren,\n TRouteTree\n > => {\n this.children = children as any\n return this as any\n }\n\n update = (\n options: UpdatableRouteOptions<\n TLoader,\n TSearchSchema,\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n >,\n ) => {\n Object.assign(this.options, options)\n return this\n }\n\n static __onInit = (route: any) => {\n // This is a dummy static method that should get\n // replaced by a framework specific implementation if necessary\n }\n}\n\nexport type AnyRootRoute = RootRoute<any, any, any, any>\n\nexport class RouterContext<TRouterContext extends {}> {\n constructor() {}\n\n createRootRoute = <\n TLoader = unknown,\n TSearchSchema extends AnySearchSchema = {},\n TRouteContext extends RouteContext = RouteContext,\n >(\n options?: Omit<\n RouteOptions<\n AnyRoute,\n RootRouteId,\n '',\n TLoader,\n TSearchSchema,\n TSearchSchema,\n TSearchSchema,\n {},\n {},\n TRouterContext,\n TRouterContext,\n TRouteContext,\n MergeParamsFromParent<TRouterContext, TRouteContext>\n >,\n | 'path'\n | 'id'\n | 'getParentRoute'\n | 'caseSensitive'\n | 'parseParams'\n | 'stringifyParams'\n >,\n ): RootRoute<TLoader, TSearchSchema, TRouteContext, TRouterContext> => {\n return new RootRoute(options) as any\n }\n}\n\nexport class RootRoute<\n TLoader = unknown,\n TSearchSchema extends AnySearchSchema = {},\n TRouteContext extends RouteContext = RouteContext,\n TRouterContext extends {} = {},\n> extends Route<\n any,\n '/',\n '/',\n string,\n RootRouteId,\n TLoader,\n TSearchSchema,\n TSearchSchema,\n {},\n {},\n TRouterContext,\n TRouterContext,\n TRouteContext,\n MergeParamsFromParent<TRouterContext, TRouteContext>,\n TRouterContext,\n any,\n any\n> {\n constructor(\n options?: Omit<\n RouteOptions<\n AnyRoute,\n RootRouteId,\n '',\n TLoader,\n TSearchSchema,\n TSearchSchema,\n TSearchSchema,\n {},\n {},\n TRouterContext,\n TRouterContext,\n TRouteContext,\n MergeParamsFromParent<TRouterContext, TRouteContext>\n >,\n | 'path'\n | 'id'\n | 'getParentRoute'\n | 'caseSensitive'\n | 'parseParams'\n | 'stringifyParams'\n >,\n ) {\n super(options as any)\n }\n}\n\nexport type ResolveFullPath<\n TParentRoute extends AnyRoute,\n TPath extends string,\n TPrefixed = RoutePrefix<TParentRoute['fullPath'], TPath>,\n> = TPrefixed extends RootRouteId ? '/' : TPrefixed\n\ntype RoutePrefix<\n TPrefix extends string,\n TPath extends string,\n> = string extends TPath\n ? RootRouteId\n : TPath extends string\n ? TPrefix extends RootRouteId\n ? TPath extends '/'\n ? '/'\n : `/${TrimPath<TPath>}`\n : `${TPrefix}/${TPath}` extends '/'\n ? '/'\n : `/${TrimPathLeft<`${TrimPathRight<TPrefix>}/${TrimPath<TPath>}`>}`\n : never\n\nexport type TrimPath<T extends string> = '' extends T\n ? ''\n : TrimPathRight<TrimPathLeft<T>>\n\nexport type TrimPathLeft<T extends string> =\n T extends `${RootRouteId}/${infer U}`\n ? TrimPathLeft<U>\n : T extends `/${infer U}`\n ? TrimPathLeft<U>\n : T\nexport type TrimPathRight<T extends string> = T extends '/'\n ? '/'\n : T extends `${infer U}/`\n ? TrimPathRight<U>\n : T\n"],"names":["rootRouteId","Route","constructor","options","isRoot","getParentRoute","__onInit","init","opts","originalIndex","router","path","id","parentRoute","invariant","trimPath","customId","joinPaths","fullPath","to","addChildren","children","update","Object","assign","route","RouterContext","createRootRoute","RootRoute"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"route.js","sources":["../../src/route.ts"],"sourcesContent":["import { ParsePathParams } from './link'\nimport {\n AnyRouter,\n Router,\n RouteMatch,\n RegisteredRouter,\n AnyRouteMatch,\n} from './router'\nimport { IsAny, NoInfer, PickRequired, UnionToIntersection } from './utils'\nimport invariant from 'tiny-invariant'\nimport { joinPaths, trimPath } from './path'\n\nexport const rootRouteId = '__root__' as const\nexport type RootRouteId = typeof rootRouteId\nexport type AnyPathParams = {}\nexport type AnySearchSchema = {}\nexport type AnyContext = {}\nexport interface RouteMeta {}\nexport interface RouteContext {}\nexport interface RegisterRouteComponent<\n TLoader = unknown,\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> {\n // RouteComponent: unknown // This is registered by the framework\n}\nexport interface RegisterErrorRouteComponent<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> {\n // ErrorRouteComponent: unknown // This is registered by the framework\n}\nexport interface RegisterPendingRouteComponent<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> {\n // PendingRouteComponent: unknown // This is registered by the framework\n}\nexport interface RegisterRouteProps<\n TLoader = unknown,\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> {\n // RouteProps: unknown // This is registered by the framework\n}\nexport interface RegisterErrorRouteProps<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> {\n // ErrorRouteProps: unknown // This is registered by the framework\n}\n\nexport interface RegisterPendingRouteProps<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> {\n // PendingRouteProps: unknown // This is registered by the framework\n}\n\nexport type RegisteredRouteComponent<\n TLoader = unknown,\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> = RegisterRouteComponent<\n TLoader,\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n> extends {\n RouteComponent: infer T\n}\n ? T\n : () => unknown\n\nexport type RegisteredErrorRouteComponent<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> = RegisterErrorRouteComponent<\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n> extends {\n ErrorRouteComponent: infer T\n}\n ? T\n : () => unknown\n\nexport type RegisteredPendingRouteComponent<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> = RegisterPendingRouteComponent<\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n> extends {\n PendingRouteComponent: infer T\n}\n ? T\n : () => unknown\n\nexport type RegisteredRouteProps<\n TLoader = unknown,\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> = RegisterRouteProps<\n TLoader,\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n> extends {\n RouteProps: infer T\n}\n ? T\n : {}\n\nexport type RegisteredErrorRouteProps<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> = RegisterRouteProps<\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n> extends {\n ErrorRouteProps: infer T\n}\n ? T\n : {}\n\nexport type RegisteredPendingRouteProps<\n TFullSearchSchema extends AnySearchSchema = AnySearchSchema,\n TAllParams extends AnyPathParams = AnyPathParams,\n TRouteContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> = RegisterRouteProps<\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n> extends {\n PendingRouteProps: infer T\n}\n ? T\n : {}\n\nexport type PreloadableObj = { preload?: () => Promise<void> }\n\nexport type RoutePathOptions<TCustomId, TPath> =\n | {\n path: TPath\n }\n | {\n id: TCustomId\n }\n\nexport type RoutePathOptionsIntersection<TCustomId, TPath> =\n UnionToIntersection<RoutePathOptions<TCustomId, TPath>>\n\nexport type MetaOptions = keyof PickRequired<RouteMeta> extends never\n ? {\n meta?: RouteMeta\n }\n : {\n meta: RouteMeta\n }\n\nexport type AnyRouteProps = RegisteredRouteProps<any, any, any, any, any>\n\nexport type RouteOptions<\n TParentRoute extends AnyRoute = AnyRoute,\n TCustomId extends string = string,\n TPath extends string = string,\n TLoader = unknown,\n TParentSearchSchema extends AnySearchSchema = {},\n TSearchSchema extends AnySearchSchema = {},\n TFullSearchSchema extends AnySearchSchema = TSearchSchema,\n TParams extends AnyPathParams = AnyPathParams,\n TAllParams extends AnyPathParams = TParams,\n TParentContext extends AnyContext = AnyContext,\n TAllParentContext extends AnyContext = AnyContext,\n TRouteContext extends RouteContext = RouteContext,\n TAllContext extends AnyContext = AnyContext,\n> = BaseRouteOptions<\n TParentRoute,\n TCustomId,\n TPath,\n TLoader,\n TParentSearchSchema,\n TSearchSchema,\n TFullSearchSchema,\n TParams,\n TAllParams,\n TParentContext,\n TAllParentContext,\n TRouteContext,\n TAllContext\n> &\n UpdatableRouteOptions<\n TLoader,\n TSearchSchema,\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n >\n\nexport type ParamsFallback<\n TPath extends string,\n TParams,\n> = unknown extends TParams ? Record<ParsePathParams<TPath>, string> : TParams\n\ntype Prefix<T extends string, U extends string> = U extends `${T}${infer _}`\n ? U\n : never\n\nexport type BaseRouteOptions<\n TParentRoute extends AnyRoute = AnyRoute,\n TCustomId extends string = string,\n TPath extends string = string,\n TLoader = unknown,\n TParentSearchSchema extends AnySearchSchema = {},\n TSearchSchema extends AnySearchSchema = {},\n TFullSearchSchema extends AnySearchSchema = TSearchSchema,\n TParams extends AnyPathParams = {},\n TAllParams = ParamsFallback<TPath, TParams>,\n TParentContext extends AnyContext = AnyContext,\n TAllParentContext extends AnyContext = AnyContext,\n TRouteContext extends RouteContext = RouteContext,\n TAllContext extends AnyContext = AnyContext,\n> = RoutePathOptions<TCustomId, TPath> & {\n layoutLimit?: string\n getParentRoute: () => TParentRoute\n validateSearch?: SearchSchemaValidator<TSearchSchema>\n loader?: LoaderFn<\n TLoader,\n TSearchSchema,\n TFullSearchSchema,\n TAllParams,\n NoInfer<TRouteContext>,\n TAllContext\n >\n} & ([TLoader] extends [never]\n ? {\n loader: 'Loaders must return a type other than never. If you are throwing a redirect() and not returning anything, return a redirect() instead.'\n }\n : {}) &\n (\n | {\n // Both or none\n parseParams?: (\n rawParams: IsAny<TPath, any, Record<ParsePathParams<TPath>, string>>,\n ) => TParams extends Record<ParsePathParams<TPath>, any>\n ? TParams\n : 'parseParams must return an object'\n stringifyParams?: (\n params: NoInfer<ParamsFallback<TPath, TParams>>,\n ) => Record<ParsePathParams<TPath>, string>\n }\n | {\n stringifyParams?: never\n parseParams?: never\n }\n ) &\n (keyof PickRequired<RouteContext> extends never\n ? {\n getContext?: GetContextFn<\n TParentRoute,\n TAllParams,\n TFullSearchSchema,\n TParentContext,\n TAllParentContext,\n TRouteContext\n >\n }\n : {\n getContext: GetContextFn<\n TParentRoute,\n TAllParams,\n TFullSearchSchema,\n TParentContext,\n TAllParentContext,\n TRouteContext\n >\n })\n\ntype GetContextFn<\n TParentRoute,\n TAllParams,\n TFullSearchSchema,\n TParentContext,\n TAllParentContext,\n TRouteContext,\n> = (\n opts: {\n params: TAllParams\n search: TFullSearchSchema\n } & (TParentRoute extends undefined\n ? {\n context?: TAllParentContext\n parentContext?: TParentContext\n }\n : {\n context: TAllParentContext\n parentContext: TParentContext\n }),\n) => TRouteContext\n\nexport type UpdatableRouteOptions<\n TLoader,\n TSearchSchema extends AnySearchSchema,\n TFullSearchSchema extends AnySearchSchema,\n TAllParams extends AnyPathParams,\n TRouteContext extends AnyContext,\n TAllContext extends AnyContext,\n> = MetaOptions & {\n key?: null | false | GetKeyFn<TFullSearchSchema, TAllParams>\n // If true, this route will be matched as case-sensitive\n caseSensitive?: boolean\n // If true, this route will be forcefully wrapped in a suspense boundary\n wrapInSuspense?: boolean\n // The content to be rendered when the route is matched. If no component is provided, defaults to `<Outlet />`\n component?: RegisteredRouteComponent<\n TLoader,\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n >\n // The content to be rendered when the route encounters an error\n errorComponent?: RegisteredErrorRouteComponent<\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n > //\n // If supported by your framework, the content to be rendered as the fallback content until the route is ready to render\n pendingComponent?: RegisteredPendingRouteComponent<\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n >\n // Filter functions that can manipulate search params *before* they are passed to links and navigate\n // calls that match this route.\n preSearchFilters?: SearchFilter<TFullSearchSchema>[]\n // Filter functions that can manipulate search params *after* they are passed to links and navigate\n // calls that match this route.\n postSearchFilters?: SearchFilter<TFullSearchSchema>[]\n // If set, preload matches of this route will be considered fresh for this many milliseconds.\n preloadMaxAge?: number\n // If set, a match of this route will be considered fresh for this many milliseconds.\n maxAge?: number\n // If set, a match of this route that becomes inactive (or unused) will be garbage collected after this many milliseconds\n gcMaxAge?: number\n // This async function is called before a route is loaded.\n // If an error is thrown here, the route's loader will not be called.\n // If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onError` function.\n // If thrown during a preload event, the error will be logged to the console.\n beforeLoad?: (\n opts: LoaderContext<\n TSearchSchema,\n TFullSearchSchema,\n TAllParams,\n NoInfer<TRouteContext>,\n TAllContext\n >,\n ) => Promise<void> | void\n onError?: (err: any) => void\n // These functions are called as route matches are loaded, stick around and leave the active\n // matches\n onEnter?: (match: AnyRouteMatch) => void\n onTransition?: (match: AnyRouteMatch) => void\n onLeave?: (match: AnyRouteMatch) => void\n}\n\nexport type ParseParamsOption<TPath extends string, TParams> = ParseParamsFn<\n TPath,\n TParams\n>\n\nexport type ParseParamsFn<TPath extends string, TParams> = (\n rawParams: IsAny<TPath, any, Record<ParsePathParams<TPath>, string>>,\n) => TParams extends Record<ParsePathParams<TPath>, any>\n ? TParams\n : 'parseParams must return an object'\n\nexport type ParseParamsObj<TPath extends string, TParams> = {\n parse?: ParseParamsFn<TPath, TParams>\n}\n\n// The parse type here allows a zod schema to be passed directly to the validator\nexport type SearchSchemaValidator<TReturn> =\n | SearchSchemaValidatorObj<TReturn>\n | SearchSchemaValidatorFn<TReturn>\n\nexport type SearchSchemaValidatorObj<TReturn> = {\n parse?: SearchSchemaValidatorFn<TReturn>\n}\n\nexport type SearchSchemaValidatorFn<TReturn> = (\n searchObj: Record<string, unknown>,\n) => TReturn\n\nexport type DefinedPathParamWarning =\n 'Path params cannot be redefined by child routes!'\n\nexport type ParentParams<TParentParams> = AnyPathParams extends TParentParams\n ? {}\n : {\n [Key in keyof TParentParams]?: DefinedPathParamWarning\n }\n\nexport type LoaderFn<\n TLoader = unknown,\n TSearchSchema extends AnySearchSchema = {},\n TFullSearchSchema extends AnySearchSchema = {},\n TAllParams = {},\n TContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> = (\n match: LoaderContext<\n TSearchSchema,\n TFullSearchSchema,\n TAllParams,\n TContext,\n TAllContext\n > & {\n parentMatchPromise?: Promise<void>\n },\n) => Promise<TLoader> | TLoader\n\nexport type GetKeyFn<\n TFullSearchSchema extends AnySearchSchema = {},\n TAllParams = {},\n> = (loaderContext: { params: TAllParams; search: TFullSearchSchema }) => any\n\nexport interface LoaderContext<\n TSearchSchema extends AnySearchSchema = {},\n TFullSearchSchema extends AnySearchSchema = {},\n TAllParams = {},\n TContext extends AnyContext = AnyContext,\n TAllContext extends AnyContext = AnyContext,\n> {\n params: TAllParams\n routeSearch: TSearchSchema\n search: TFullSearchSchema\n abortController: AbortController\n preload: boolean\n routeContext: TContext\n context: TAllContext\n}\n\nexport type UnloaderFn<TPath extends string> = (\n routeMatch: RouteMatch<any, Route>,\n) => void\n\nexport type SearchFilter<T, U = T> = (prev: T) => U\n\nexport type ResolveId<\n TParentRoute,\n TCustomId extends string,\n TPath extends string,\n> = TParentRoute extends { id: infer TParentId extends string }\n ? RoutePrefix<TParentId, string extends TCustomId ? TPath : TCustomId>\n : RootRouteId\n\nexport type InferFullSearchSchema<TRoute> = TRoute extends {\n isRoot: true\n types: {\n searchSchema: infer TSearchSchema\n }\n}\n ? TSearchSchema\n : TRoute extends {\n types: {\n fullSearchSchema: infer TFullSearchSchema\n }\n }\n ? TFullSearchSchema\n : {}\n\nexport type ResolveFullSearchSchema<TParentRoute, TSearchSchema> =\n InferFullSearchSchema<TParentRoute> & TSearchSchema\n\nexport interface AnyRoute\n extends Route<\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any\n > {}\n\nexport type MergeParamsFromParent<T, U> = IsAny<T, U, T & U>\n\nexport type UseLoaderResult<T> = T\n// T extends Record<PropertyKey, infer U>\n// ? {\n// [K in keyof T]: UseLoaderResultPromise<T[K]>\n// }\n// : UseLoaderResultPromise<T>\n\n// export type UseLoaderResultPromise<T> = T extends Promise<infer U>\n// ? StreamedPromise<U>\n// : T\n\nexport type StreamedPromise<T> = {\n promise: Promise<T>\n status: 'resolved' | 'pending'\n data: T\n resolve: (value: T) => void\n}\n\nexport type RouteConstraints = {\n TParentRoute: AnyRoute\n TPath: string\n TFullPath: string\n TCustomId: string\n TId: string\n TSearchSchema: AnySearchSchema\n TFullSearchSchema: AnySearchSchema\n TParams: Record<string, any>\n TAllParams: Record<string, any>\n TParentContext: AnyContext\n TAllParentContext: AnyContext\n TRouteContext: RouteContext\n TAllContext: AnyContext\n TRouterContext: AnyContext\n TChildren: unknown\n TRouteTree: AnyRoute\n}\n\nexport class Route<\n TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute,\n TPath extends RouteConstraints['TPath'] = '/',\n TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<\n TParentRoute,\n TPath\n >,\n TCustomId extends RouteConstraints['TCustomId'] = string,\n TId extends RouteConstraints['TId'] = ResolveId<\n TParentRoute,\n TCustomId,\n TPath\n >,\n TLoader = unknown,\n TSearchSchema extends RouteConstraints['TSearchSchema'] = {},\n TFullSearchSchema extends RouteConstraints['TFullSearchSchema'] = ResolveFullSearchSchema<\n TParentRoute,\n TSearchSchema\n >,\n TParams extends RouteConstraints['TParams'] = Record<\n ParsePathParams<TPath>,\n string\n >,\n TAllParams extends RouteConstraints['TAllParams'] = MergeParamsFromParent<\n TParentRoute['types']['allParams'],\n TParams\n >,\n TParentContext extends RouteConstraints['TParentContext'] = TParentRoute['types']['routeContext'],\n TAllParentContext extends RouteConstraints['TAllParentContext'] = TParentRoute['types']['context'],\n TRouteContext extends RouteConstraints['TRouteContext'] = RouteContext,\n TAllContext extends RouteConstraints['TAllContext'] = MergeParamsFromParent<\n TParentRoute['types']['context'],\n TRouteContext\n >,\n TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,\n TChildren extends RouteConstraints['TChildren'] = unknown,\n TRouteTree extends RouteConstraints['TRouteTree'] = AnyRoute,\n> {\n types!: {\n parentRoute: TParentRoute\n path: TPath\n to: TrimPathRight<TFullPath>\n fullPath: TFullPath\n customId: TCustomId\n id: TId\n loader: TLoader\n searchSchema: TSearchSchema\n fullSearchSchema: TFullSearchSchema\n params: TParams\n allParams: TAllParams\n parentContext: TParentContext\n allParentContext: TAllParentContext\n routeContext: TRouteContext\n context: TAllContext\n children: TChildren\n routeTree: TRouteTree\n routerContext: TRouterContext\n }\n isRoot: TParentRoute extends Route<any> ? true : false\n options: RouteOptions<\n TParentRoute,\n TCustomId,\n TPath,\n TLoader,\n InferFullSearchSchema<TParentRoute>,\n TSearchSchema,\n TFullSearchSchema,\n TParams,\n TAllParams,\n TParentContext,\n TAllParentContext,\n TRouteContext,\n TAllContext\n > &\n UpdatableRouteOptions<\n TLoader,\n TSearchSchema,\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n >\n\n // Set up in this.init()\n parentRoute!: TParentRoute\n id!: TId\n // customId!: TCustomId\n path!: TPath\n fullPath!: TFullPath\n to!: TrimPathRight<TFullPath>\n\n // Optional\n children?: TChildren\n originalIndex?: number\n router?: AnyRouter\n rank!: number\n\n constructor(\n options: RouteOptions<\n TParentRoute,\n TCustomId,\n TPath,\n TLoader,\n InferFullSearchSchema<TParentRoute>,\n TSearchSchema,\n TFullSearchSchema,\n TParams,\n TAllParams,\n TParentContext,\n TAllParentContext,\n TRouteContext,\n TAllContext\n > &\n UpdatableRouteOptions<\n TLoader,\n TSearchSchema,\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n >,\n ) {\n this.options = (options as any) || {}\n this.isRoot = !options?.getParentRoute as any\n Route.__onInit(this)\n }\n\n init = (opts: { originalIndex: number; router: AnyRouter }) => {\n this.originalIndex = opts.originalIndex\n this.router = opts.router\n\n const options = this.options as RouteOptions<\n TParentRoute,\n TCustomId,\n TPath,\n InferFullSearchSchema<TParentRoute>,\n TSearchSchema,\n TParams\n > &\n RoutePathOptionsIntersection<TCustomId, TPath>\n\n const isRoot = !options?.path && !options?.id\n\n this.parentRoute = this.options?.getParentRoute?.()\n\n if (isRoot) {\n this.path = rootRouteId as TPath\n } else {\n invariant(\n this.parentRoute,\n `Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a Route instance.`,\n )\n }\n\n let path: undefined | string = isRoot ? rootRouteId : options.path\n\n // If the path is anything other than an index path, trim it up\n if (path && path !== '/') {\n path = trimPath(path)\n }\n\n const customId = options?.id || path\n\n // Strip the parentId prefix from the first level of children\n let id = isRoot\n ? rootRouteId\n : joinPaths([\n (this.parentRoute.id as any) === rootRouteId\n ? ''\n : this.parentRoute.id,\n customId,\n ])\n\n if (path === rootRouteId) {\n path = '/'\n }\n\n if (id !== rootRouteId) {\n id = joinPaths(['/', id])\n }\n\n const fullPath =\n id === rootRouteId ? '/' : joinPaths([this.parentRoute.fullPath, path])\n\n this.path = path as TPath\n this.id = id as TId\n // this.customId = customId as TCustomId\n this.fullPath = fullPath as TFullPath\n this.to = fullPath as TrimPathRight<TFullPath>\n }\n\n addChildren = <TNewChildren extends AnyRoute[]>(\n children: TNewChildren,\n ): Route<\n TParentRoute,\n TPath,\n TFullPath,\n TCustomId,\n TId,\n TLoader,\n TSearchSchema,\n TFullSearchSchema,\n TParams,\n TAllParams,\n TParentContext,\n TAllParentContext,\n TRouteContext,\n TAllContext,\n TRouterContext,\n TNewChildren,\n TRouteTree\n > => {\n this.children = children as any\n return this as any\n }\n\n update = (\n options: UpdatableRouteOptions<\n TLoader,\n TSearchSchema,\n TFullSearchSchema,\n TAllParams,\n TRouteContext,\n TAllContext\n >,\n ) => {\n Object.assign(this.options, options)\n return this\n }\n\n static __onInit = (route: any) => {\n // This is a dummy static method that should get\n // replaced by a framework specific implementation if necessary\n }\n}\n\nexport type AnyRootRoute = RootRoute<any, any, any, any>\n\nexport class RouterContext<TRouterContext extends {}> {\n constructor() {}\n\n createRootRoute = <\n TLoader = unknown,\n TSearchSchema extends AnySearchSchema = {},\n TRouteContext extends RouteContext = RouteContext,\n >(\n options?: Omit<\n RouteOptions<\n AnyRoute,\n RootRouteId,\n '',\n TLoader,\n TSearchSchema,\n TSearchSchema,\n TSearchSchema,\n {},\n {},\n TRouterContext,\n TRouterContext,\n TRouteContext,\n MergeParamsFromParent<TRouterContext, TRouteContext>\n >,\n | 'path'\n | 'id'\n | 'getParentRoute'\n | 'caseSensitive'\n | 'parseParams'\n | 'stringifyParams'\n >,\n ): RootRoute<TLoader, TSearchSchema, TRouteContext, TRouterContext> => {\n return new RootRoute(options) as any\n }\n}\n\nexport class RootRoute<\n TLoader = unknown,\n TSearchSchema extends AnySearchSchema = {},\n TRouteContext extends RouteContext = RouteContext,\n TRouterContext extends {} = {},\n> extends Route<\n any,\n '/',\n '/',\n string,\n RootRouteId,\n TLoader,\n TSearchSchema,\n TSearchSchema,\n {},\n {},\n TRouterContext,\n TRouterContext,\n TRouteContext,\n MergeParamsFromParent<TRouterContext, TRouteContext>,\n TRouterContext,\n any,\n any\n> {\n constructor(\n options?: Omit<\n RouteOptions<\n AnyRoute,\n RootRouteId,\n '',\n TLoader,\n TSearchSchema,\n TSearchSchema,\n TSearchSchema,\n {},\n {},\n TRouterContext,\n TRouterContext,\n TRouteContext,\n MergeParamsFromParent<TRouterContext, TRouteContext>\n >,\n | 'path'\n | 'id'\n | 'getParentRoute'\n | 'caseSensitive'\n | 'parseParams'\n | 'stringifyParams'\n >,\n ) {\n super(options as any)\n }\n}\n\nexport type ResolveFullPath<\n TParentRoute extends AnyRoute,\n TPath extends string,\n TPrefixed = RoutePrefix<TParentRoute['fullPath'], TPath>,\n> = TPrefixed extends RootRouteId ? '/' : TPrefixed\n\ntype RoutePrefix<\n TPrefix extends string,\n TPath extends string,\n> = string extends TPath\n ? RootRouteId\n : TPath extends string\n ? TPrefix extends RootRouteId\n ? TPath extends '/'\n ? '/'\n : `/${TrimPath<TPath>}`\n : `${TPrefix}/${TPath}` extends '/'\n ? '/'\n : `/${TrimPathLeft<`${TrimPathRight<TPrefix>}/${TrimPath<TPath>}`>}`\n : never\n\nexport type TrimPath<T extends string> = '' extends T\n ? ''\n : TrimPathRight<TrimPathLeft<T>>\n\nexport type TrimPathLeft<T extends string> =\n T extends `${RootRouteId}/${infer U}`\n ? TrimPathLeft<U>\n : T extends `/${infer U}`\n ? TrimPathLeft<U>\n : T\nexport type TrimPathRight<T extends string> = T extends '/'\n ? '/'\n : T extends `${infer U}/`\n ? TrimPathRight<U>\n : T\n"],"names":["rootRouteId","Route","constructor","options","isRoot","getParentRoute","__onInit","init","opts","originalIndex","router","path","id","parentRoute","invariant","trimPath","customId","joinPaths","fullPath","to","addChildren","children","update","Object","assign","route","RouterContext","createRootRoute","RootRoute"],"mappings":"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"}
|
package/build/cjs/router.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
14
|
|
|
15
|
-
var
|
|
15
|
+
var store = require('@tanstack/store');
|
|
16
16
|
var invariant = require('tiny-invariant');
|
|
17
17
|
var path = require('./path.js');
|
|
18
18
|
var searchParams = require('./searchParams.js');
|
|
@@ -26,6 +26,8 @@ var invariant__default = /*#__PURE__*/_interopDefaultLegacy(invariant);
|
|
|
26
26
|
//
|
|
27
27
|
|
|
28
28
|
const componentTypes = ['component', 'errorComponent', 'pendingComponent'];
|
|
29
|
+
const visibilityChangeEvent = 'visibilitychange';
|
|
30
|
+
const focusEvent = 'focus';
|
|
29
31
|
class Router {
|
|
30
32
|
#unsubHistory;
|
|
31
33
|
resetNextScroll = false;
|
|
@@ -39,7 +41,7 @@ class Router {
|
|
|
39
41
|
// fetchServerDataFn: options?.fetchServerDataFn ?? defaultFetchServerDataFn,
|
|
40
42
|
};
|
|
41
43
|
|
|
42
|
-
this.__store = new
|
|
44
|
+
this.__store = new store.Store(getInitialRouterState(), {
|
|
43
45
|
onUpdate: () => {
|
|
44
46
|
const prev = this.state;
|
|
45
47
|
const next = this.__store.state;
|
|
@@ -102,12 +104,26 @@ class Router {
|
|
|
102
104
|
this.__store.setState(s => Object.assign(s, getInitialRouterState()));
|
|
103
105
|
};
|
|
104
106
|
mount = () => {
|
|
105
|
-
//
|
|
106
|
-
//
|
|
107
|
+
// addEventListener does not exist in React Native, but window does
|
|
108
|
+
// In the future, we might need to invert control here for more adapters
|
|
109
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
110
|
+
if (typeof window !== 'undefined' && window.addEventListener) {
|
|
111
|
+
window.addEventListener(visibilityChangeEvent, this.#onFocus, false);
|
|
112
|
+
window.addEventListener(focusEvent, this.#onFocus, false);
|
|
113
|
+
}
|
|
107
114
|
this.safeLoad();
|
|
108
|
-
|
|
115
|
+
return () => {
|
|
116
|
+
if (typeof window !== 'undefined' && window.removeEventListener) {
|
|
117
|
+
window.removeEventListener(visibilityChangeEvent, this.#onFocus);
|
|
118
|
+
window.removeEventListener(focusEvent, this.#onFocus);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
#onFocus = () => {
|
|
123
|
+
if (this.options.refetchOnWindowFocus ?? true) {
|
|
124
|
+
this.reload();
|
|
125
|
+
}
|
|
109
126
|
};
|
|
110
|
-
|
|
111
127
|
update = opts => {
|
|
112
128
|
this.options = {
|
|
113
129
|
...this.options,
|
|
@@ -178,7 +194,6 @@ class Router {
|
|
|
178
194
|
};
|
|
179
195
|
|
|
180
196
|
// Cancel any pending matches
|
|
181
|
-
// this.cancelMatches()
|
|
182
197
|
|
|
183
198
|
let pendingMatches;
|
|
184
199
|
this.#emit({
|
|
@@ -221,6 +236,9 @@ class Router {
|
|
|
221
236
|
if (latestPromise = checkLatest()) {
|
|
222
237
|
return latestPromise;
|
|
223
238
|
}
|
|
239
|
+
const exitingMatchIds = this.state.matchIds.filter(id => !this.state.pendingMatchIds.includes(id));
|
|
240
|
+
const enteringMatchIds = this.state.pendingMatchIds.filter(id => !this.state.matchIds.includes(id));
|
|
241
|
+
const stayingMatchIds = this.state.matchIds.filter(id => this.state.pendingMatchIds.includes(id));
|
|
224
242
|
this.__store.setState(s => ({
|
|
225
243
|
...s,
|
|
226
244
|
status: 'idle',
|
|
@@ -228,6 +246,13 @@ class Router {
|
|
|
228
246
|
matchIds: s.pendingMatchIds,
|
|
229
247
|
pendingMatchIds: []
|
|
230
248
|
}));
|
|
249
|
+
[[exitingMatchIds, 'onLeave'], [enteringMatchIds, 'onEnter'], [stayingMatchIds, 'onTransition']].forEach(([matchIds, hook]) => {
|
|
250
|
+
matchIds.forEach(id => {
|
|
251
|
+
const match = this.getRouteMatch(id);
|
|
252
|
+
const route = this.getRoute(match.routeId);
|
|
253
|
+
route.options[hook]?.(match);
|
|
254
|
+
});
|
|
255
|
+
});
|
|
231
256
|
this.#emit({
|
|
232
257
|
type: 'onLoad',
|
|
233
258
|
from: prevLocation,
|
|
@@ -244,6 +269,9 @@ class Router {
|
|
|
244
269
|
}
|
|
245
270
|
});
|
|
246
271
|
this.latestLoadPromise = promise;
|
|
272
|
+
this.latestLoadPromise.then(() => {
|
|
273
|
+
this.cleanMatches();
|
|
274
|
+
});
|
|
247
275
|
return this.latestLoadPromise;
|
|
248
276
|
};
|
|
249
277
|
#mergeMatches = (prevMatchesById, nextMatches) => {
|
|
@@ -288,7 +316,7 @@ class Router {
|
|
|
288
316
|
const now = Date.now();
|
|
289
317
|
const outdatedMatchIds = Object.values(this.state.matchesById).filter(match => {
|
|
290
318
|
const route = this.getRoute(match.routeId);
|
|
291
|
-
return !this.state.matchIds.includes(match.id) && !this.state.pendingMatchIds.includes(match.id) && match.
|
|
319
|
+
return !this.state.matchIds.includes(match.id) && !this.state.pendingMatchIds.includes(match.id) && (match.preloadMaxAge > -1 ? match.updatedAt + match.preloadMaxAge < now : true) && (route.options.gcMaxAge ? match.updatedAt + route.options.gcMaxAge < now : true);
|
|
292
320
|
}).map(d => d.id);
|
|
293
321
|
if (outdatedMatchIds.length) {
|
|
294
322
|
this.__store.setState(s => {
|
|
@@ -376,8 +404,8 @@ class Router {
|
|
|
376
404
|
params: routeParams,
|
|
377
405
|
pathname: path.joinPaths([this.basepath, interpolatedPath]),
|
|
378
406
|
updatedAt: Date.now(),
|
|
379
|
-
|
|
380
|
-
|
|
407
|
+
maxAge: -1,
|
|
408
|
+
preloadMaxAge: -1,
|
|
381
409
|
routeSearch: {},
|
|
382
410
|
search: {},
|
|
383
411
|
status: hasLoaders ? 'pending' : 'success',
|
|
@@ -473,11 +501,10 @@ class Router {
|
|
|
473
501
|
paramsError: match.paramsError,
|
|
474
502
|
searchError: match.searchError,
|
|
475
503
|
params: match.params,
|
|
476
|
-
|
|
504
|
+
preloadMaxAge: 0
|
|
477
505
|
}));
|
|
478
506
|
});
|
|
479
507
|
}
|
|
480
|
-
this.cleanMatches();
|
|
481
508
|
let firstBadMatchIndex;
|
|
482
509
|
|
|
483
510
|
// Check each match middleware to see if the route can be accessed
|
|
@@ -539,8 +566,7 @@ class Router {
|
|
|
539
566
|
matchPromises.push((async () => {
|
|
540
567
|
const parentMatchPromise = matchPromises[index - 1];
|
|
541
568
|
const route = this.getRoute(match.routeId);
|
|
542
|
-
if (match.isFetching || match.status === 'success' && !
|
|
543
|
-
matchId: match.id,
|
|
569
|
+
if (match.isFetching || match.status === 'success' && !isMatchInvalid(match, {
|
|
544
570
|
preload: opts?.preload
|
|
545
571
|
})) {
|
|
546
572
|
return this.getRouteMatch(match.id)?.loadPromise;
|
|
@@ -613,7 +639,6 @@ class Router {
|
|
|
613
639
|
})());
|
|
614
640
|
});
|
|
615
641
|
await Promise.all(matchPromises);
|
|
616
|
-
this.cleanMatches();
|
|
617
642
|
};
|
|
618
643
|
reload = () => {
|
|
619
644
|
return this.navigate({
|
|
@@ -798,7 +823,7 @@ class Router {
|
|
|
798
823
|
dehydrate = () => {
|
|
799
824
|
return {
|
|
800
825
|
state: {
|
|
801
|
-
dehydratedMatches: this.state.matches.map(d => utils.pick(d, ['fetchedAt', 'invalid', '
|
|
826
|
+
dehydratedMatches: this.state.matches.map(d => utils.pick(d, ['fetchedAt', 'invalid', 'preloadMaxAge', 'maxAge', 'id', 'loaderData', 'status', 'updatedAt']))
|
|
802
827
|
}
|
|
803
828
|
};
|
|
804
829
|
};
|
|
@@ -1037,7 +1062,6 @@ class Router {
|
|
|
1037
1062
|
...next.state
|
|
1038
1063
|
});
|
|
1039
1064
|
this.resetNextScroll = location.resetScroll ?? true;
|
|
1040
|
-
console.log('resetScroll', this.resetNextScroll);
|
|
1041
1065
|
return this.latestLoadPromise;
|
|
1042
1066
|
};
|
|
1043
1067
|
getRouteMatch = id => {
|
|
@@ -1062,17 +1086,17 @@ class Router {
|
|
|
1062
1086
|
if (!match) return;
|
|
1063
1087
|
const route = this.getRoute(match.routeId);
|
|
1064
1088
|
const updatedAt = opts?.updatedAt ?? Date.now();
|
|
1065
|
-
const
|
|
1066
|
-
const
|
|
1089
|
+
const preloadMaxAge = opts?.maxAge ?? route.options.preloadMaxAge ?? this.options.defaultPreloadMaxAge ?? 5000;
|
|
1090
|
+
const maxAge = opts?.maxAge ?? route.options.maxAge ?? this.options.defaultMaxAge ?? -1;
|
|
1067
1091
|
this.setRouteMatch(id, s => ({
|
|
1068
1092
|
...s,
|
|
1069
1093
|
error: undefined,
|
|
1070
1094
|
status: 'success',
|
|
1071
1095
|
isFetching: false,
|
|
1072
|
-
updatedAt:
|
|
1096
|
+
updatedAt: updatedAt,
|
|
1073
1097
|
loaderData: utils.functionalUpdate(updater, s.loaderData),
|
|
1074
|
-
|
|
1075
|
-
|
|
1098
|
+
preloadMaxAge,
|
|
1099
|
+
maxAge
|
|
1076
1100
|
}));
|
|
1077
1101
|
};
|
|
1078
1102
|
invalidate = async opts => {
|
|
@@ -1103,20 +1127,6 @@ class Router {
|
|
|
1103
1127
|
return this.reload();
|
|
1104
1128
|
}
|
|
1105
1129
|
};
|
|
1106
|
-
getIsInvalid = opts => {
|
|
1107
|
-
if (!opts?.matchId) {
|
|
1108
|
-
return !!this.state.matches.find(d => this.getIsInvalid({
|
|
1109
|
-
matchId: d.id,
|
|
1110
|
-
preload: opts?.preload
|
|
1111
|
-
}));
|
|
1112
|
-
}
|
|
1113
|
-
const match = this.getRouteMatch(opts?.matchId);
|
|
1114
|
-
if (!match) {
|
|
1115
|
-
return false;
|
|
1116
|
-
}
|
|
1117
|
-
const now = Date.now();
|
|
1118
|
-
return match.invalid || (opts?.preload ? match.preloadInvalidAt : match.invalidAt) < now;
|
|
1119
|
-
};
|
|
1120
1130
|
}
|
|
1121
1131
|
|
|
1122
1132
|
// Detect if we're in the DOM
|
|
@@ -1162,11 +1172,22 @@ function lazyFn(fn, key) {
|
|
|
1162
1172
|
return imported[key || 'default'](...args);
|
|
1163
1173
|
};
|
|
1164
1174
|
}
|
|
1175
|
+
function isMatchInvalid(match, opts) {
|
|
1176
|
+
const now = Date.now();
|
|
1177
|
+
if (match.invalid) {
|
|
1178
|
+
return true;
|
|
1179
|
+
}
|
|
1180
|
+
if (opts?.preload) {
|
|
1181
|
+
return match.preloadMaxAge < 0 ? false : match.updatedAt + match.preloadMaxAge < now;
|
|
1182
|
+
}
|
|
1183
|
+
return match.maxAge < 0 ? false : match.updatedAt + match.maxAge < now;
|
|
1184
|
+
}
|
|
1165
1185
|
|
|
1166
1186
|
exports.PathParamError = PathParamError;
|
|
1167
1187
|
exports.Router = Router;
|
|
1168
1188
|
exports.SearchParamError = SearchParamError;
|
|
1169
1189
|
exports.componentTypes = componentTypes;
|
|
1190
|
+
exports.isMatchInvalid = isMatchInvalid;
|
|
1170
1191
|
exports.isRedirect = isRedirect;
|
|
1171
1192
|
exports.lazyFn = lazyFn;
|
|
1172
1193
|
exports.redirect = redirect;
|