@tanstack/router-core 1.156.0 → 1.157.1

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 (55) hide show
  1. package/dist/cjs/index.cjs +2 -0
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs/index.d.cts +1 -0
  4. package/dist/cjs/isServer/client.cjs +5 -0
  5. package/dist/cjs/isServer/client.cjs.map +1 -0
  6. package/dist/cjs/isServer/client.d.cts +1 -0
  7. package/dist/cjs/isServer/development.cjs +5 -0
  8. package/dist/cjs/isServer/development.cjs.map +1 -0
  9. package/dist/cjs/isServer/development.d.cts +1 -0
  10. package/dist/cjs/isServer/server.cjs +5 -0
  11. package/dist/cjs/isServer/server.cjs.map +1 -0
  12. package/dist/cjs/isServer/server.d.cts +1 -0
  13. package/dist/cjs/isServer.d.cts +24 -0
  14. package/dist/cjs/load-matches.cjs +2 -1
  15. package/dist/cjs/load-matches.cjs.map +1 -1
  16. package/dist/cjs/location.d.cts +2 -3
  17. package/dist/cjs/router.cjs +27 -18
  18. package/dist/cjs/router.cjs.map +1 -1
  19. package/dist/cjs/scroll-restoration.cjs.map +1 -1
  20. package/dist/cjs/utils.cjs +5 -0
  21. package/dist/cjs/utils.cjs.map +1 -1
  22. package/dist/cjs/utils.d.cts +9 -0
  23. package/dist/esm/index.d.ts +1 -0
  24. package/dist/esm/index.js +2 -0
  25. package/dist/esm/index.js.map +1 -1
  26. package/dist/esm/isServer/client.d.ts +1 -0
  27. package/dist/esm/isServer/client.js +5 -0
  28. package/dist/esm/isServer/client.js.map +1 -0
  29. package/dist/esm/isServer/development.d.ts +1 -0
  30. package/dist/esm/isServer/development.js +5 -0
  31. package/dist/esm/isServer/development.js.map +1 -0
  32. package/dist/esm/isServer/server.d.ts +1 -0
  33. package/dist/esm/isServer/server.js +5 -0
  34. package/dist/esm/isServer/server.js.map +1 -0
  35. package/dist/esm/isServer.d.ts +24 -0
  36. package/dist/esm/load-matches.js +2 -1
  37. package/dist/esm/load-matches.js.map +1 -1
  38. package/dist/esm/location.d.ts +2 -3
  39. package/dist/esm/router.js +28 -19
  40. package/dist/esm/router.js.map +1 -1
  41. package/dist/esm/scroll-restoration.js.map +1 -1
  42. package/dist/esm/utils.d.ts +9 -0
  43. package/dist/esm/utils.js +5 -0
  44. package/dist/esm/utils.js.map +1 -1
  45. package/package.json +81 -1
  46. package/src/index.ts +1 -0
  47. package/src/isServer/client.ts +1 -0
  48. package/src/isServer/development.ts +2 -0
  49. package/src/isServer/server.ts +1 -0
  50. package/src/isServer.ts +24 -0
  51. package/src/load-matches.ts +8 -7
  52. package/src/location.ts +2 -3
  53. package/src/router.ts +58 -32
  54. package/src/scroll-restoration.ts +3 -2
  55. package/src/utils.ts +15 -0
@@ -43,8 +43,7 @@ export interface ParsedLocation<TSearchObj extends AnySchema = {}> {
43
43
  publicHref: string;
44
44
  /**
45
45
  * @private
46
- * @description The full URL of the location.
47
- * @private
46
+ * @description Whether the publicHref is external (different origin from rewrite).
48
47
  */
49
- url: URL;
48
+ external: boolean;
50
49
  }
@@ -1,6 +1,6 @@
1
1
  import { Store, batch } from "@tanstack/store";
2
2
  import { createBrowserHistory, parseHref } from "@tanstack/history";
3
- import { createControlledPromise, isDangerousProtocol, deepEqual, replaceEqualDeep, last, decodePath, functionalUpdate, findLast } from "./utils.js";
3
+ import { createControlledPromise, isDangerousProtocol, deepEqual, replaceEqualDeep, last, decodePath, functionalUpdate, findLast, encodeNonAscii } from "./utils.js";
4
4
  import { processRouteTree, processRouteMasks, findSingleMatch, findRouteMatch, findFlatMatch } from "./new-process-route-tree.js";
5
5
  import { compileDecodeCharMap, trimPath, resolvePath, cleanPath, trimPathRight, interpolatePath } from "./path.js";
6
6
  import { createLRUCache } from "./lru-cache.js";
@@ -134,7 +134,8 @@ class RouterCore {
134
134
  if (basepathChanged || rewriteChanged) {
135
135
  this.basepath = nextBasepath;
136
136
  const rewrites = [];
137
- if (trimPath(nextBasepath) !== "") {
137
+ const trimmed = trimPath(nextBasepath);
138
+ if (trimmed && trimmed !== "/") {
138
139
  rewrites.push(
139
140
  rewriteBasepath({
140
141
  basepath: nextBasepath
@@ -214,8 +215,8 @@ class RouterCore {
214
215
  return {
215
216
  href: fullPath,
216
217
  publicHref: href,
217
- url,
218
218
  pathname: decodePath(url.pathname),
219
+ external: !!this.rewrite && url.origin !== this.origin,
219
220
  searchStr,
220
221
  search: replaceEqualDeep(previousLocation?.search, parsedSearch),
221
222
  hash: decodePath(url.hash.split("#").reverse()[0] ?? ""),
@@ -380,17 +381,32 @@ class RouterCore {
380
381
  let nextState = dest.state === true ? currentLocation.state : dest.state ? functionalUpdate(dest.state, currentLocation.state) : {};
381
382
  nextState = replaceEqualDeep(currentLocation.state, nextState);
382
383
  const fullPath = `${nextPathname}${searchStr}${hashStr}`;
383
- const url = new URL(fullPath, this.origin);
384
- const rewrittenUrl = executeRewriteOutput(this.rewrite, url);
384
+ let href;
385
+ let publicHref;
386
+ let external = false;
387
+ if (this.rewrite) {
388
+ const url = new URL(fullPath, this.origin);
389
+ const rewrittenUrl = executeRewriteOutput(this.rewrite, url);
390
+ href = url.href.replace(url.origin, "");
391
+ if (rewrittenUrl.origin !== this.origin) {
392
+ publicHref = rewrittenUrl.href;
393
+ external = true;
394
+ } else {
395
+ publicHref = rewrittenUrl.pathname + rewrittenUrl.search + rewrittenUrl.hash;
396
+ }
397
+ } else {
398
+ href = encodeNonAscii(fullPath);
399
+ publicHref = href;
400
+ }
385
401
  return {
386
- publicHref: rewrittenUrl.pathname + rewrittenUrl.search + rewrittenUrl.hash,
387
- href: fullPath,
388
- url: rewrittenUrl,
402
+ publicHref,
403
+ href,
389
404
  pathname: nextPathname,
390
405
  search: nextSearch,
391
406
  searchStr,
392
407
  state: nextState,
393
408
  hash: hash ?? "",
409
+ external,
394
410
  unmaskOnReload: dest.unmaskOnReload
395
411
  };
396
412
  };
@@ -469,9 +485,6 @@ class RouterCore {
469
485
  maskedLocation,
470
486
  // eslint-disable-next-line prefer-const
471
487
  hashScrollIntoView,
472
- // don't pass url into history since it is a URL instance that cannot be serialized
473
- // eslint-disable-next-line prefer-const
474
- url: _url,
475
488
  ...nextHistory
476
489
  } = next;
477
490
  if (maskedLocation) {
@@ -573,8 +586,8 @@ class RouterCore {
573
586
  if (reloadDocument) {
574
587
  if (to !== void 0 || !href) {
575
588
  const location = this.buildLocation({ to, ...rest });
576
- href = href ?? location.url.href;
577
- publicHref = publicHref ?? location.url.href;
589
+ href = href ?? location.publicHref;
590
+ publicHref = publicHref ?? location.publicHref;
578
591
  }
579
592
  const reloadHref = !hrefIsUrl && publicHref ? publicHref : href;
580
593
  if (isDangerousProtocol(reloadHref)) {
@@ -628,7 +641,7 @@ class RouterCore {
628
641
  state: true,
629
642
  _includeValidateSearch: true
630
643
  });
631
- if (this.latestLocation.publicHref !== nextLocation.publicHref || nextLocation.url.origin !== this.origin) {
644
+ if (this.latestLocation.publicHref !== nextLocation.publicHref) {
632
645
  const href = this.getParsedLocationHref(nextLocation);
633
646
  throw redirect({ href });
634
647
  }
@@ -849,11 +862,7 @@ class RouterCore {
849
862
  return this.load({ sync: opts?.sync });
850
863
  };
851
864
  this.getParsedLocationHref = (location) => {
852
- let href = location.url.href;
853
- if (this.origin && location.url.origin === this.origin) {
854
- href = href.replace(this.origin, "") || "/";
855
- }
856
- return href;
865
+ return location.publicHref || "/";
857
866
  };
858
867
  this.resolveRedirect = (redirect2) => {
859
868
  const locationHeader = redirect2.headers.get("Location");