@tanstack/router-core 1.157.0 → 1.157.3

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 (57) hide show
  1. package/dist/cjs/index.cjs +0 -5
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs/index.d.cts +0 -1
  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/load-matches.cjs +8 -8
  14. package/dist/cjs/load-matches.cjs.map +1 -1
  15. package/dist/cjs/location.d.cts +2 -3
  16. package/dist/cjs/router.cjs +35 -26
  17. package/dist/cjs/router.cjs.map +1 -1
  18. package/dist/cjs/scroll-restoration.cjs +3 -3
  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 +0 -1
  24. package/dist/esm/index.js +0 -2
  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/load-matches.js +1 -1
  36. package/dist/esm/load-matches.js.map +1 -1
  37. package/dist/esm/location.d.ts +2 -3
  38. package/dist/esm/router.js +29 -20
  39. package/dist/esm/router.js.map +1 -1
  40. package/dist/esm/scroll-restoration.js +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 +82 -3
  46. package/src/index.ts +0 -1
  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/load-matches.ts +1 -1
  51. package/src/location.ts +2 -3
  52. package/src/router.ts +44 -26
  53. package/src/scroll-restoration.ts +1 -1
  54. package/src/utils.ts +15 -0
  55. package/dist/cjs/isServer.d.cts +0 -24
  56. package/dist/esm/isServer.d.ts +0 -24
  57. package/src/isServer.ts +0 -24
@@ -1,6 +1,7 @@
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 { isServer } from "@tanstack/router-core/isServer";
4
+ import { createControlledPromise, isDangerousProtocol, deepEqual, replaceEqualDeep, last, decodePath, functionalUpdate, findLast, encodeNonAscii } from "./utils.js";
4
5
  import { processRouteTree, processRouteMasks, findSingleMatch, findRouteMatch, findFlatMatch } from "./new-process-route-tree.js";
5
6
  import { compileDecodeCharMap, trimPath, resolvePath, cleanPath, trimPathRight, interpolatePath } from "./path.js";
6
7
  import { createLRUCache } from "./lru-cache.js";
@@ -11,7 +12,6 @@ import { rootRouteId } from "./root.js";
11
12
  import { redirect, isRedirect } from "./redirect.js";
12
13
  import { loadMatches, loadRouteChunk, routeNeedsPreload } from "./load-matches.js";
13
14
  import { rewriteBasepath, composeRewrites, executeRewriteInput, executeRewriteOutput } from "./rewrite.js";
14
- import { isServer } from "@tanstack/router-is-server";
15
15
  function defaultSerializeError(err) {
16
16
  if (err instanceof Error) {
17
17
  const obj = {
@@ -135,7 +135,8 @@ class RouterCore {
135
135
  if (basepathChanged || rewriteChanged) {
136
136
  this.basepath = nextBasepath;
137
137
  const rewrites = [];
138
- if (trimPath(nextBasepath) !== "") {
138
+ const trimmed = trimPath(nextBasepath);
139
+ if (trimmed && trimmed !== "/") {
139
140
  rewrites.push(
140
141
  rewriteBasepath({
141
142
  basepath: nextBasepath
@@ -215,8 +216,8 @@ class RouterCore {
215
216
  return {
216
217
  href: fullPath,
217
218
  publicHref: href,
218
- url,
219
219
  pathname: decodePath(url.pathname),
220
+ external: !!this.rewrite && url.origin !== this.origin,
220
221
  searchStr,
221
222
  search: replaceEqualDeep(previousLocation?.search, parsedSearch),
222
223
  hash: decodePath(url.hash.split("#").reverse()[0] ?? ""),
@@ -381,17 +382,32 @@ class RouterCore {
381
382
  let nextState = dest.state === true ? currentLocation.state : dest.state ? functionalUpdate(dest.state, currentLocation.state) : {};
382
383
  nextState = replaceEqualDeep(currentLocation.state, nextState);
383
384
  const fullPath = `${nextPathname}${searchStr}${hashStr}`;
384
- const url = new URL(fullPath, this.origin);
385
- const rewrittenUrl = executeRewriteOutput(this.rewrite, url);
385
+ let href;
386
+ let publicHref;
387
+ let external = false;
388
+ if (this.rewrite) {
389
+ const url = new URL(fullPath, this.origin);
390
+ const rewrittenUrl = executeRewriteOutput(this.rewrite, url);
391
+ href = url.href.replace(url.origin, "");
392
+ if (rewrittenUrl.origin !== this.origin) {
393
+ publicHref = rewrittenUrl.href;
394
+ external = true;
395
+ } else {
396
+ publicHref = rewrittenUrl.pathname + rewrittenUrl.search + rewrittenUrl.hash;
397
+ }
398
+ } else {
399
+ href = encodeNonAscii(fullPath);
400
+ publicHref = href;
401
+ }
386
402
  return {
387
- publicHref: rewrittenUrl.pathname + rewrittenUrl.search + rewrittenUrl.hash,
388
- href: fullPath,
389
- url: rewrittenUrl,
403
+ publicHref,
404
+ href,
390
405
  pathname: nextPathname,
391
406
  search: nextSearch,
392
407
  searchStr,
393
408
  state: nextState,
394
409
  hash: hash ?? "",
410
+ external,
395
411
  unmaskOnReload: dest.unmaskOnReload
396
412
  };
397
413
  };
@@ -470,9 +486,6 @@ class RouterCore {
470
486
  maskedLocation,
471
487
  // eslint-disable-next-line prefer-const
472
488
  hashScrollIntoView,
473
- // don't pass url into history since it is a URL instance that cannot be serialized
474
- // eslint-disable-next-line prefer-const
475
- url: _url,
476
489
  ...nextHistory
477
490
  } = next;
478
491
  if (maskedLocation) {
@@ -574,8 +587,8 @@ class RouterCore {
574
587
  if (reloadDocument) {
575
588
  if (to !== void 0 || !href) {
576
589
  const location = this.buildLocation({ to, ...rest });
577
- href = href ?? location.url.href;
578
- publicHref = publicHref ?? location.url.href;
590
+ href = href ?? location.publicHref;
591
+ publicHref = publicHref ?? location.publicHref;
579
592
  }
580
593
  const reloadHref = !hrefIsUrl && publicHref ? publicHref : href;
581
594
  if (isDangerousProtocol(reloadHref)) {
@@ -629,7 +642,7 @@ class RouterCore {
629
642
  state: true,
630
643
  _includeValidateSearch: true
631
644
  });
632
- if (this.latestLocation.publicHref !== nextLocation.publicHref || nextLocation.url.origin !== this.origin) {
645
+ if (this.latestLocation.publicHref !== nextLocation.publicHref) {
633
646
  const href = this.getParsedLocationHref(nextLocation);
634
647
  throw redirect({ href });
635
648
  }
@@ -850,11 +863,7 @@ class RouterCore {
850
863
  return this.load({ sync: opts?.sync });
851
864
  };
852
865
  this.getParsedLocationHref = (location) => {
853
- let href = location.url.href;
854
- if (this.origin && location.url.origin === this.origin) {
855
- href = href.replace(this.origin, "") || "/";
856
- }
857
- return href;
866
+ return location.publicHref || "/";
858
867
  };
859
868
  this.resolveRedirect = (redirect2) => {
860
869
  const locationHeader = redirect2.headers.get("Location");