@tanstack/router-core 1.120.4-alpha.19 → 1.120.4

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 (58) hide show
  1. package/dist/cjs/fileRoute.d.cts +2 -6
  2. package/dist/cjs/index.cjs +0 -3
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs/index.d.cts +6 -6
  5. package/dist/cjs/link.cjs.map +1 -1
  6. package/dist/cjs/link.d.cts +1 -18
  7. package/dist/cjs/path.cjs +16 -130
  8. package/dist/cjs/path.cjs.map +1 -1
  9. package/dist/cjs/path.d.cts +0 -17
  10. package/dist/cjs/redirect.cjs +14 -17
  11. package/dist/cjs/redirect.cjs.map +1 -1
  12. package/dist/cjs/redirect.d.cts +7 -13
  13. package/dist/cjs/route.cjs +1 -12
  14. package/dist/cjs/route.cjs.map +1 -1
  15. package/dist/cjs/route.d.cts +14 -15
  16. package/dist/cjs/router.cjs +155 -231
  17. package/dist/cjs/router.cjs.map +1 -1
  18. package/dist/cjs/router.d.cts +3 -46
  19. package/dist/cjs/scroll-restoration.cjs +23 -12
  20. package/dist/cjs/scroll-restoration.cjs.map +1 -1
  21. package/dist/cjs/scroll-restoration.d.cts +1 -1
  22. package/dist/cjs/typePrimitives.d.cts +2 -2
  23. package/dist/cjs/utils.cjs.map +1 -1
  24. package/dist/cjs/utils.d.cts +0 -2
  25. package/dist/esm/fileRoute.d.ts +2 -6
  26. package/dist/esm/index.d.ts +6 -6
  27. package/dist/esm/index.js +2 -5
  28. package/dist/esm/link.d.ts +1 -18
  29. package/dist/esm/link.js.map +1 -1
  30. package/dist/esm/path.d.ts +0 -17
  31. package/dist/esm/path.js +16 -130
  32. package/dist/esm/path.js.map +1 -1
  33. package/dist/esm/redirect.d.ts +7 -13
  34. package/dist/esm/redirect.js +14 -17
  35. package/dist/esm/redirect.js.map +1 -1
  36. package/dist/esm/route.d.ts +14 -15
  37. package/dist/esm/route.js +1 -12
  38. package/dist/esm/route.js.map +1 -1
  39. package/dist/esm/router.d.ts +3 -46
  40. package/dist/esm/router.js +158 -234
  41. package/dist/esm/router.js.map +1 -1
  42. package/dist/esm/scroll-restoration.d.ts +1 -1
  43. package/dist/esm/scroll-restoration.js +23 -12
  44. package/dist/esm/scroll-restoration.js.map +1 -1
  45. package/dist/esm/typePrimitives.d.ts +2 -2
  46. package/dist/esm/utils.d.ts +0 -2
  47. package/dist/esm/utils.js.map +1 -1
  48. package/package.json +2 -2
  49. package/src/fileRoute.ts +1 -90
  50. package/src/index.ts +6 -14
  51. package/src/link.ts +11 -97
  52. package/src/path.ts +16 -181
  53. package/src/redirect.ts +22 -37
  54. package/src/route.ts +35 -104
  55. package/src/router.ts +209 -332
  56. package/src/scroll-restoration.ts +44 -27
  57. package/src/typePrimitives.ts +2 -2
  58. package/src/utils.ts +0 -14
@@ -18,12 +18,22 @@ export type ScrollRestorationOptions = {
18
18
  scrollBehavior?: ScrollToOptions['behavior']
19
19
  }
20
20
 
21
+ function getSafeSessionStorage() {
22
+ try {
23
+ if (
24
+ typeof window !== 'undefined' &&
25
+ typeof window.sessionStorage === 'object'
26
+ ) {
27
+ return window.sessionStorage
28
+ }
29
+ } catch {
30
+ return undefined
31
+ }
32
+ return undefined
33
+ }
34
+
21
35
  export const storageKey = 'tsr-scroll-restoration-v1_3'
22
- let sessionsStorage = false
23
- try {
24
- sessionsStorage =
25
- typeof window !== 'undefined' && typeof window.sessionStorage === 'object'
26
- } catch {}
36
+
27
37
  const throttle = (fn: (...args: Array<any>) => void, wait: number) => {
28
38
  let timeout: any
29
39
  return (...args: Array<any>) => {
@@ -35,28 +45,32 @@ const throttle = (fn: (...args: Array<any>) => void, wait: number) => {
35
45
  }
36
46
  }
37
47
  }
38
- export const scrollRestorationCache: ScrollRestorationCache = sessionsStorage
39
- ? (() => {
40
- const state: ScrollRestorationByKey =
41
- JSON.parse(window.sessionStorage.getItem(storageKey) || 'null') || {}
42
-
43
- return {
44
- state,
45
- // This setter is simply to make sure that we set the sessionStorage right
46
- // after the state is updated. It doesn't necessarily need to be a functional
47
- // update.
48
- set: (updater) => (
49
- (scrollRestorationCache.state =
50
- functionalUpdate(updater, scrollRestorationCache.state) ||
51
- scrollRestorationCache.state),
52
- window.sessionStorage.setItem(
53
- storageKey,
54
- JSON.stringify(scrollRestorationCache.state),
55
- )
56
- ),
57
- }
58
- })()
59
- : (undefined as any)
48
+
49
+ function createScrollRestorationCache(): ScrollRestorationCache | undefined {
50
+ const safeSessionStorage = getSafeSessionStorage()
51
+ if (!safeSessionStorage) {
52
+ return undefined
53
+ }
54
+
55
+ const persistedState = safeSessionStorage.getItem(storageKey)
56
+ let state: ScrollRestorationByKey = persistedState
57
+ ? JSON.parse(persistedState)
58
+ : {}
59
+
60
+ return {
61
+ state,
62
+ // This setter is simply to make sure that we set the sessionStorage right
63
+ // after the state is updated. It doesn't necessarily need to be a functional
64
+ // update.
65
+ set: (updater) => (
66
+ (state = functionalUpdate(updater, state) || state),
67
+ safeSessionStorage.setItem(storageKey, JSON.stringify(state))
68
+ ),
69
+ }
70
+ }
71
+
72
+ export const scrollRestorationCache = createScrollRestorationCache()
73
+
60
74
  /**
61
75
  * The default `getKey` function for `useScrollRestoration`.
62
76
  * It returns the `key` from the location state or the `href` of the location.
@@ -176,6 +190,9 @@ export function restoreScroll(
176
190
  }
177
191
 
178
192
  export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
193
+ if (scrollRestorationCache === undefined) {
194
+ return
195
+ }
179
196
  const shouldScrollRestoration =
180
197
  force ?? router.options.scrollRestoration ?? false
181
198
 
@@ -5,7 +5,7 @@ import type {
5
5
  SearchParamOptions,
6
6
  ToPathOption,
7
7
  } from './link'
8
- import type { RedirectOptions } from './redirect'
8
+ import type { Redirect } from './redirect'
9
9
  import type { RouteIds } from './routeInfo'
10
10
  import type { AnyRouter, RegisteredRouter } from './router'
11
11
  import type { UseParamsResult } from './useParams'
@@ -104,7 +104,7 @@ export type ValidateRedirectOptions<
104
104
  TDefaultFrom extends string = string,
105
105
  > = Constrain<
106
106
  TOptions,
107
- RedirectOptions<
107
+ Redirect<
108
108
  TRouter,
109
109
  InferFrom<TOptions, TDefaultFrom>,
110
110
  InferTo<TOptions>,
package/src/utils.ts CHANGED
@@ -169,20 +169,6 @@ export type ValidateJSON<T> = ((...args: Array<any>) => any) extends T
169
169
  : 'Function is not serializable'
170
170
  : { [K in keyof T]: ValidateJSON<T[K]> }
171
171
 
172
- export type LooseReturnType<T> = T extends (
173
- ...args: Array<any>
174
- ) => infer TReturn
175
- ? TReturn
176
- : never
177
-
178
- export type LooseAsyncReturnType<T> = T extends (
179
- ...args: Array<any>
180
- ) => infer TReturn
181
- ? TReturn extends Promise<infer TReturn>
182
- ? TReturn
183
- : TReturn
184
- : never
185
-
186
172
  export function last<T>(arr: Array<T>) {
187
173
  return arr[arr.length - 1]
188
174
  }