@tanstack/react-router 1.34.0 → 1.34.2

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/awaited.tsx CHANGED
@@ -10,7 +10,9 @@ export type AwaitOptions<T> = {
10
10
  promise: DeferredPromise<T>
11
11
  }
12
12
 
13
- export function useAwaited<T>({ promise }: AwaitOptions<T>): [T] {
13
+ export function useAwaited<T>({
14
+ promise,
15
+ }: AwaitOptions<T>): [T, DeferredPromise<T>] {
14
16
  const router = useRouter()
15
17
  // const rerender = React.useReducer((x) => x + 1, 0)[1]
16
18
 
@@ -56,20 +58,6 @@ export function useAwaited<T>({ promise }: AwaitOptions<T>): [T] {
56
58
  throw isDehydratedDeferred(promise) ? state.promise : promise
57
59
  }
58
60
 
59
- // If we are the originator of the promise,
60
- // inject the state into the HTML stream
61
- if (!isDehydratedDeferred(promise)) {
62
- router.injectHtml(
63
- `<script class='tsr_deferred_data'>window.__TSR__DEFERRED__${state.uid} = ${JSON.stringify(router.options.transformer.stringify(state))}
64
- if (window.__TSR__ROUTER__) {
65
- let deferred = window.__TSR__ROUTER__.getDeferred('${state.uid}');
66
- if (deferred) deferred.resolve(window.__TSR__DEFERRED__${state.uid});
67
- }
68
- document.querySelectorAll('.tsr_deferred_data').forEach((el) => el.parentElement.removeChild(el));
69
- </script>`,
70
- )
71
- }
72
-
73
61
  if (state.status === 'error') {
74
62
  if (typeof document !== 'undefined') {
75
63
  if (isServerSideError(state.error)) {
@@ -93,7 +81,7 @@ export function useAwaited<T>({ promise }: AwaitOptions<T>): [T] {
93
81
  }
94
82
  }
95
83
 
96
- return [promise.__deferredState.data as any]
84
+ return [promise.__deferredState.data as any, promise]
97
85
  }
98
86
 
99
87
  export function Await<T>(
@@ -115,6 +103,40 @@ function AwaitInner<T>(
115
103
  children: (result: T) => React.ReactNode
116
104
  },
117
105
  ) {
118
- const awaited = useAwaited(props)
119
- return props.children(...awaited) as React.JSX.Element
106
+ const router = useRouter()
107
+ const [data, promise] = useAwaited(props)
108
+ const state = promise.__deferredState
109
+ // If we are the originator of the promise,
110
+ // inject the state into the HTML stream
111
+ return (
112
+ <>
113
+ {!isDehydratedDeferred(promise) ? (
114
+ <ScriptOnce
115
+ children={`window.__TSR__DEFERRED__${state.uid} = ${JSON.stringify(router.options.transformer.stringify(state))}
116
+ if (window.__TSR__ROUTER__) {
117
+ let deferred = window.__TSR__ROUTER__.getDeferred('${state.uid}');
118
+ if (deferred) deferred.resolve(window.__TSR__DEFERRED__${state.uid});
119
+ }
120
+ document.querySelectorAll('.tsr-script-once').forEach((el) => el.parentElement.removeChild(el));`}
121
+ />
122
+ ) : null}
123
+ {props.children(data)}
124
+ </>
125
+ )
126
+ }
127
+
128
+ function ScriptOnce({
129
+ className,
130
+ children,
131
+ ...rest
132
+ }: { children: string } & React.HTMLProps<HTMLScriptElement>) {
133
+ return (
134
+ <script
135
+ {...rest}
136
+ className={`tsr-script-once ${className || ''}`}
137
+ dangerouslySetInnerHTML={{
138
+ __html: children,
139
+ }}
140
+ />
141
+ )
120
142
  }
package/src/router.ts CHANGED
@@ -483,7 +483,6 @@ export class Router<
483
483
  shouldViewTransition?: boolean = undefined
484
484
  latestLoadPromise: Promise<void> = Promise.resolve()
485
485
  subscribers = new Set<RouterListener<RouterEvent>>()
486
- injectedHtml: Array<InjectedHtmlEntry> = []
487
486
  dehydratedData?: TDehydrated
488
487
  viewTransitionPromise?: ControlledPromise<true>
489
488
  manifest?: Manifest
@@ -1526,8 +1525,9 @@ export class Router<
1526
1525
 
1527
1526
  this.__store.setState((s) => ({
1528
1527
  ...s,
1529
- statusCode:
1530
- redirect?.statusCode || notFound
1528
+ statusCode: redirect
1529
+ ? redirect.statusCode
1530
+ : notFound
1531
1531
  ? 404
1532
1532
  : s.matches.some((d) => d.status === 'error')
1533
1533
  ? 500
@@ -2231,9 +2231,10 @@ export class Router<
2231
2231
  return match
2232
2232
  }
2233
2233
 
2234
- injectHtml = async (html: string | (() => Promise<string> | string)) => {
2235
- this.injectedHtml.push(html)
2236
- }
2234
+ /**
2235
+ * @deprecated Injecting HTML directly is no longer supported. Use the new <ScriptOnce /> component instead.
2236
+ */
2237
+ injectHtml = async (html: string | (() => Promise<string> | string)) => {}
2237
2238
 
2238
2239
  // We use a token -> weak map to keep track of deferred promises
2239
2240
  // that are registered on the server and need to be resolved
@@ -2250,55 +2251,6 @@ export class Router<
2250
2251
  return this.registeredDeferreds.get(token)
2251
2252
  }
2252
2253
 
2253
- /**
2254
- * @deprecated Please inject your own html using the `injectHtml` method
2255
- */
2256
- dehydrateData = <T>(key: any, getData: T | (() => Promise<T> | T)) => {
2257
- warning(
2258
- false,
2259
- `The dehydrateData method is deprecated. Please use the injectHtml method to inject your own data.`,
2260
- )
2261
-
2262
- if (typeof document === 'undefined') {
2263
- const strKey = typeof key === 'string' ? key : JSON.stringify(key)
2264
-
2265
- this.injectHtml(async () => {
2266
- const id = `__TSR_DEHYDRATED__${strKey}`
2267
- const data =
2268
- typeof getData === 'function' ? await (getData as any)() : getData
2269
- return `<script id='${id}' suppressHydrationWarning>
2270
- window["__TSR_DEHYDRATED__${escapeJSON(
2271
- strKey,
2272
- )}"] = ${JSON.stringify(this.options.transformer.stringify(data))}
2273
- </script>`
2274
- })
2275
-
2276
- return () => this.hydrateData<T>(key)
2277
- }
2278
-
2279
- return () => undefined
2280
- }
2281
-
2282
- /**
2283
- * @deprecated Please extract your own data from scripts injected using the `injectHtml` method
2284
- */
2285
- hydrateData = <T = unknown>(key: any) => {
2286
- warning(
2287
- false,
2288
- `The hydrateData method is deprecated. Please use the extractHtml method to extract your own data.`,
2289
- )
2290
-
2291
- if (typeof document !== 'undefined') {
2292
- const strKey = typeof key === 'string' ? key : JSON.stringify(key)
2293
-
2294
- return this.options.transformer.parse(
2295
- window[`__TSR_DEHYDRATED__${strKey}` as any] as unknown as string,
2296
- ) as T
2297
- }
2298
-
2299
- return undefined
2300
- }
2301
-
2302
2254
  dehydrate = (): DehydratedRouter => {
2303
2255
  const pickError =
2304
2256
  this.options.errorSerializer?.serialize ?? defaultSerializeError