@tanstack/router-core 1.157.9 → 1.157.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/router-core",
3
- "version": "1.157.9",
3
+ "version": "1.157.11",
4
4
  "description": "Modern and scalable routing for React applications",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
package/src/redirect.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { SAFE_URL_PROTOCOLS, isDangerousProtocol } from './utils'
2
2
  import type { NavigateOptions } from './link'
3
3
  import type { AnyRouter, RegisteredRouter } from './router'
4
+ import type { ParsedLocation } from './location'
4
5
 
5
6
  export type AnyRedirect = Redirect<any, any, any, any, any>
6
7
 
@@ -14,7 +15,13 @@ export type Redirect<
14
15
  TMaskFrom extends string = TFrom,
15
16
  TMaskTo extends string = '.',
16
17
  > = Response & {
17
- options: NavigateOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>
18
+ options: NavigateOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> & {
19
+ /**
20
+ * @internal
21
+ * A **trusted** built location that can be used to redirect to.
22
+ */
23
+ _builtLocation?: ParsedLocation
24
+ }
18
25
  redirectHandled?: boolean
19
26
  }
20
27
 
@@ -45,6 +52,11 @@ export type RedirectOptions<
45
52
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RedirectType#headers-property)
46
53
  */
47
54
  headers?: HeadersInit
55
+ /**
56
+ * @internal
57
+ * A **trusted** built location that can be used to redirect to.
58
+ */
59
+ _builtLocation?: ParsedLocation
48
60
  } & NavigateOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>
49
61
 
50
62
  export type ResolvedRedirect<
@@ -113,13 +125,21 @@ export function redirect<
113
125
  opts.statusCode = opts.statusCode || opts.code || 307
114
126
 
115
127
  // Block dangerous protocols in redirect href
116
- if (typeof opts.href === 'string' && isDangerousProtocol(opts.href)) {
128
+ if (
129
+ !opts._builtLocation &&
130
+ typeof opts.href === 'string' &&
131
+ isDangerousProtocol(opts.href)
132
+ ) {
117
133
  throw new Error(
118
134
  `Redirect blocked: unsafe protocol in href "${opts.href}". Only ${SAFE_URL_PROTOCOLS.join(', ')} protocols are allowed.`,
119
135
  )
120
136
  }
121
137
 
122
- if (!opts.reloadDocument && typeof opts.href === 'string') {
138
+ if (
139
+ !opts._builtLocation &&
140
+ !opts.reloadDocument &&
141
+ typeof opts.href === 'string'
142
+ ) {
123
143
  try {
124
144
  new URL(opts.href)
125
145
  opts.reloadDocument = true
package/src/router.ts CHANGED
@@ -1821,16 +1821,8 @@ export class RouterCore<
1821
1821
  destMatchResult.routeParams['**']
1822
1822
  : trimPathRight(interpolatedNextTo)
1823
1823
 
1824
- let globalNotFoundRouteId: string | undefined
1825
- if (isGlobalNotFound) {
1826
- if (this.options.notFoundRoute) {
1827
- destRoutes = [...destRoutes, this.options.notFoundRoute]
1828
- } else {
1829
- globalNotFoundRouteId = findGlobalNotFoundRouteId(
1830
- this.options.notFoundMode,
1831
- destRoutes,
1832
- )
1833
- }
1824
+ if (isGlobalNotFound && this.options.notFoundRoute) {
1825
+ destRoutes = [...destRoutes, this.options.notFoundRoute]
1834
1826
  }
1835
1827
 
1836
1828
  // If there are any params, we need to stringify them
@@ -2292,8 +2284,11 @@ export class RouterCore<
2292
2284
  // always uses this.origin when constructing URLs
2293
2285
  if (this.latestLocation.publicHref !== nextLocation.publicHref) {
2294
2286
  const href = this.getParsedLocationHref(nextLocation)
2295
-
2296
- throw redirect({ href })
2287
+ if (nextLocation.external) {
2288
+ throw redirect({ href })
2289
+ } else {
2290
+ throw redirect({ href, _builtLocation: nextLocation })
2291
+ }
2297
2292
  }
2298
2293
  }
2299
2294
 
@@ -2622,8 +2617,9 @@ export class RouterCore<
2622
2617
  resolveRedirect = (redirect: AnyRedirect): AnyRedirect => {
2623
2618
  const locationHeader = redirect.headers.get('Location')
2624
2619
 
2625
- if (!redirect.options.href) {
2626
- const location = this.buildLocation(redirect.options)
2620
+ if (!redirect.options.href || redirect.options._builtLocation) {
2621
+ const location =
2622
+ redirect.options._builtLocation ?? this.buildLocation(redirect.options)
2627
2623
  const href = this.getParsedLocationHref(location)
2628
2624
  redirect.options.href = href
2629
2625
  redirect.headers.set('Location', href)