@tanstack/router-core 1.133.19 → 1.133.25

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.
Files changed (76) hide show
  1. package/dist/cjs/defer.cjs.map +1 -1
  2. package/dist/cjs/defer.d.cts +5 -0
  3. package/dist/cjs/index.cjs +1 -0
  4. package/dist/cjs/index.cjs.map +1 -1
  5. package/dist/cjs/index.d.cts +1 -1
  6. package/dist/cjs/path.cjs +1 -1
  7. package/dist/cjs/path.cjs.map +1 -1
  8. package/dist/cjs/path.d.cts +30 -0
  9. package/dist/cjs/process-route-tree.cjs.map +1 -1
  10. package/dist/cjs/process-route-tree.d.cts +8 -0
  11. package/dist/cjs/qss.cjs.map +1 -1
  12. package/dist/cjs/qss.d.cts +2 -0
  13. package/dist/cjs/redirect.cjs.map +1 -1
  14. package/dist/cjs/redirect.d.cts +9 -0
  15. package/dist/cjs/rewrite.cjs.map +1 -1
  16. package/dist/cjs/rewrite.d.cts +4 -0
  17. package/dist/cjs/root.cjs.map +1 -1
  18. package/dist/cjs/root.d.cts +1 -0
  19. package/dist/cjs/route.cjs.map +1 -1
  20. package/dist/cjs/route.d.cts +2 -2
  21. package/dist/cjs/router.cjs +6 -0
  22. package/dist/cjs/router.cjs.map +1 -1
  23. package/dist/cjs/router.d.cts +65 -1
  24. package/dist/cjs/scroll-restoration.cjs.map +1 -1
  25. package/dist/cjs/scroll-restoration.d.cts +23 -0
  26. package/dist/cjs/searchMiddleware.cjs.map +1 -1
  27. package/dist/cjs/searchMiddleware.d.cts +6 -0
  28. package/dist/cjs/searchParams.cjs.map +1 -1
  29. package/dist/cjs/searchParams.d.cts +4 -0
  30. package/dist/cjs/ssr/serializer/transformer.cjs.map +1 -1
  31. package/dist/cjs/ssr/serializer/transformer.d.cts +6 -0
  32. package/dist/esm/defer.d.ts +5 -0
  33. package/dist/esm/defer.js.map +1 -1
  34. package/dist/esm/index.d.ts +1 -1
  35. package/dist/esm/index.js +2 -1
  36. package/dist/esm/path.d.ts +30 -0
  37. package/dist/esm/path.js +1 -1
  38. package/dist/esm/path.js.map +1 -1
  39. package/dist/esm/process-route-tree.d.ts +8 -0
  40. package/dist/esm/process-route-tree.js.map +1 -1
  41. package/dist/esm/qss.d.ts +2 -0
  42. package/dist/esm/qss.js.map +1 -1
  43. package/dist/esm/redirect.d.ts +9 -0
  44. package/dist/esm/redirect.js.map +1 -1
  45. package/dist/esm/rewrite.d.ts +4 -0
  46. package/dist/esm/rewrite.js.map +1 -1
  47. package/dist/esm/root.d.ts +1 -0
  48. package/dist/esm/root.js.map +1 -1
  49. package/dist/esm/route.d.ts +2 -2
  50. package/dist/esm/route.js.map +1 -1
  51. package/dist/esm/router.d.ts +65 -1
  52. package/dist/esm/router.js +7 -1
  53. package/dist/esm/router.js.map +1 -1
  54. package/dist/esm/scroll-restoration.d.ts +23 -0
  55. package/dist/esm/scroll-restoration.js.map +1 -1
  56. package/dist/esm/searchMiddleware.d.ts +6 -0
  57. package/dist/esm/searchMiddleware.js.map +1 -1
  58. package/dist/esm/searchParams.d.ts +4 -0
  59. package/dist/esm/searchParams.js.map +1 -1
  60. package/dist/esm/ssr/serializer/transformer.d.ts +6 -0
  61. package/dist/esm/ssr/serializer/transformer.js.map +1 -1
  62. package/package.json +1 -1
  63. package/src/defer.ts +5 -0
  64. package/src/index.ts +2 -0
  65. package/src/path.ts +32 -2
  66. package/src/process-route-tree.ts +8 -0
  67. package/src/qss.ts +2 -0
  68. package/src/redirect.ts +9 -0
  69. package/src/rewrite.ts +4 -0
  70. package/src/root.ts +1 -0
  71. package/src/route.ts +2 -2
  72. package/src/router.ts +67 -4
  73. package/src/scroll-restoration.ts +23 -0
  74. package/src/searchMiddleware.ts +6 -0
  75. package/src/searchParams.ts +4 -0
  76. package/src/ssr/serializer/transformer.ts +6 -0
package/src/router.ts CHANGED
@@ -778,6 +778,10 @@ export interface ViewTransitionOptions {
778
778
  }
779
779
 
780
780
  // TODO where is this used? can we remove this?
781
+ /**
782
+ * Convert an unknown error into a minimal, serializable object.
783
+ * Includes name and message (and stack in development).
784
+ */
781
785
  export function defaultSerializeError(err: unknown) {
782
786
  if (err instanceof Error) {
783
787
  const obj = {
@@ -797,8 +801,20 @@ export function defaultSerializeError(err: unknown) {
797
801
  }
798
802
  }
799
803
 
800
- export type TrailingSlashOption = 'always' | 'never' | 'preserve'
804
+ /** Options for configuring trailing-slash behavior. */
805
+ export const trailingSlashOptions = {
806
+ always: 'always',
807
+ never: 'never',
808
+ preserve: 'preserve',
809
+ } as const
801
810
 
811
+ export type TrailingSlashOption =
812
+ (typeof trailingSlashOptions)[keyof typeof trailingSlashOptions]
813
+
814
+ /**
815
+ * Compute whether path, href or hash changed between previous and current
816
+ * resolved locations in router state.
817
+ */
802
818
  export function getLocationChangeInfo(routerState: {
803
819
  resolvedLocation?: ParsedLocation
804
820
  location: ParsedLocation
@@ -835,6 +851,15 @@ export type CreateRouterFn = <
835
851
  TDehydrated
836
852
  >
837
853
 
854
+ /**
855
+ * Core, framework-agnostic router engine that powers TanStack Router.
856
+ *
857
+ * Provides navigation, matching, loading, preloading, caching and event APIs
858
+ * used by framework adapters (React/Solid). Prefer framework helpers like
859
+ * `createRouter` in app code.
860
+ *
861
+ * @link https://tanstack.com/router/latest/docs/framework/react/api/router/RouterType
862
+ */
838
863
  export class RouterCore<
839
864
  in out TRouteTree extends AnyRoute,
840
865
  in out TTrailingSlashOption extends TrailingSlashOption,
@@ -1089,6 +1114,12 @@ export class RouterCore<
1089
1114
  }
1090
1115
  }
1091
1116
 
1117
+ /**
1118
+ * Subscribe to router lifecycle events like `onBeforeNavigate`, `onLoad`,
1119
+ * `onResolved`, etc. Returns an unsubscribe function.
1120
+ *
1121
+ * @link https://tanstack.com/router/latest/docs/framework/react/api/router/RouterEventsType
1122
+ */
1092
1123
  subscribe: SubscribeFn = (eventType, fn) => {
1093
1124
  const listener: RouterListener<any> = {
1094
1125
  eventType,
@@ -1110,6 +1141,10 @@ export class RouterCore<
1110
1141
  })
1111
1142
  }
1112
1143
 
1144
+ /**
1145
+ * Parse a HistoryLocation into a strongly-typed ParsedLocation using the
1146
+ * current router options, rewrite rules and search parser/stringifier.
1147
+ */
1113
1148
  parseLocation: ParseLocationFn<TRouteTree> = (
1114
1149
  locationToParse,
1115
1150
  previousLocation,
@@ -1165,6 +1200,7 @@ export class RouterCore<
1165
1200
  return location
1166
1201
  }
1167
1202
 
1203
+ /** Resolve a path against the router basepath and trailing-slash policy. */
1168
1204
  resolvePathWithBase = (from: string, path: string) => {
1169
1205
  const resolvedPath = resolvePath({
1170
1206
  base: from,
@@ -1531,6 +1567,13 @@ export class RouterCore<
1531
1567
  })
1532
1568
  }
1533
1569
 
1570
+ /**
1571
+ * Build the next ParsedLocation from navigation options without committing.
1572
+ * Resolves `to`/`from`, params/search/hash/state, applies search validation
1573
+ * and middlewares, and returns a stable, stringified location object.
1574
+ *
1575
+ * @link https://tanstack.com/router/latest/docs/framework/react/api/router/RouterType#buildlocation-method
1576
+ */
1534
1577
  buildLocation: BuildLocationFn = (opts) => {
1535
1578
  const build = (
1536
1579
  dest: BuildNextOptions & {
@@ -1778,6 +1821,10 @@ export class RouterCore<
1778
1821
 
1779
1822
  commitLocationPromise: undefined | ControlledPromise<void>
1780
1823
 
1824
+ /**
1825
+ * Commit a previously built location to history (push/replace), optionally
1826
+ * using view transitions and scroll restoration options.
1827
+ */
1781
1828
  commitLocation: CommitLocationFn = ({
1782
1829
  viewTransition,
1783
1830
  ignoreBlocker,
@@ -1868,6 +1915,7 @@ export class RouterCore<
1868
1915
  return this.commitLocationPromise
1869
1916
  }
1870
1917
 
1918
+ /** Convenience helper: build a location from options, then commit it. */
1871
1919
  buildAndCommitLocation = ({
1872
1920
  replace,
1873
1921
  resetScroll,
@@ -1904,6 +1952,13 @@ export class RouterCore<
1904
1952
  })
1905
1953
  }
1906
1954
 
1955
+ /**
1956
+ * Imperatively navigate using standard `NavigateOptions`. When `reloadDocument`
1957
+ * or an absolute `href` is provided, performs a full document navigation.
1958
+ * Otherwise, builds and commits a client-side location.
1959
+ *
1960
+ * @link https://tanstack.com/router/latest/docs/framework/react/api/router/NavigateOptionsType
1961
+ */
1907
1962
  navigate: NavigateFn = ({ to, reloadDocument, href, ...rest }) => {
1908
1963
  if (!reloadDocument && href) {
1909
1964
  try {
@@ -2471,8 +2526,10 @@ export class RouterCore<
2471
2526
  }
2472
2527
  }
2473
2528
 
2529
+ /** Error thrown when search parameter validation fails. */
2474
2530
  export class SearchParamError extends Error {}
2475
2531
 
2532
+ /** Error thrown when path parameter parsing/validation fails. */
2476
2533
  export class PathParamError extends Error {}
2477
2534
 
2478
2535
  const normalize = (str: string) =>
@@ -2481,9 +2538,10 @@ function comparePaths(a: string, b: string) {
2481
2538
  return normalize(a) === normalize(b)
2482
2539
  }
2483
2540
 
2484
- // A function that takes an import() argument which is a function and returns a new function that will
2485
- // proxy arguments from the caller to the imported function, retaining all type
2486
- // information along the way
2541
+ /**
2542
+ * Lazily import a module function and forward arguments to it, retaining
2543
+ * parameter and return types for the selected export key.
2544
+ */
2487
2545
  export function lazyFn<
2488
2546
  T extends Record<string, (...args: Array<any>) => any>,
2489
2547
  TKey extends keyof T = 'default',
@@ -2496,6 +2554,7 @@ export function lazyFn<
2496
2554
  }
2497
2555
  }
2498
2556
 
2557
+ /** Create an initial RouterState from a parsed location. */
2499
2558
  export function getInitialRouterState(
2500
2559
  location: ParsedLocation,
2501
2560
  ): RouterState<any> {
@@ -2541,6 +2600,10 @@ function validateSearch(validateSearch: AnyValidator, input: unknown): unknown {
2541
2600
  return {}
2542
2601
  }
2543
2602
 
2603
+ /**
2604
+ * Build the matched route chain and extract params for a pathname.
2605
+ * Falls back to the root route if no specific route is found.
2606
+ */
2544
2607
  export function getMatchedRoutes<TRouteLike extends RouteLike>({
2545
2608
  pathname,
2546
2609
  routePathname,
@@ -33,6 +33,8 @@ function getSafeSessionStorage() {
33
33
  return undefined
34
34
  }
35
35
 
36
+ /** SessionStorage key used to persist scroll restoration state. */
37
+ /** SessionStorage key used to store scroll positions across navigations. */
36
38
  export const storageKey = 'tsr-scroll-restoration-v1_3'
37
39
 
38
40
  const throttle = (fn: (...args: Array<any>) => void, wait: number) => {
@@ -70,6 +72,8 @@ function createScrollRestorationCache(): ScrollRestorationCache | null {
70
72
  }
71
73
  }
72
74
 
75
+ /** In-memory handle to the persisted scroll restoration cache. */
76
+ /** In-memory handle to the persisted scroll restoration cache. */
73
77
  export const scrollRestorationCache = createScrollRestorationCache()
74
78
 
75
79
  /**
@@ -79,10 +83,17 @@ export const scrollRestorationCache = createScrollRestorationCache()
79
83
  * The `location.href` is used as a fallback to support the use case where the location state is not available like the initial render.
80
84
  */
81
85
 
86
+ /**
87
+ * Default scroll restoration cache key: location state key or full href.
88
+ */
89
+ /**
90
+ * Default scroll restoration cache key: location state key or full href.
91
+ */
82
92
  export const defaultGetScrollRestorationKey = (location: ParsedLocation) => {
83
93
  return location.state.__TSR_key! || location.href
84
94
  }
85
95
 
96
+ /** Best-effort nth-child CSS selector for a given element. */
86
97
  export function getCssSelector(el: any): string {
87
98
  const path = []
88
99
  let parent: HTMLElement
@@ -101,6 +112,9 @@ let ignoreScroll = false
101
112
  // unless they are passed in as arguments. Why? Because we need to be able to
102
113
  // toString() it into a script tag to execute as early as possible in the browser
103
114
  // during SSR. Additionally, we also call it from within the router lifecycle
115
+ /**
116
+ * Restore scroll positions for window/elements based on cached entries.
117
+ */
104
118
  export function restoreScroll({
105
119
  storageKey,
106
120
  key,
@@ -200,6 +214,7 @@ export function restoreScroll({
200
214
  ignoreScroll = false
201
215
  }
202
216
 
217
+ /** Setup global listeners and hooks to support scroll restoration. */
203
218
  export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
204
219
  if (!scrollRestorationCache && !router.isServer) {
205
220
  return
@@ -357,6 +372,14 @@ export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
357
372
  })
358
373
  }
359
374
 
375
+ /**
376
+ * @private
377
+ * Handles hash-based scrolling after navigation completes.
378
+ * To be used in framework-specific <Transitioner> components during the onResolved event.
379
+ *
380
+ * Provides hash scrolling for programmatic navigation when default browser handling is prevented.
381
+ * @param router The router instance containing current location and state
382
+ */
360
383
  /**
361
384
  * @private
362
385
  * Handles hash-based scrolling after navigation completes.
@@ -13,6 +13,9 @@ import type { IsRequiredParams } from './link'
13
13
  * @returns A search middleware suitable for route `search.middlewares`.
14
14
  * @link https://tanstack.com/router/latest/docs/framework/react/api/router/retainSearchParamsFunction
15
15
  */
16
+ /**
17
+ * Retain specified search params across navigations by merging prior values.
18
+ */
16
19
  export function retainSearchParams<TSearchSchema extends object>(
17
20
  keys: Array<keyof TSearchSchema> | true,
18
21
  ): SearchMiddleware<TSearchSchema> {
@@ -41,6 +44,9 @@ export function retainSearchParams<TSearchSchema extends object>(
41
44
  * @returns A search middleware suitable for route `search.middlewares`.
42
45
  * @link https://tanstack.com/router/latest/docs/framework/react/api/router/stripSearchParamsFunction
43
46
  */
47
+ /**
48
+ * Remove optional/default-valued search params from navigations.
49
+ */
44
50
  export function stripSearchParams<
45
51
  TSearchSchema,
46
52
  TOptionalProps = PickOptional<NoInfer<TSearchSchema>>,
@@ -1,6 +1,8 @@
1
1
  import { decode, encode } from './qss'
2
2
  import type { AnySchema } from './validators'
3
3
 
4
+ /** Default `parseSearch` that strips leading '?' and JSON-parses values. */
5
+ /** Default `parseSearch` that strips leading '?' and JSON-parses values. */
4
6
  export const defaultParseSearch = parseSearchWith(JSON.parse)
5
7
  export const defaultStringifySearch = stringifySearchWith(
6
8
  JSON.stringify,
@@ -17,6 +19,7 @@ export const defaultStringifySearch = stringifySearchWith(
17
19
  * @returns A `parseSearch` function compatible with `Router` options.
18
20
  * @link https://tanstack.com/router/latest/docs/framework/react/guide/custom-search-param-serialization
19
21
  */
22
+ /** Build a parseSearch function using a provided JSON-like parser. */
20
23
  export function parseSearchWith(parser: (str: string) => any) {
21
24
  return (searchStr: string): AnySchema => {
22
25
  if (searchStr[0] === '?') {
@@ -53,6 +56,7 @@ export function parseSearchWith(parser: (str: string) => any) {
53
56
  * @returns A `stringifySearch` function compatible with `Router` options.
54
57
  * @link https://tanstack.com/router/latest/docs/framework/react/guide/custom-search-param-serialization
55
58
  */
59
+ /** Build a stringifySearch function using a provided serializer/parser. */
56
60
  export function stringifySearchWith(
57
61
  stringify: (search: any) => string,
58
62
  parser?: (str: string) => any,
@@ -32,6 +32,10 @@ export type UnionizeSerializationAdaptersInput<
32
32
  TAdapters extends ReadonlyArray<AnySerializationAdapter>,
33
33
  > = TAdapters[number]['~types']['input']
34
34
 
35
+ /**
36
+ * Create a strongly-typed serialization adapter for SSR hydration.
37
+ * Use to register custom types with the router serializer.
38
+ */
35
39
  export function createSerializationAdapter<
36
40
  TInput = unknown,
37
41
  TOutput = unknown,
@@ -154,6 +158,7 @@ export interface SerializationAdapterTypes<
154
158
 
155
159
  export type AnySerializationAdapter = SerializationAdapter<any, any, any>
156
160
 
161
+ /** Create a Seroval plugin for server-side serialization only. */
157
162
  export function makeSsrSerovalPlugin(
158
163
  serializationAdapter: AnySerializationAdapter,
159
164
  options: { didRun: boolean },
@@ -182,6 +187,7 @@ export function makeSsrSerovalPlugin(
182
187
  })
183
188
  }
184
189
 
190
+ /** Create a Seroval plugin for client/server symmetric (de)serialization. */
185
191
  export function makeSerovalPlugin(
186
192
  serializationAdapter: AnySerializationAdapter,
187
193
  ): Plugin<any, SerovalNode> {