@tanstack/react-router 1.19.4 → 1.19.7

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/src/redirects.ts CHANGED
@@ -1,10 +1,9 @@
1
+ import { PickAsRequired } from '.'
1
2
  import { NavigateOptions } from './link'
2
3
  import { AnyRoute } from './route'
3
4
  import { RoutePaths } from './routeInfo'
4
5
  import { RegisteredRouter } from './router'
5
6
 
6
- // Detect if we're in the DOM
7
-
8
7
  export type AnyRedirect = Redirect<any, any, any, any, any>
9
8
 
10
9
  export type Redirect<
@@ -14,11 +13,27 @@ export type Redirect<
14
13
  TMaskFrom extends RoutePaths<TRouteTree> = TFrom,
15
14
  TMaskTo extends string = '',
16
15
  > = {
16
+ /**
17
+ * @deprecated Use `statusCode` instead
18
+ **/
17
19
  code?: number
20
+ statusCode?: number
18
21
  throw?: any
19
- href?: string
20
22
  } & NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>
21
23
 
24
+ export type ResolvedRedirect<
25
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
26
+ TFrom extends RoutePaths<TRouteTree> = '/',
27
+ TTo extends string = '',
28
+ TMaskFrom extends RoutePaths<TRouteTree> = TFrom,
29
+ TMaskTo extends string = '',
30
+ > = PickAsRequired<
31
+ Redirect<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
32
+ 'code' | 'statusCode'
33
+ > & {
34
+ href: string
35
+ }
36
+
22
37
  export function redirect<
23
38
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
24
39
  TFrom extends RoutePaths<TRouteTree> = '/',
@@ -29,6 +44,7 @@ export function redirect<
29
44
  opts: Redirect<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
30
45
  ): Redirect<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> {
31
46
  ;(opts as any).isRedirect = true
47
+ opts.statusCode = opts.statusCode || opts.code || 301
32
48
  if (opts.throw ?? true) {
33
49
  throw opts
34
50
  }
package/src/router.ts CHANGED
@@ -65,7 +65,7 @@ import {
65
65
  trimPathRight,
66
66
  } from './path'
67
67
  import invariant from 'tiny-invariant'
68
- import { AnyRedirect, isRedirect } from './redirects'
68
+ import { AnyRedirect, ResolvedRedirect, isRedirect } from './redirects'
69
69
  import { NotFoundError, isNotFound } from './not-found'
70
70
  import { NavigateOptions, ResolveRelativePath, ToOptions } from './link'
71
71
  import { NoInfer } from '@tanstack/react-store'
@@ -168,6 +168,7 @@ export interface RouterState<TRouteTree extends AnyRoute = AnyRoute> {
168
168
  resolvedLocation: ParsedLocation<FullSearchSchema<TRouteTree>>
169
169
  lastUpdated: number
170
170
  statusCode: number
171
+ redirect?: ResolvedRedirect
171
172
  }
172
173
 
173
174
  export type ListenerFn<TEvent extends RouterEvent> = (event: TEvent) => void
@@ -1476,9 +1477,14 @@ export class Router<
1476
1477
  handleError(err)
1477
1478
 
1478
1479
  // If the route is still active, redirect
1479
- if (isActive) {
1480
- this.handleRedirect(err)
1481
- }
1480
+ // TODO: Do we really need this?
1481
+ invariant(
1482
+ false,
1483
+ 'You need to redirect from a background fetch? This is not supported yet. File an issue.',
1484
+ )
1485
+ // if (isActive) {
1486
+ // this.handleRedirect(err)
1487
+ // }
1482
1488
  }
1483
1489
  }
1484
1490
  })()
@@ -1583,7 +1589,7 @@ export class Router<
1583
1589
  })
1584
1590
 
1585
1591
  try {
1586
- let redirected: AnyRedirect
1592
+ let redirect: ResolvedRedirect
1587
1593
  let notFound: NotFoundError
1588
1594
 
1589
1595
  try {
@@ -1595,8 +1601,7 @@ export class Router<
1595
1601
  })
1596
1602
  } catch (err) {
1597
1603
  if (isRedirect(err)) {
1598
- redirected = err
1599
- this.handleRedirect(err)
1604
+ redirect = this.resolveRedirect(err)
1600
1605
  } else if (isNotFound(err)) {
1601
1606
  notFound = err
1602
1607
  this.handleNotFound(pendingMatches, err)
@@ -1635,11 +1640,12 @@ export class Router<
1635
1640
  ...exitingMatches.filter((d) => d.status !== 'error'),
1636
1641
  ],
1637
1642
  statusCode:
1638
- redirected?.code || notFound
1643
+ redirect?.statusCode || notFound
1639
1644
  ? 404
1640
1645
  : s.matches.some((d) => d.status === 'error')
1641
1646
  ? 500
1642
1647
  : 200,
1648
+ redirect,
1643
1649
  }))
1644
1650
  this.cleanCache()
1645
1651
  })
@@ -1682,13 +1688,14 @@ export class Router<
1682
1688
  return this.latestLoadPromise
1683
1689
  }
1684
1690
 
1685
- handleRedirect = (err: AnyRedirect) => {
1686
- if (!err.href) {
1687
- err.href = this.buildLocation(err as any).href
1688
- }
1689
- if (!isServer) {
1690
- this.navigate({ ...(err as any), replace: true })
1691
+ resolveRedirect = (err: AnyRedirect): ResolvedRedirect => {
1692
+ let redirect = err as ResolvedRedirect
1693
+
1694
+ if (!redirect.href) {
1695
+ redirect.href = this.buildLocation(redirect as any).href
1691
1696
  }
1697
+
1698
+ return redirect
1692
1699
  }
1693
1700
 
1694
1701
  cleanCache = () => {
@@ -73,43 +73,3 @@ export function Navigate<
73
73
 
74
74
  return null
75
75
  }
76
-
77
- export type UseLinkPropsOptions<
78
- TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
79
- TFrom extends RoutePaths<TRouteTree> | string = string,
80
- TTo extends string = '',
81
- TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
82
- TMaskTo extends string = '',
83
- > = ActiveLinkOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> &
84
- React.AnchorHTMLAttributes<HTMLAnchorElement>
85
-
86
- export type LinkProps<
87
- TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
88
- TFrom extends RoutePaths<TRouteTree> | string = string,
89
- TTo extends string = '',
90
- TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
91
- TMaskTo extends string = '',
92
- > = ActiveLinkOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> &
93
- Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'children'> & {
94
- // If a function is passed as a child, it will be given the `isActive` boolean to aid in further styling on the element it returns
95
- children?:
96
- | React.ReactNode
97
- | ((state: { isActive: boolean }) => React.ReactNode)
98
- }
99
-
100
- export type ActiveLinkOptions<
101
- TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
102
- TFrom extends RoutePaths<TRouteTree> | string = string,
103
- TTo extends string = '',
104
- TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
105
- TMaskTo extends string = '',
106
- > = LinkOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> & {
107
- // A function that returns additional props for the `active` state of this link. These props override other props passed to the link (`style`'s are merged, `className`'s are concatenated)
108
- activeProps?:
109
- | React.AnchorHTMLAttributes<HTMLAnchorElement>
110
- | (() => React.AnchorHTMLAttributes<HTMLAnchorElement>)
111
- // A function that returns additional props for the `inactive` state of this link. These props override other props passed to the link (`style`'s are merged, `className`'s are concatenated)
112
- inactiveProps?:
113
- | React.AnchorHTMLAttributes<HTMLAnchorElement>
114
- | (() => React.AnchorHTMLAttributes<HTMLAnchorElement>)
115
- }