@tanstack/router-core 1.133.20 → 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.
- package/dist/cjs/defer.cjs.map +1 -1
- package/dist/cjs/defer.d.cts +5 -0
- package/dist/cjs/path.cjs.map +1 -1
- package/dist/cjs/path.d.cts +30 -0
- package/dist/cjs/process-route-tree.cjs.map +1 -1
- package/dist/cjs/process-route-tree.d.cts +8 -0
- package/dist/cjs/qss.cjs.map +1 -1
- package/dist/cjs/qss.d.cts +2 -0
- package/dist/cjs/redirect.cjs.map +1 -1
- package/dist/cjs/redirect.d.cts +9 -0
- package/dist/cjs/rewrite.cjs.map +1 -1
- package/dist/cjs/rewrite.d.cts +4 -0
- package/dist/cjs/root.cjs.map +1 -1
- package/dist/cjs/root.d.cts +1 -0
- package/dist/cjs/route.cjs.map +1 -1
- package/dist/cjs/route.d.cts +2 -2
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/cjs/router.d.cts +59 -0
- package/dist/cjs/scroll-restoration.cjs.map +1 -1
- package/dist/cjs/scroll-restoration.d.cts +23 -0
- package/dist/cjs/searchMiddleware.cjs.map +1 -1
- package/dist/cjs/searchMiddleware.d.cts +6 -0
- package/dist/cjs/searchParams.cjs.map +1 -1
- package/dist/cjs/searchParams.d.cts +4 -0
- package/dist/cjs/ssr/serializer/transformer.cjs.map +1 -1
- package/dist/cjs/ssr/serializer/transformer.d.cts +6 -0
- package/dist/esm/defer.d.ts +5 -0
- package/dist/esm/defer.js.map +1 -1
- package/dist/esm/path.d.ts +30 -0
- package/dist/esm/path.js.map +1 -1
- package/dist/esm/process-route-tree.d.ts +8 -0
- package/dist/esm/process-route-tree.js.map +1 -1
- package/dist/esm/qss.d.ts +2 -0
- package/dist/esm/qss.js.map +1 -1
- package/dist/esm/redirect.d.ts +9 -0
- package/dist/esm/redirect.js.map +1 -1
- package/dist/esm/rewrite.d.ts +4 -0
- package/dist/esm/rewrite.js.map +1 -1
- package/dist/esm/root.d.ts +1 -0
- package/dist/esm/root.js.map +1 -1
- package/dist/esm/route.d.ts +2 -2
- package/dist/esm/route.js.map +1 -1
- package/dist/esm/router.d.ts +59 -0
- package/dist/esm/router.js.map +1 -1
- package/dist/esm/scroll-restoration.d.ts +23 -0
- package/dist/esm/scroll-restoration.js.map +1 -1
- package/dist/esm/searchMiddleware.d.ts +6 -0
- package/dist/esm/searchMiddleware.js.map +1 -1
- package/dist/esm/searchParams.d.ts +4 -0
- package/dist/esm/searchParams.js.map +1 -1
- package/dist/esm/ssr/serializer/transformer.d.ts +6 -0
- package/dist/esm/ssr/serializer/transformer.js.map +1 -1
- package/package.json +1 -1
- package/src/defer.ts +5 -0
- package/src/path.ts +30 -0
- package/src/process-route-tree.ts +8 -0
- package/src/qss.ts +2 -0
- package/src/redirect.ts +9 -0
- package/src/rewrite.ts +4 -0
- package/src/root.ts +1 -0
- package/src/route.ts +2 -2
- package/src/router.ts +59 -3
- package/src/scroll-restoration.ts +23 -0
- package/src/searchMiddleware.ts +6 -0
- package/src/searchParams.ts +4 -0
- 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,6 +801,7 @@ export function defaultSerializeError(err: unknown) {
|
|
|
797
801
|
}
|
|
798
802
|
}
|
|
799
803
|
|
|
804
|
+
/** Options for configuring trailing-slash behavior. */
|
|
800
805
|
export const trailingSlashOptions = {
|
|
801
806
|
always: 'always',
|
|
802
807
|
never: 'never',
|
|
@@ -806,6 +811,10 @@ export const trailingSlashOptions = {
|
|
|
806
811
|
export type TrailingSlashOption =
|
|
807
812
|
(typeof trailingSlashOptions)[keyof typeof trailingSlashOptions]
|
|
808
813
|
|
|
814
|
+
/**
|
|
815
|
+
* Compute whether path, href or hash changed between previous and current
|
|
816
|
+
* resolved locations in router state.
|
|
817
|
+
*/
|
|
809
818
|
export function getLocationChangeInfo(routerState: {
|
|
810
819
|
resolvedLocation?: ParsedLocation
|
|
811
820
|
location: ParsedLocation
|
|
@@ -842,6 +851,15 @@ export type CreateRouterFn = <
|
|
|
842
851
|
TDehydrated
|
|
843
852
|
>
|
|
844
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
|
+
*/
|
|
845
863
|
export class RouterCore<
|
|
846
864
|
in out TRouteTree extends AnyRoute,
|
|
847
865
|
in out TTrailingSlashOption extends TrailingSlashOption,
|
|
@@ -1096,6 +1114,12 @@ export class RouterCore<
|
|
|
1096
1114
|
}
|
|
1097
1115
|
}
|
|
1098
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
|
+
*/
|
|
1099
1123
|
subscribe: SubscribeFn = (eventType, fn) => {
|
|
1100
1124
|
const listener: RouterListener<any> = {
|
|
1101
1125
|
eventType,
|
|
@@ -1117,6 +1141,10 @@ export class RouterCore<
|
|
|
1117
1141
|
})
|
|
1118
1142
|
}
|
|
1119
1143
|
|
|
1144
|
+
/**
|
|
1145
|
+
* Parse a HistoryLocation into a strongly-typed ParsedLocation using the
|
|
1146
|
+
* current router options, rewrite rules and search parser/stringifier.
|
|
1147
|
+
*/
|
|
1120
1148
|
parseLocation: ParseLocationFn<TRouteTree> = (
|
|
1121
1149
|
locationToParse,
|
|
1122
1150
|
previousLocation,
|
|
@@ -1172,6 +1200,7 @@ export class RouterCore<
|
|
|
1172
1200
|
return location
|
|
1173
1201
|
}
|
|
1174
1202
|
|
|
1203
|
+
/** Resolve a path against the router basepath and trailing-slash policy. */
|
|
1175
1204
|
resolvePathWithBase = (from: string, path: string) => {
|
|
1176
1205
|
const resolvedPath = resolvePath({
|
|
1177
1206
|
base: from,
|
|
@@ -1538,6 +1567,13 @@ export class RouterCore<
|
|
|
1538
1567
|
})
|
|
1539
1568
|
}
|
|
1540
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
|
+
*/
|
|
1541
1577
|
buildLocation: BuildLocationFn = (opts) => {
|
|
1542
1578
|
const build = (
|
|
1543
1579
|
dest: BuildNextOptions & {
|
|
@@ -1785,6 +1821,10 @@ export class RouterCore<
|
|
|
1785
1821
|
|
|
1786
1822
|
commitLocationPromise: undefined | ControlledPromise<void>
|
|
1787
1823
|
|
|
1824
|
+
/**
|
|
1825
|
+
* Commit a previously built location to history (push/replace), optionally
|
|
1826
|
+
* using view transitions and scroll restoration options.
|
|
1827
|
+
*/
|
|
1788
1828
|
commitLocation: CommitLocationFn = ({
|
|
1789
1829
|
viewTransition,
|
|
1790
1830
|
ignoreBlocker,
|
|
@@ -1875,6 +1915,7 @@ export class RouterCore<
|
|
|
1875
1915
|
return this.commitLocationPromise
|
|
1876
1916
|
}
|
|
1877
1917
|
|
|
1918
|
+
/** Convenience helper: build a location from options, then commit it. */
|
|
1878
1919
|
buildAndCommitLocation = ({
|
|
1879
1920
|
replace,
|
|
1880
1921
|
resetScroll,
|
|
@@ -1911,6 +1952,13 @@ export class RouterCore<
|
|
|
1911
1952
|
})
|
|
1912
1953
|
}
|
|
1913
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
|
+
*/
|
|
1914
1962
|
navigate: NavigateFn = ({ to, reloadDocument, href, ...rest }) => {
|
|
1915
1963
|
if (!reloadDocument && href) {
|
|
1916
1964
|
try {
|
|
@@ -2478,8 +2526,10 @@ export class RouterCore<
|
|
|
2478
2526
|
}
|
|
2479
2527
|
}
|
|
2480
2528
|
|
|
2529
|
+
/** Error thrown when search parameter validation fails. */
|
|
2481
2530
|
export class SearchParamError extends Error {}
|
|
2482
2531
|
|
|
2532
|
+
/** Error thrown when path parameter parsing/validation fails. */
|
|
2483
2533
|
export class PathParamError extends Error {}
|
|
2484
2534
|
|
|
2485
2535
|
const normalize = (str: string) =>
|
|
@@ -2488,9 +2538,10 @@ function comparePaths(a: string, b: string) {
|
|
|
2488
2538
|
return normalize(a) === normalize(b)
|
|
2489
2539
|
}
|
|
2490
2540
|
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2541
|
+
/**
|
|
2542
|
+
* Lazily import a module function and forward arguments to it, retaining
|
|
2543
|
+
* parameter and return types for the selected export key.
|
|
2544
|
+
*/
|
|
2494
2545
|
export function lazyFn<
|
|
2495
2546
|
T extends Record<string, (...args: Array<any>) => any>,
|
|
2496
2547
|
TKey extends keyof T = 'default',
|
|
@@ -2503,6 +2554,7 @@ export function lazyFn<
|
|
|
2503
2554
|
}
|
|
2504
2555
|
}
|
|
2505
2556
|
|
|
2557
|
+
/** Create an initial RouterState from a parsed location. */
|
|
2506
2558
|
export function getInitialRouterState(
|
|
2507
2559
|
location: ParsedLocation,
|
|
2508
2560
|
): RouterState<any> {
|
|
@@ -2548,6 +2600,10 @@ function validateSearch(validateSearch: AnyValidator, input: unknown): unknown {
|
|
|
2548
2600
|
return {}
|
|
2549
2601
|
}
|
|
2550
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
|
+
*/
|
|
2551
2607
|
export function getMatchedRoutes<TRouteLike extends RouteLike>({
|
|
2552
2608
|
pathname,
|
|
2553
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.
|
package/src/searchMiddleware.ts
CHANGED
|
@@ -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>>,
|
package/src/searchParams.ts
CHANGED
|
@@ -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> {
|