@tanstack/router-core 1.126.2 → 1.127.0

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 (74) hide show
  1. package/dist/cjs/Matches.cjs.map +1 -1
  2. package/dist/cjs/Matches.d.cts +1 -1
  3. package/dist/cjs/index.d.cts +1 -2
  4. package/dist/cjs/router.cjs +8 -25
  5. package/dist/cjs/router.cjs.map +1 -1
  6. package/dist/cjs/router.d.cts +9 -57
  7. package/dist/cjs/ssr/client.cjs +0 -2
  8. package/dist/cjs/ssr/client.cjs.map +1 -1
  9. package/dist/cjs/ssr/client.d.cts +1 -2
  10. package/dist/cjs/ssr/createRequestHandler.cjs +2 -1
  11. package/dist/cjs/ssr/createRequestHandler.cjs.map +1 -1
  12. package/dist/cjs/ssr/seroval-plugins.cjs +34 -0
  13. package/dist/cjs/ssr/seroval-plugins.cjs.map +1 -0
  14. package/dist/cjs/ssr/seroval-plugins.d.cts +10 -0
  15. package/dist/cjs/ssr/server.cjs +0 -4
  16. package/dist/cjs/ssr/server.cjs.map +1 -1
  17. package/dist/cjs/ssr/server.d.cts +1 -3
  18. package/dist/cjs/ssr/ssr-client.cjs +18 -56
  19. package/dist/cjs/ssr/ssr-client.cjs.map +1 -1
  20. package/dist/cjs/ssr/ssr-client.d.cts +17 -57
  21. package/dist/cjs/ssr/ssr-server.cjs +75 -220
  22. package/dist/cjs/ssr/ssr-server.cjs.map +1 -1
  23. package/dist/cjs/ssr/ssr-server.d.cts +14 -28
  24. package/dist/cjs/ssr/transformStreamWithRouter.cjs +1 -0
  25. package/dist/cjs/ssr/transformStreamWithRouter.cjs.map +1 -1
  26. package/dist/cjs/ssr/tsrScript.cjs +1 -1
  27. package/dist/cjs/ssr/tsrScript.cjs.map +1 -1
  28. package/dist/cjs/ssr/tsrScript.d.cts +0 -1
  29. package/dist/esm/Matches.d.ts +1 -1
  30. package/dist/esm/Matches.js.map +1 -1
  31. package/dist/esm/index.d.ts +1 -2
  32. package/dist/esm/router.d.ts +9 -57
  33. package/dist/esm/router.js +8 -25
  34. package/dist/esm/router.js.map +1 -1
  35. package/dist/esm/ssr/client.d.ts +1 -2
  36. package/dist/esm/ssr/client.js +1 -3
  37. package/dist/esm/ssr/client.js.map +1 -1
  38. package/dist/esm/ssr/createRequestHandler.js +3 -2
  39. package/dist/esm/ssr/createRequestHandler.js.map +1 -1
  40. package/dist/esm/ssr/seroval-plugins.d.ts +10 -0
  41. package/dist/esm/ssr/seroval-plugins.js +34 -0
  42. package/dist/esm/ssr/seroval-plugins.js.map +1 -0
  43. package/dist/esm/ssr/server.d.ts +1 -3
  44. package/dist/esm/ssr/server.js +1 -5
  45. package/dist/esm/ssr/ssr-client.d.ts +17 -57
  46. package/dist/esm/ssr/ssr-client.js +18 -56
  47. package/dist/esm/ssr/ssr-client.js.map +1 -1
  48. package/dist/esm/ssr/ssr-server.d.ts +14 -28
  49. package/dist/esm/ssr/ssr-server.js +76 -221
  50. package/dist/esm/ssr/ssr-server.js.map +1 -1
  51. package/dist/esm/ssr/transformStreamWithRouter.js +1 -0
  52. package/dist/esm/ssr/transformStreamWithRouter.js.map +1 -1
  53. package/dist/esm/ssr/tsrScript.d.ts +0 -1
  54. package/dist/esm/ssr/tsrScript.js +1 -1
  55. package/dist/esm/ssr/tsrScript.js.map +1 -1
  56. package/package.json +3 -1
  57. package/src/Matches.ts +1 -1
  58. package/src/index.ts +0 -18
  59. package/src/router.ts +15 -82
  60. package/src/ssr/client.ts +1 -11
  61. package/src/ssr/createRequestHandler.ts +2 -2
  62. package/src/ssr/seroval-plugins.ts +43 -0
  63. package/src/ssr/server.ts +1 -14
  64. package/src/ssr/ssr-client.ts +35 -128
  65. package/src/ssr/ssr-server.ts +89 -307
  66. package/src/ssr/transformStreamWithRouter.ts +1 -0
  67. package/src/ssr/tsrScript.ts +4 -88
  68. package/dist/cjs/serializer.cjs +0 -146
  69. package/dist/cjs/serializer.cjs.map +0 -1
  70. package/dist/cjs/serializer.d.cts +0 -28
  71. package/dist/esm/serializer.d.ts +0 -28
  72. package/dist/esm/serializer.js +0 -146
  73. package/dist/esm/serializer.js.map +0 -1
  74. package/src/serializer.ts +0 -205
@@ -4,13 +4,11 @@ import { AnyRedirect, ResolvedRedirect } from './redirect.js';
4
4
  import { HistoryLocation, HistoryState, ParsedHistoryState, RouterHistory } from '@tanstack/history';
5
5
  import { Awaitable, ControlledPromise, NoInfer, NonNullableUpdater, PickAsRequired, Updater } from './utils.js';
6
6
  import { ParsedLocation } from './location.js';
7
- import { DeferredPromiseState } from './defer.js';
8
7
  import { AnyContext, AnyRoute, AnyRouteWithContext, MakeRemountDepsOptionsUnion, RouteMask } from './route.js';
9
8
  import { FullSearchSchema, RouteById, RoutePaths, RoutesById, RoutesByPath } from './routeInfo.js';
10
9
  import { AnyRouteMatch, MakeRouteMatch, MakeRouteMatchUnion, MatchRouteOptions } from './Matches.js';
11
10
  import { BuildLocationFn, CommitLocationOptions, NavigateFn } from './RouterProvider.js';
12
11
  import { Manifest } from './manifest.js';
13
- import { TsrSerializer } from './serializer.js';
14
12
  import { AnySchema } from './validators.js';
15
13
  import { NavigateOptions, ResolveRelativePath, ToOptions } from './link.js';
16
14
  import { NotFoundError } from './not-found.js';
@@ -202,7 +200,7 @@ export interface RouterOptions<TRouteTree extends AnyRoute, TTrailingSlashOption
202
200
  * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#dehydrate-method)
203
201
  * @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/external-data-loading#critical-dehydrationhydration)
204
202
  */
205
- dehydrate?: () => TDehydrated;
203
+ dehydrate?: () => Awaitable<TDehydrated>;
206
204
  /**
207
205
  * A function that will be called when the router is hydrated.
208
206
  *
@@ -349,7 +347,7 @@ type NavigationEventInfo = {
349
347
  hrefChanged: boolean;
350
348
  hashChanged: boolean;
351
349
  };
352
- export type RouterEvents = {
350
+ export interface RouterEvents {
353
351
  onBeforeNavigate: {
354
352
  type: 'onBeforeNavigate';
355
353
  } & NavigationEventInfo;
@@ -365,20 +363,17 @@ export type RouterEvents = {
365
363
  onBeforeRouteMount: {
366
364
  type: 'onBeforeRouteMount';
367
365
  } & NavigationEventInfo;
368
- onInjectedHtml: {
369
- type: 'onInjectedHtml';
370
- promise: Promise<string>;
371
- };
372
366
  onRendered: {
373
367
  type: 'onRendered';
374
368
  } & NavigationEventInfo;
375
- };
369
+ }
376
370
  export type RouterEvent = RouterEvents[keyof RouterEvents];
377
371
  export type ListenerFn<TEvent extends RouterEvent> = (event: TEvent) => void;
378
372
  export type RouterListener<TRouterEvent extends RouterEvent> = {
379
373
  eventType: TRouterEvent['type'];
380
374
  fn: ListenerFn<TRouterEvent>;
381
375
  };
376
+ export type SubscribeFn = <TType extends keyof RouterEvents>(eventType: TType, fn: ListenerFn<RouterEvents[TType]>) => () => void;
382
377
  export interface MatchRoutesOpts {
383
378
  preload?: boolean;
384
379
  throwOnError?: boolean;
@@ -392,10 +387,6 @@ export type RouterContextOptions<TRouteTree extends AnyRoute> = AnyContext exten
392
387
  context: InferRouterContext<TRouteTree>;
393
388
  };
394
389
  export type RouterConstructorOptions<TRouteTree extends AnyRoute, TTrailingSlashOption extends TrailingSlashOption, TDefaultStructuralSharingOption extends boolean, TRouterHistory extends RouterHistory, TDehydrated extends Record<string, any>> = Omit<RouterOptions<TRouteTree, TTrailingSlashOption, TDefaultStructuralSharingOption, TRouterHistory, TDehydrated>, 'context'> & RouterContextOptions<TRouteTree>;
395
- export interface RouterErrorSerializer<TSerializedError> {
396
- serialize: (err: unknown) => TSerializedError;
397
- deserialize: (err: TSerializedError) => unknown;
398
- }
399
390
  export type PreloadRouteFn<TRouteTree extends AnyRoute, TTrailingSlashOption extends TrailingSlashOption, TDefaultStructuralSharingOption extends boolean, TRouterHistory extends RouterHistory> = <TFrom extends RoutePaths<TRouteTree> | string = string, TTo extends string | undefined = undefined, TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom, TMaskTo extends string = ''>(opts: NavigateOptions<RouterCore<TRouteTree, TTrailingSlashOption, TDefaultStructuralSharingOption, TRouterHistory>, TFrom, TTo, TMaskFrom, TMaskTo>) => Promise<Array<AnyRouteMatch> | undefined>;
400
391
  export type MatchRouteFn<TRouteTree extends AnyRoute, TTrailingSlashOption extends TrailingSlashOption, TDefaultStructuralSharingOption extends boolean, TRouterHistory extends RouterHistory> = <TFrom extends RoutePaths<TRouteTree> = '/', TTo extends string | undefined = undefined, TResolved = ResolveRelativePath<TFrom, NoInfer<TTo>>>(location: ToOptions<RouterCore<TRouteTree, TTrailingSlashOption, TDefaultStructuralSharingOption, TRouterHistory>, TFrom, TTo>, opts?: MatchRouteOptions) => false | RouteById<TRouteTree, TResolved>['types']['allParams'];
401
392
  export type UpdateFn<TRouteTree extends AnyRoute, TTrailingSlashOption extends TrailingSlashOption, TDefaultStructuralSharingOption extends boolean, TRouterHistory extends RouterHistory, TDehydrated extends Record<string, any>> = (newOptions: RouterConstructorOptions<TRouteTree, TTrailingSlashOption, TDefaultStructuralSharingOption, TRouterHistory, TDehydrated>) => void;
@@ -416,7 +407,6 @@ export type LoadFn = (opts?: {
416
407
  }) => Promise<void>;
417
408
  export type CommitLocationFn = ({ viewTransition, ignoreBlocker, ...next }: ParsedLocation & CommitLocationOptions) => Promise<void>;
418
409
  export type StartTransitionFn = (fn: () => void) => void;
419
- export type SubscribeFn = <TType extends keyof RouterEvents>(eventType: TType, fn: ListenerFn<RouterEvents[TType]>) => () => void;
420
410
  export interface MatchRoutesFn {
421
411
  (pathname: string, locationSearch: AnySchema, opts?: MatchRoutesOpts): Array<AnyRouteMatch>;
422
412
  (next: ParsedLocation, opts?: MatchRoutesOpts): Array<AnyRouteMatch>;
@@ -429,18 +419,15 @@ export type ResolveRedirect = (err: AnyRedirect) => ResolvedRedirect;
429
419
  export type ClearCacheFn<TRouter extends AnyRouter> = (opts?: {
430
420
  filter?: (d: MakeRouteMatchUnion<TRouter>) => boolean;
431
421
  }) => void;
432
- export interface ServerSrr {
422
+ export interface ServerSsr {
433
423
  injectedHtml: Array<InjectedHtmlEntry>;
434
424
  injectHtml: (getHtml: () => string | Promise<string>) => Promise<void>;
435
425
  injectScript: (getScript: () => string | Promise<string>, opts?: {
436
426
  logScript?: boolean;
437
427
  }) => Promise<void>;
438
- streamValue: (key: string, value: any) => void;
439
- streamedKeys: Set<string>;
440
- onMatchSettled: (opts: {
441
- router: AnyRouter;
442
- match: AnyRouteMatch;
443
- }) => any;
428
+ isDehydrated: () => boolean;
429
+ onRenderFinished: (listener: () => void) => void;
430
+ dehydrate: () => Promise<void>;
444
431
  }
445
432
  export type AnyRouterWithContext<TContext> = RouterCore<AnyRouteWithContext<TContext>, any, any, any, any>;
446
433
  export type AnyRouter = RouterCore<any, any, any, any, any>;
@@ -459,25 +446,6 @@ export declare function defaultSerializeError(err: unknown): {
459
446
  } | {
460
447
  data: unknown;
461
448
  };
462
- export interface ExtractedBaseEntry {
463
- dataType: '__beforeLoadContext' | 'loaderData';
464
- type: string;
465
- path: Array<string>;
466
- id: number;
467
- matchIndex: number;
468
- }
469
- export interface ExtractedStream extends ExtractedBaseEntry {
470
- type: 'stream';
471
- streamState: StreamState;
472
- }
473
- export interface ExtractedPromise extends ExtractedBaseEntry {
474
- type: 'promise';
475
- promiseState: DeferredPromiseState<any>;
476
- }
477
- export type ExtractedEntry = ExtractedStream | ExtractedPromise;
478
- export type StreamState = {
479
- promises: Array<ControlledPromise<string | null>>;
480
- };
481
449
  export type TrailingSlashOption = 'always' | 'never' | 'preserve';
482
450
  export declare function getLocationChangeInfo(routerState: {
483
451
  resolvedLocation?: ParsedLocation;
@@ -568,24 +536,8 @@ export declare class RouterCore<in out TRouteTree extends AnyRoute, in out TTrai
568
536
  matchRoute: MatchRouteFn<TRouteTree, TTrailingSlashOption, TDefaultStructuralSharingOption, TRouterHistory>;
569
537
  ssr?: {
570
538
  manifest: Manifest | undefined;
571
- serializer: TsrSerializer;
572
- };
573
- serverSsr?: {
574
- injectedHtml: Array<InjectedHtmlEntry>;
575
- injectHtml: (getHtml: () => string | Promise<string>) => Promise<void>;
576
- injectScript: (getScript: () => string | Promise<string>, opts?: {
577
- logScript?: boolean;
578
- }) => Promise<void>;
579
- streamValue: (key: string, value: any) => void;
580
- streamedKeys: Set<string>;
581
- onMatchSettled: (opts: {
582
- router: AnyRouter;
583
- match: AnyRouteMatch;
584
- }) => any;
585
- };
586
- clientSsr?: {
587
- getStreamedValue: <T>(key: string) => T | undefined;
588
539
  };
540
+ serverSsr?: ServerSsr;
589
541
  _handleNotFound: (matches: Array<AnyRouteMatch>, err: NotFoundError, { updateMatch, }?: {
590
542
  updateMatch?: (id: string, updater: (match: AnyRouteMatch) => AnyRouteMatch) => void;
591
543
  }) => void;
@@ -745,7 +745,7 @@ class RouterCore {
745
745
  triggerOnReady();
746
746
  }
747
747
  const handleRedirectAndNotFound = (match, err) => {
748
- var _a, _b, _c, _d;
748
+ var _a, _b, _c;
749
749
  if (isRedirect(err) || isNotFound(err)) {
750
750
  if (isRedirect(err)) {
751
751
  if (err.redirectHandled) {
@@ -778,10 +778,6 @@ class RouterCore {
778
778
  this._handleNotFound(matches, err, {
779
779
  updateMatch
780
780
  });
781
- (_d = this.serverSsr) == null ? void 0 : _d.onMatchSettled({
782
- router: this,
783
- match: this.getMatch(match.id)
784
- });
785
781
  throw err;
786
782
  }
787
783
  }
@@ -968,7 +964,7 @@ class RouterCore {
968
964
  cause: preload ? "preload" : cause,
969
965
  matches
970
966
  };
971
- const beforeLoadContext = await ((_d = (_c = route.options).beforeLoad) == null ? void 0 : _d.call(_c, beforeLoadFnContext)) ?? {};
967
+ const beforeLoadContext = await ((_d = (_c = route.options).beforeLoad) == null ? void 0 : _d.call(_c, beforeLoadFnContext));
972
968
  if (isRedirect(beforeLoadContext) || isNotFound(beforeLoadContext)) {
973
969
  handleSerialError(index, beforeLoadContext, "BEFORE_LOAD");
974
970
  }
@@ -1003,12 +999,11 @@ class RouterCore {
1003
999
  validResolvedMatches.forEach(({ id: matchId, routeId }, index) => {
1004
1000
  matchPromises.push(
1005
1001
  (async () => {
1006
- var _a2, _b2;
1007
1002
  let loaderShouldRunAsync = false;
1008
1003
  let loaderIsRunningAsync = false;
1009
1004
  const route = this.looseRoutesById[routeId];
1010
1005
  const executeHead = async () => {
1011
- var _a3, _b3, _c2, _d2, _e, _f;
1006
+ var _a2, _b2, _c2, _d2, _e, _f;
1012
1007
  const match = this.getMatch(matchId);
1013
1008
  if (!match) {
1014
1009
  return;
@@ -1019,7 +1014,7 @@ class RouterCore {
1019
1014
  params: match.params,
1020
1015
  loaderData: match.loaderData
1021
1016
  };
1022
- const headFnContent = await ((_b3 = (_a3 = route.options).head) == null ? void 0 : _b3.call(_a3, assetContext));
1017
+ const headFnContent = await ((_b2 = (_a2 = route.options).head) == null ? void 0 : _b2.call(_a2, assetContext));
1023
1018
  const meta = headFnContent == null ? void 0 : headFnContent.meta;
1024
1019
  const links = headFnContent == null ? void 0 : headFnContent.links;
1025
1020
  const headScripts = headFnContent == null ? void 0 : headFnContent.scripts;
@@ -1049,10 +1044,6 @@ class RouterCore {
1049
1044
  ...prev,
1050
1045
  ...head
1051
1046
  }));
1052
- (_a2 = this.serverSsr) == null ? void 0 : _a2.onMatchSettled({
1053
- router: this,
1054
- match: this.getMatch(matchId)
1055
- });
1056
1047
  return this.getMatch(matchId);
1057
1048
  } else {
1058
1049
  await potentialPendingMinPromise();
@@ -1101,7 +1092,7 @@ class RouterCore {
1101
1092
  preload: !!preload && !this.state.matches.find((d) => d.id === matchId)
1102
1093
  }));
1103
1094
  const runLoader = async () => {
1104
- var _a3, _b3, _c2, _d2, _e;
1095
+ var _a2, _b2, _c2, _d2;
1105
1096
  try {
1106
1097
  try {
1107
1098
  if (!this.isServer || this.isServer && this.getMatch(matchId).ssr === true) {
@@ -1111,7 +1102,7 @@ class RouterCore {
1111
1102
  ...prev,
1112
1103
  isFetching: "loader"
1113
1104
  }));
1114
- const loaderData = await ((_b3 = (_a3 = route.options).loader) == null ? void 0 : _b3.call(_a3, getLoaderContext()));
1105
+ const loaderData = await ((_b2 = (_a2 = route.options).loader) == null ? void 0 : _b2.call(_a2, getLoaderContext()));
1115
1106
  handleRedirectAndNotFound(
1116
1107
  this.getMatch(matchId),
1117
1108
  loaderData
@@ -1154,10 +1145,6 @@ class RouterCore {
1154
1145
  ...head
1155
1146
  }));
1156
1147
  }
1157
- (_e = this.serverSsr) == null ? void 0 : _e.onMatchSettled({
1158
- router: this,
1159
- match: this.getMatch(matchId)
1160
- });
1161
1148
  } catch (err) {
1162
1149
  const head = await executeHead();
1163
1150
  updateMatch(matchId, (prev) => ({
@@ -1200,10 +1187,6 @@ class RouterCore {
1200
1187
  ...prev,
1201
1188
  ...head
1202
1189
  }));
1203
- (_b2 = this.serverSsr) == null ? void 0 : _b2.onMatchSettled({
1204
- router: this,
1205
- match: this.getMatch(matchId)
1206
- });
1207
1190
  }
1208
1191
  }
1209
1192
  if (!loaderIsRunningAsync) {
@@ -1475,7 +1458,7 @@ class RouterCore {
1475
1458
  parseSearch: options.parseSearch ?? defaultParseSearch
1476
1459
  });
1477
1460
  if (typeof document !== "undefined") {
1478
- window.__TSR_ROUTER__ = this;
1461
+ self.__TSR_ROUTER__ = this;
1479
1462
  }
1480
1463
  }
1481
1464
  get state() {
@@ -1621,7 +1604,7 @@ class RouterCore {
1621
1604
  error: void 0,
1622
1605
  paramsError: parseErrors[index],
1623
1606
  __routeContext: {},
1624
- __beforeLoadContext: {},
1607
+ __beforeLoadContext: void 0,
1625
1608
  context: {},
1626
1609
  abortController: new AbortController(),
1627
1610
  fetchCount: 0,