@tanstack/router-core 1.171.3 → 1.171.5

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 (46) hide show
  1. package/dist/cjs/index.cjs +0 -3
  2. package/dist/cjs/index.d.cts +1 -2
  3. package/dist/cjs/rewrite.cjs +1 -6
  4. package/dist/cjs/rewrite.cjs.map +1 -1
  5. package/dist/cjs/rewrite.d.cts +0 -4
  6. package/dist/cjs/router.cjs +9 -2
  7. package/dist/cjs/router.cjs.map +1 -1
  8. package/dist/cjs/router.d.cts +5 -1
  9. package/dist/cjs/scroll-restoration-inline.cjs +1 -1
  10. package/dist/cjs/scroll-restoration-inline.cjs.map +1 -1
  11. package/dist/cjs/scroll-restoration-inline.d.cts +1 -6
  12. package/dist/cjs/scroll-restoration-script/server.cjs +5 -12
  13. package/dist/cjs/scroll-restoration-script/server.cjs.map +1 -1
  14. package/dist/cjs/scroll-restoration.cjs +117 -111
  15. package/dist/cjs/scroll-restoration.cjs.map +1 -1
  16. package/dist/cjs/scroll-restoration.d.cts +0 -10
  17. package/dist/esm/index.d.ts +1 -2
  18. package/dist/esm/index.js +2 -3
  19. package/dist/esm/rewrite.d.ts +0 -4
  20. package/dist/esm/rewrite.js +1 -6
  21. package/dist/esm/rewrite.js.map +1 -1
  22. package/dist/esm/router.d.ts +5 -1
  23. package/dist/esm/router.js +9 -3
  24. package/dist/esm/router.js.map +1 -1
  25. package/dist/esm/scroll-restoration-inline.d.ts +1 -6
  26. package/dist/esm/scroll-restoration-inline.js +1 -1
  27. package/dist/esm/scroll-restoration-inline.js.map +1 -1
  28. package/dist/esm/scroll-restoration-script/server.js +5 -12
  29. package/dist/esm/scroll-restoration-script/server.js.map +1 -1
  30. package/dist/esm/scroll-restoration.d.ts +0 -10
  31. package/dist/esm/scroll-restoration.js +118 -111
  32. package/dist/esm/scroll-restoration.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/index.ts +0 -3
  35. package/src/rewrite.ts +1 -8
  36. package/src/router.ts +28 -4
  37. package/src/scroll-restoration-inline.ts +23 -51
  38. package/src/scroll-restoration-script/server.ts +5 -26
  39. package/src/scroll-restoration.ts +190 -174
  40. package/dist/cjs/hash-scroll.cjs +0 -20
  41. package/dist/cjs/hash-scroll.cjs.map +0 -1
  42. package/dist/cjs/hash-scroll.d.cts +0 -7
  43. package/dist/esm/hash-scroll.d.ts +0 -7
  44. package/dist/esm/hash-scroll.js +0 -20
  45. package/dist/esm/hash-scroll.js.map +0 -1
  46. package/src/hash-scroll.ts +0 -21
@@ -1 +1 @@
1
- {"version":3,"file":"rewrite.js","names":[],"sources":["../../src/rewrite.ts"],"sourcesContent":["import { joinPaths, trimPath } from './path'\nimport type { LocationRewrite } from './router'\n\n/** Compose multiple rewrite pairs into a single in/out rewrite. */\n/** Compose multiple rewrite pairs into a single in/out rewrite. */\nexport function composeRewrites(rewrites: Array<LocationRewrite>) {\n return {\n input: ({ url }) => {\n for (const rewrite of rewrites) {\n url = executeRewriteInput(rewrite, url)\n }\n return url\n },\n output: ({ url }) => {\n for (let i = rewrites.length - 1; i >= 0; i--) {\n url = executeRewriteOutput(rewrites[i], url)\n }\n return url\n },\n } satisfies LocationRewrite\n}\n\n/** Create a rewrite pair that strips/adds a basepath on input/output. */\n/** Create a rewrite pair that strips/adds a basepath on input/output. */\nexport function rewriteBasepath(opts: {\n basepath: string\n caseSensitive?: boolean\n}) {\n const trimmedBasepath = trimPath(opts.basepath)\n const normalizedBasepath = `/${trimmedBasepath}`\n const normalizedBasepathWithSlash = `${normalizedBasepath}/`\n const checkBasepath = opts.caseSensitive\n ? normalizedBasepath\n : normalizedBasepath.toLowerCase()\n const checkBasepathWithSlash = opts.caseSensitive\n ? normalizedBasepathWithSlash\n : normalizedBasepathWithSlash.toLowerCase()\n\n return {\n input: ({ url }) => {\n const pathname = opts.caseSensitive\n ? url.pathname\n : url.pathname.toLowerCase()\n\n // Handle exact basepath match (e.g., /my-app -> /)\n if (pathname === checkBasepath) {\n url.pathname = '/'\n } else if (pathname.startsWith(checkBasepathWithSlash)) {\n // Handle basepath with trailing content (e.g., /my-app/users -> /users)\n url.pathname = url.pathname.slice(normalizedBasepath.length)\n }\n return url\n },\n output: ({ url }) => {\n url.pathname = joinPaths(['/', trimmedBasepath, url.pathname])\n return url\n },\n } satisfies LocationRewrite\n}\n\n/** Execute a location input rewrite if provided. */\n/** Execute a location input rewrite if provided. */\nexport function executeRewriteInput(\n rewrite: LocationRewrite | undefined,\n url: URL,\n): URL {\n const res = rewrite?.input?.({ url })\n if (res) {\n if (typeof res === 'string') {\n return new URL(res)\n } else if (res instanceof URL) {\n return res\n }\n }\n return url\n}\n\n/** Execute a location output rewrite if provided. */\n/** Execute a location output rewrite if provided. */\nexport function executeRewriteOutput(\n rewrite: LocationRewrite | undefined,\n url: URL,\n): URL {\n const res = rewrite?.output?.({ url })\n if (res) {\n if (typeof res === 'string') {\n return new URL(res)\n } else if (res instanceof URL) {\n return res\n }\n }\n return url\n}\n"],"mappings":";;;;AAKA,SAAgB,gBAAgB,UAAkC;AAChE,QAAO;EACL,QAAQ,EAAE,UAAU;AAClB,QAAK,MAAM,WAAW,SACpB,OAAM,oBAAoB,SAAS,IAAI;AAEzC,UAAO;;EAET,SAAS,EAAE,UAAU;AACnB,QAAK,IAAI,IAAI,SAAS,SAAS,GAAG,KAAK,GAAG,IACxC,OAAM,qBAAqB,SAAS,IAAI,IAAI;AAE9C,UAAO;;EAEV;;;;AAKH,SAAgB,gBAAgB,MAG7B;CACD,MAAM,kBAAkB,SAAS,KAAK,SAAS;CAC/C,MAAM,qBAAqB,IAAI;CAC/B,MAAM,8BAA8B,GAAG,mBAAmB;CAC1D,MAAM,gBAAgB,KAAK,gBACvB,qBACA,mBAAmB,aAAa;CACpC,MAAM,yBAAyB,KAAK,gBAChC,8BACA,4BAA4B,aAAa;AAE7C,QAAO;EACL,QAAQ,EAAE,UAAU;GAClB,MAAM,WAAW,KAAK,gBAClB,IAAI,WACJ,IAAI,SAAS,aAAa;AAG9B,OAAI,aAAa,cACf,KAAI,WAAW;YACN,SAAS,WAAW,uBAAuB,CAEpD,KAAI,WAAW,IAAI,SAAS,MAAM,mBAAmB,OAAO;AAE9D,UAAO;;EAET,SAAS,EAAE,UAAU;AACnB,OAAI,WAAW,UAAU;IAAC;IAAK;IAAiB,IAAI;IAAS,CAAC;AAC9D,UAAO;;EAEV;;;;AAKH,SAAgB,oBACd,SACA,KACK;CACL,MAAM,MAAM,SAAS,QAAQ,EAAE,KAAK,CAAC;AACrC,KAAI;MACE,OAAO,QAAQ,SACjB,QAAO,IAAI,IAAI,IAAI;WACV,eAAe,IACxB,QAAO;;AAGX,QAAO;;;;AAKT,SAAgB,qBACd,SACA,KACK;CACL,MAAM,MAAM,SAAS,SAAS,EAAE,KAAK,CAAC;AACtC,KAAI;MACE,OAAO,QAAQ,SACjB,QAAO,IAAI,IAAI,IAAI;WACV,eAAe,IACxB,QAAO;;AAGX,QAAO"}
1
+ {"version":3,"file":"rewrite.js","names":[],"sources":["../../src/rewrite.ts"],"sourcesContent":["import { joinPaths, trimPath } from './path'\nimport type { LocationRewrite } from './router'\n\n/** Compose multiple rewrite pairs into a single in/out rewrite. */\nexport function composeRewrites(rewrites: Array<LocationRewrite>) {\n return {\n input: ({ url }) => {\n for (const rewrite of rewrites) {\n url = executeRewriteInput(rewrite, url)\n }\n return url\n },\n output: ({ url }) => {\n for (let i = rewrites.length - 1; i >= 0; i--) {\n url = executeRewriteOutput(rewrites[i], url)\n }\n return url\n },\n } satisfies LocationRewrite\n}\n\n/** Create a rewrite pair that strips/adds a basepath on input/output. */\nexport function rewriteBasepath(opts: {\n basepath: string\n caseSensitive?: boolean\n}) {\n const trimmedBasepath = trimPath(opts.basepath)\n const normalizedBasepath = `/${trimmedBasepath}`\n const checkBasepath = opts.caseSensitive\n ? normalizedBasepath\n : normalizedBasepath.toLowerCase()\n const checkBasepathWithSlash = `${checkBasepath}/`\n\n return {\n input: ({ url }) => {\n const pathname = opts.caseSensitive\n ? url.pathname\n : url.pathname.toLowerCase()\n\n // Handle exact basepath match (e.g., /my-app -> /)\n if (pathname === checkBasepath) {\n url.pathname = '/'\n } else if (pathname.startsWith(checkBasepathWithSlash)) {\n // Handle basepath with trailing content (e.g., /my-app/users -> /users)\n url.pathname = url.pathname.slice(normalizedBasepath.length)\n }\n return url\n },\n output: ({ url }) => {\n url.pathname = joinPaths(['/', trimmedBasepath, url.pathname])\n return url\n },\n } satisfies LocationRewrite\n}\n\n/** Execute a location input rewrite if provided. */\nexport function executeRewriteInput(\n rewrite: LocationRewrite | undefined,\n url: URL,\n): URL {\n const res = rewrite?.input?.({ url })\n if (res) {\n if (typeof res === 'string') {\n return new URL(res)\n } else if (res instanceof URL) {\n return res\n }\n }\n return url\n}\n\n/** Execute a location output rewrite if provided. */\nexport function executeRewriteOutput(\n rewrite: LocationRewrite | undefined,\n url: URL,\n): URL {\n const res = rewrite?.output?.({ url })\n if (res) {\n if (typeof res === 'string') {\n return new URL(res)\n } else if (res instanceof URL) {\n return res\n }\n }\n return url\n}\n"],"mappings":";;;AAIA,SAAgB,gBAAgB,UAAkC;AAChE,QAAO;EACL,QAAQ,EAAE,UAAU;AAClB,QAAK,MAAM,WAAW,SACpB,OAAM,oBAAoB,SAAS,IAAI;AAEzC,UAAO;;EAET,SAAS,EAAE,UAAU;AACnB,QAAK,IAAI,IAAI,SAAS,SAAS,GAAG,KAAK,GAAG,IACxC,OAAM,qBAAqB,SAAS,IAAI,IAAI;AAE9C,UAAO;;EAEV;;;AAIH,SAAgB,gBAAgB,MAG7B;CACD,MAAM,kBAAkB,SAAS,KAAK,SAAS;CAC/C,MAAM,qBAAqB,IAAI;CAC/B,MAAM,gBAAgB,KAAK,gBACvB,qBACA,mBAAmB,aAAa;CACpC,MAAM,yBAAyB,GAAG,cAAc;AAEhD,QAAO;EACL,QAAQ,EAAE,UAAU;GAClB,MAAM,WAAW,KAAK,gBAClB,IAAI,WACJ,IAAI,SAAS,aAAa;AAG9B,OAAI,aAAa,cACf,KAAI,WAAW;YACN,SAAS,WAAW,uBAAuB,CAEpD,KAAI,WAAW,IAAI,SAAS,MAAM,mBAAmB,OAAO;AAE9D,UAAO;;EAET,SAAS,EAAE,UAAU;AACnB,OAAI,WAAW,UAAU;IAAC;IAAK;IAAiB,IAAI;IAAS,CAAC;AAC9D,UAAO;;EAEV;;;AAIH,SAAgB,oBACd,SACA,KACK;CACL,MAAM,MAAM,SAAS,QAAQ,EAAE,KAAK,CAAC;AACrC,KAAI;MACE,OAAO,QAAQ,SACjB,QAAO,IAAI,IAAI,IAAI;WACV,eAAe,IACxB,QAAO;;AAGX,QAAO;;;AAIT,SAAgB,qBACd,SACA,KACK;CACL,MAAM,MAAM,SAAS,SAAS,EAAE,KAAK,CAAC;AACtC,KAAI;MACE,OAAO,QAAQ,SACjB,QAAO,IAAI,IAAI,IAAI;WACV,eAAe,IACxB,QAAO;;AAGX,QAAO"}
@@ -3,7 +3,7 @@ import { LRUCache } from './lru-cache.js';
3
3
  import { ProcessRouteTreeResult, ProcessedTree } from './new-process-route-tree.js';
4
4
  import { SearchParser, SearchSerializer } from './searchParams.js';
5
5
  import { AnyRedirect, ResolvedRedirect } from './redirect.js';
6
- import { HistoryLocation, HistoryState, ParsedHistoryState, RouterHistory } from '@tanstack/history';
6
+ import { HistoryAction, HistoryLocation, HistoryState, ParsedHistoryState, RouterHistory } from '@tanstack/history';
7
7
  import { Awaitable, Constrain, ControlledPromise, NoInfer, NonNullableUpdater, PickAsRequired, Updater } from './utils.js';
8
8
  import { ParsedLocation } from './location.js';
9
9
  import { AnyContext, AnyRoute, AnyRouteWithContext, LoaderStaleReloadMode, MakeRemountDepsOptionsUnion, RouteLike, RouteMask } from './route.js';
@@ -483,6 +483,9 @@ export type GetMatchRoutesFn = (pathname: string) => {
483
483
  export type EmitFn = (routerEvent: RouterEvent) => void;
484
484
  export type LoadFn = (opts?: {
485
485
  sync?: boolean;
486
+ action?: {
487
+ type: HistoryAction;
488
+ };
486
489
  }) => Promise<void>;
487
490
  export type CommitLocationFn = ({ viewTransition, ignoreBlocker, ...next }: ParsedLocation & CommitLocationOptions) => Promise<void>;
488
491
  export type StartTransitionFn = (fn: () => void) => void;
@@ -568,6 +571,7 @@ export declare function getLocationChangeInfo(location: ParsedLocation, resolved
568
571
  hrefChanged: boolean;
569
572
  hashChanged: boolean;
570
573
  };
574
+ export declare const locationHistoryActions: WeakMap<ParsedLocation<{}>, HistoryAction>;
571
575
  export type CreateRouterFn = <TRouteTree extends AnyRoute, TTrailingSlashOption extends TrailingSlashOption = 'never', TDefaultStructuralSharingOption extends boolean = false, TRouterHistory extends RouterHistory = RouterHistory, TDehydrated extends Record<string, any> = Record<string, any>>(options: undefined extends number ? 'strictNullChecks must be enabled in tsconfig.json' : RouterConstructorOptions<TRouteTree, TTrailingSlashOption, TDefaultStructuralSharingOption, TRouterHistory, TDehydrated>) => RouterCore<TRouteTree, TTrailingSlashOption, TDefaultStructuralSharingOption, TRouterHistory, TDehydrated>;
572
576
  declare global {
573
577
  var __TSR_CACHE__: {
@@ -49,6 +49,7 @@ function getLocationChangeInfo(location, resolvedLocation) {
49
49
  hashChanged: fromLocation?.hash !== toLocation.hash
50
50
  };
51
51
  }
52
+ const locationHistoryActions = /* @__PURE__ */ new WeakMap();
52
53
  /**
53
54
  * Core, framework-agnostic router engine that powers TanStack Router.
54
55
  *
@@ -374,6 +375,7 @@ var RouterCore = class {
374
375
  return buildWithMatches(opts);
375
376
  };
376
377
  this.commitLocation = async ({ viewTransition, ignoreBlocker, ...next }) => {
378
+ let historyAction;
377
379
  const isSameState = () => {
378
380
  const ignoredProps = [
379
381
  "key",
@@ -422,10 +424,11 @@ var RouterCore = class {
422
424
  }
423
425
  nextHistory.state.__hashScrollIntoViewOptions = hashScrollIntoView ?? this.options.defaultHashScrollIntoView ?? true;
424
426
  this.shouldViewTransition = viewTransition;
425
- this.history[next.replace ? "replace" : "push"](nextHistory.publicHref, nextHistory.state, { ignoreBlocker });
427
+ historyAction = next.replace ? "REPLACE" : "PUSH";
428
+ this.history[historyAction === "REPLACE" ? "replace" : "push"](nextHistory.publicHref, nextHistory.state, { ignoreBlocker });
426
429
  }
427
430
  this.resetNextScroll = next.resetScroll ?? true;
428
- if (!this.history.subscribers.size) this.load();
431
+ if (!this.history.subscribers.size) this.load(historyAction ? { action: { type: historyAction } } : void 0);
429
432
  return this.commitLocationPromise;
430
433
  };
431
434
  this.buildAndCommitLocation = ({ replace, resetScroll, hashScrollIntoView, viewTransition, ignoreBlocker, href, ...rest } = {}) => {
@@ -530,6 +533,7 @@ var RouterCore = class {
530
533
  });
531
534
  };
532
535
  this.load = async (opts) => {
536
+ const historyAction = opts?.action?.type;
533
537
  let redirect;
534
538
  let notFound;
535
539
  let loadPromise;
@@ -538,6 +542,8 @@ var RouterCore = class {
538
542
  this.startTransition(async () => {
539
543
  try {
540
544
  this.beforeLoad();
545
+ if (historyAction) locationHistoryActions.set(this.latestLocation, historyAction);
546
+ else locationHistoryActions.delete(this.latestLocation);
541
547
  const next = this.latestLocation;
542
548
  const locationChangeInfo = getLocationChangeInfo(next, this.stores.resolvedLocation.get());
543
549
  if (!this.stores.redirect.get()) this.emit({
@@ -1172,6 +1178,6 @@ function extractStrictParams(route, accumulatedParams) {
1172
1178
  }
1173
1179
  }
1174
1180
  //#endregion
1175
- export { PathParamError, RouterCore, SearchParamError, defaultSerializeError, getInitialRouterState, getLocationChangeInfo, getMatchedRoutes, lazyFn, trailingSlashOptions };
1181
+ export { PathParamError, RouterCore, SearchParamError, defaultSerializeError, getInitialRouterState, getLocationChangeInfo, getMatchedRoutes, lazyFn, locationHistoryActions, trailingSlashOptions };
1176
1182
 
1177
1183
  //# sourceMappingURL=router.js.map