@tanstack/router-core 1.132.0-alpha.4 → 1.132.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 (104) hide show
  1. package/dist/cjs/Matches.cjs +2 -1
  2. package/dist/cjs/Matches.cjs.map +1 -1
  3. package/dist/cjs/Matches.d.cts +2 -2
  4. package/dist/cjs/config.cjs +10 -0
  5. package/dist/cjs/config.cjs.map +1 -0
  6. package/dist/cjs/config.d.cts +17 -0
  7. package/dist/cjs/fileRoute.d.cts +3 -2
  8. package/dist/cjs/index.cjs +9 -2
  9. package/dist/cjs/index.cjs.map +1 -1
  10. package/dist/cjs/index.d.cts +10 -5
  11. package/dist/cjs/load-matches.cjs +5 -3
  12. package/dist/cjs/load-matches.cjs.map +1 -1
  13. package/dist/cjs/location.d.cts +38 -0
  14. package/dist/cjs/path.cjs +27 -64
  15. package/dist/cjs/path.cjs.map +1 -1
  16. package/dist/cjs/path.d.cts +6 -7
  17. package/dist/cjs/process-route-tree.cjs +144 -0
  18. package/dist/cjs/process-route-tree.cjs.map +1 -0
  19. package/dist/cjs/process-route-tree.d.cts +10 -0
  20. package/dist/cjs/redirect.cjs +1 -1
  21. package/dist/cjs/redirect.cjs.map +1 -1
  22. package/dist/cjs/rewrite.cjs +63 -0
  23. package/dist/cjs/rewrite.cjs.map +1 -0
  24. package/dist/cjs/rewrite.d.cts +22 -0
  25. package/dist/cjs/route.cjs.map +1 -1
  26. package/dist/cjs/route.d.cts +62 -44
  27. package/dist/cjs/router.cjs +102 -210
  28. package/dist/cjs/router.cjs.map +1 -1
  29. package/dist/cjs/router.d.cts +81 -44
  30. package/dist/cjs/scroll-restoration.cjs.map +1 -1
  31. package/dist/cjs/scroll-restoration.d.cts +9 -0
  32. package/dist/cjs/ssr/createRequestHandler.cjs +4 -1
  33. package/dist/cjs/ssr/createRequestHandler.cjs.map +1 -1
  34. package/dist/cjs/ssr/serializer/transformer.cjs +14 -12
  35. package/dist/cjs/ssr/serializer/transformer.cjs.map +1 -1
  36. package/dist/cjs/ssr/serializer/transformer.d.cts +55 -15
  37. package/dist/cjs/ssr/ssr-client.cjs.map +1 -1
  38. package/dist/cjs/ssr/ssr-server.cjs +5 -2
  39. package/dist/cjs/ssr/ssr-server.cjs.map +1 -1
  40. package/dist/cjs/ssr/ssr-server.d.cts +4 -1
  41. package/dist/cjs/utils.cjs +68 -46
  42. package/dist/cjs/utils.cjs.map +1 -1
  43. package/dist/esm/Matches.d.ts +2 -2
  44. package/dist/esm/Matches.js +2 -1
  45. package/dist/esm/Matches.js.map +1 -1
  46. package/dist/esm/config.d.ts +17 -0
  47. package/dist/esm/config.js +10 -0
  48. package/dist/esm/config.js.map +1 -0
  49. package/dist/esm/fileRoute.d.ts +3 -2
  50. package/dist/esm/index.d.ts +10 -5
  51. package/dist/esm/index.js +10 -3
  52. package/dist/esm/index.js.map +1 -1
  53. package/dist/esm/load-matches.js +5 -3
  54. package/dist/esm/load-matches.js.map +1 -1
  55. package/dist/esm/location.d.ts +38 -0
  56. package/dist/esm/path.d.ts +6 -7
  57. package/dist/esm/path.js +27 -64
  58. package/dist/esm/path.js.map +1 -1
  59. package/dist/esm/process-route-tree.d.ts +10 -0
  60. package/dist/esm/process-route-tree.js +144 -0
  61. package/dist/esm/process-route-tree.js.map +1 -0
  62. package/dist/esm/redirect.js +1 -1
  63. package/dist/esm/redirect.js.map +1 -1
  64. package/dist/esm/rewrite.d.ts +22 -0
  65. package/dist/esm/rewrite.js +63 -0
  66. package/dist/esm/rewrite.js.map +1 -0
  67. package/dist/esm/route.d.ts +62 -44
  68. package/dist/esm/route.js.map +1 -1
  69. package/dist/esm/router.d.ts +81 -44
  70. package/dist/esm/router.js +104 -212
  71. package/dist/esm/router.js.map +1 -1
  72. package/dist/esm/scroll-restoration.d.ts +9 -0
  73. package/dist/esm/scroll-restoration.js.map +1 -1
  74. package/dist/esm/ssr/createRequestHandler.js +4 -1
  75. package/dist/esm/ssr/createRequestHandler.js.map +1 -1
  76. package/dist/esm/ssr/serializer/transformer.d.ts +55 -15
  77. package/dist/esm/ssr/serializer/transformer.js +14 -12
  78. package/dist/esm/ssr/serializer/transformer.js.map +1 -1
  79. package/dist/esm/ssr/ssr-client.js.map +1 -1
  80. package/dist/esm/ssr/ssr-server.d.ts +4 -1
  81. package/dist/esm/ssr/ssr-server.js +5 -2
  82. package/dist/esm/ssr/ssr-server.js.map +1 -1
  83. package/dist/esm/utils.js +68 -46
  84. package/dist/esm/utils.js.map +1 -1
  85. package/package.json +2 -2
  86. package/src/Matches.ts +4 -3
  87. package/src/config.ts +42 -0
  88. package/src/fileRoute.ts +25 -3
  89. package/src/index.ts +23 -6
  90. package/src/load-matches.ts +31 -21
  91. package/src/location.ts +38 -0
  92. package/src/path.ts +44 -82
  93. package/src/process-route-tree.ts +233 -0
  94. package/src/redirect.ts +1 -1
  95. package/src/rewrite.ts +70 -0
  96. package/src/route.ts +311 -74
  97. package/src/router.ts +263 -389
  98. package/src/scroll-restoration.ts +1 -1
  99. package/src/ssr/createRequestHandler.ts +4 -1
  100. package/src/ssr/serializer/transformer.ts +168 -31
  101. package/src/ssr/server.ts +6 -0
  102. package/src/ssr/ssr-client.ts +2 -2
  103. package/src/ssr/ssr-server.ts +10 -7
  104. package/src/utils.ts +83 -61
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const store = require("@tanstack/store");
4
4
  const history = require("@tanstack/history");
5
- const invariant = require("tiny-invariant");
6
5
  const utils = require("./utils.cjs");
6
+ const processRouteTree = require("./process-route-tree.cjs");
7
7
  const path = require("./path.cjs");
8
8
  const notFound = require("./not-found.cjs");
9
9
  const scrollRestoration = require("./scroll-restoration.cjs");
@@ -12,6 +12,7 @@ const root = require("./root.cjs");
12
12
  const redirect = require("./redirect.cjs");
13
13
  const lruCache = require("./lru-cache.cjs");
14
14
  const loadMatches = require("./load-matches.cjs");
15
+ const rewrite = require("./rewrite.cjs");
15
16
  function defaultSerializeError(err) {
16
17
  if (err instanceof Error) {
17
18
  const obj = {
@@ -56,7 +57,6 @@ class RouterCore {
56
57
  "The notFoundRoute API is deprecated and will be removed in the next major version. See https://tanstack.com/router/v1/docs/framework/react/guide/not-found-errors#migrating-from-notfoundroute for more info."
57
58
  );
58
59
  }
59
- const previousOptions = this.options;
60
60
  this.options = {
61
61
  ...this.options,
62
62
  ...newOptions
@@ -68,24 +68,43 @@ class RouterCore {
68
68
  char
69
69
  ])
70
70
  ) : void 0;
71
- if (!this.basepath || newOptions.basepath && newOptions.basepath !== previousOptions.basepath) {
72
- if (newOptions.basepath === void 0 || newOptions.basepath === "" || newOptions.basepath === "/") {
73
- this.basepath = "/";
71
+ if (!this.history || this.options.history && this.options.history !== this.history) {
72
+ if (!this.options.history) {
73
+ if (!this.isServer) {
74
+ this.history = history.createBrowserHistory();
75
+ }
74
76
  } else {
75
- this.basepath = `/${path.trimPath(newOptions.basepath)}`;
77
+ this.history = this.options.history;
76
78
  }
77
79
  }
78
- if (!this.history || this.options.history && this.options.history !== this.history) {
79
- this.history = this.options.history ?? (this.isServer ? history.createMemoryHistory({
80
- initialEntries: [this.basepath || "/"]
81
- }) : history.createBrowserHistory());
82
- this.latestLocation = this.parseLocation();
80
+ if (this.options.basepath) {
81
+ const basepathRewrite = rewrite.rewriteBasepath({
82
+ basepath: this.options.basepath
83
+ });
84
+ if (this.options.rewrite) {
85
+ this.rewrite = rewrite.composeRewrites([basepathRewrite, this.options.rewrite]);
86
+ } else {
87
+ this.rewrite = basepathRewrite;
88
+ }
89
+ } else {
90
+ this.rewrite = this.options.rewrite;
91
+ }
92
+ this.origin = this.options.origin;
93
+ if (!this.origin) {
94
+ if (!this.isServer) {
95
+ this.origin = window.origin;
96
+ } else {
97
+ this.origin = "http://localhost";
98
+ }
99
+ }
100
+ if (this.history) {
101
+ this.updateLatestLocation();
83
102
  }
84
103
  if (this.options.routeTree !== this.routeTree) {
85
104
  this.routeTree = this.options.routeTree;
86
105
  this.buildRouteTree();
87
106
  }
88
- if (!this.__store) {
107
+ if (!this.__store && this.latestLocation) {
89
108
  this.__store = new store.Store(getInitialRouterState(this.latestLocation), {
90
109
  onUpdate: () => {
91
110
  this.__store.state = {
@@ -104,8 +123,14 @@ class RouterCore {
104
123
  );
105
124
  }
106
125
  };
126
+ this.updateLatestLocation = () => {
127
+ this.latestLocation = this.parseLocation(
128
+ this.history.location,
129
+ this.latestLocation
130
+ );
131
+ };
107
132
  this.buildRouteTree = () => {
108
- const { routesById, routesByPath, flatRoutes } = processRouteTree({
133
+ const { routesById, routesByPath, flatRoutes } = processRouteTree.processRouteTree({
109
134
  routeTree: this.routeTree,
110
135
  initRoute: (route, i) => {
111
136
  route.init({
@@ -141,25 +166,30 @@ class RouterCore {
141
166
  }
142
167
  });
143
168
  };
144
- this.parseLocation = (previousLocation, locationToParse) => {
169
+ this.parseLocation = (locationToParse, previousLocation) => {
145
170
  const parse = ({
146
- pathname,
147
- search,
148
- hash,
171
+ href,
149
172
  state
150
173
  }) => {
151
- const parsedSearch = this.options.parseSearch(search);
174
+ const fullUrl = new URL(href, this.origin);
175
+ const url = rewrite.executeRewriteInput(this.rewrite, fullUrl);
176
+ const parsedSearch = this.options.parseSearch(url.search);
152
177
  const searchStr = this.options.stringifySearch(parsedSearch);
178
+ url.search = searchStr;
179
+ const fullPath = url.href.replace(url.origin, "");
180
+ const { pathname, hash } = url;
153
181
  return {
182
+ href: fullPath,
183
+ publicHref: href,
184
+ url: url.href,
154
185
  pathname,
155
186
  searchStr,
156
187
  search: utils.replaceEqualDeep(previousLocation?.search, parsedSearch),
157
188
  hash: hash.split("#").reverse()[0] ?? "",
158
- href: `${pathname}${searchStr}${hash}`,
159
189
  state: utils.replaceEqualDeep(previousLocation?.state, state)
160
190
  };
161
191
  };
162
- const location = parse(locationToParse ?? this.history.location);
192
+ const location = parse(locationToParse);
163
193
  const { __tempLocation, __tempKey } = location.state;
164
194
  if (__tempLocation && (!__tempKey || __tempKey === this.tempLocationKey)) {
165
195
  const parsedTempLocation = parse(__tempLocation);
@@ -175,11 +205,9 @@ class RouterCore {
175
205
  };
176
206
  this.resolvePathWithBase = (from, path$1) => {
177
207
  const resolvedPath = path.resolvePath({
178
- basepath: this.basepath,
179
208
  base: from,
180
209
  to: path.cleanPath(path$1),
181
210
  trailingSlash: this.options.trailingSlash,
182
- caseSensitive: this.options.caseSensitive,
183
211
  parseCache: this.parsePathnameCache
184
212
  });
185
213
  return resolvedPath;
@@ -201,7 +229,6 @@ class RouterCore {
201
229
  return getMatchedRoutes({
202
230
  pathname,
203
231
  routePathname,
204
- basepath: this.basepath,
205
232
  caseSensitive: this.options.caseSensitive,
206
233
  routesByPath: this.routesByPath,
207
234
  routesById: this.routesById,
@@ -228,30 +255,23 @@ class RouterCore {
228
255
  _buildLocation: true
229
256
  });
230
257
  const lastMatch = utils.last(allCurrentLocationMatches);
231
- let fromPath = this.resolvePathWithBase(lastMatch.fullPath, ".");
232
- const toPath = dest.to ? this.resolvePathWithBase(fromPath, `${dest.to}`) : this.resolvePathWithBase(fromPath, ".");
233
- const routeIsChanging = !!dest.to && !comparePaths(dest.to.toString(), fromPath) && !comparePaths(toPath, fromPath);
234
- if (dest.unsafeRelative === "path") {
235
- fromPath = currentLocation.pathname;
236
- } else if (routeIsChanging && dest.from) {
237
- fromPath = dest.from;
238
- if (process.env.NODE_ENV !== "production" && dest._isNavigate) {
239
- const allFromMatches = this.getMatchedRoutes(
240
- dest.from,
241
- void 0
242
- ).matchedRoutes;
243
- const matchedFrom = utils.findLast(allCurrentLocationMatches, (d) => {
244
- return comparePaths(d.fullPath, fromPath);
245
- });
246
- const matchedCurrent = utils.findLast(allFromMatches, (d) => {
247
- return comparePaths(d.fullPath, currentLocation.pathname);
248
- });
249
- if (!matchedFrom && !matchedCurrent) {
250
- console.warn(`Could not find match for from: ${fromPath}`);
251
- }
258
+ if (dest.from && process.env.NODE_ENV !== "production" && dest._isNavigate) {
259
+ const allFromMatches = this.getMatchedRoutes(
260
+ dest.from,
261
+ void 0
262
+ ).matchedRoutes;
263
+ const matchedFrom = utils.findLast(allCurrentLocationMatches, (d) => {
264
+ return comparePaths(d.fullPath, dest.from);
265
+ });
266
+ const matchedCurrent = utils.findLast(allFromMatches, (d) => {
267
+ return comparePaths(d.fullPath, lastMatch.fullPath);
268
+ });
269
+ if (!matchedFrom && !matchedCurrent) {
270
+ console.warn(`Could not find match for from: ${dest.from}`);
252
271
  }
253
272
  }
254
- fromPath = this.resolvePathWithBase(fromPath, ".");
273
+ const defaultedFromPath = dest.unsafeRelative === "path" ? currentLocation.pathname : dest.from ?? lastMatch.fullPath;
274
+ const fromPath = this.resolvePathWithBase(defaultedFromPath, ".");
255
275
  const fromSearch = lastMatch.search;
256
276
  const fromParams = { ...lastMatch.params };
257
277
  const nextTo = dest.to ? this.resolvePathWithBase(fromPath, `${dest.to}`) : this.resolvePathWithBase(fromPath, ".");
@@ -316,13 +336,18 @@ class RouterCore {
316
336
  const hashStr = hash ? `#${hash}` : "";
317
337
  let nextState = dest.state === true ? currentLocation.state : dest.state ? utils.functionalUpdate(dest.state, currentLocation.state) : {};
318
338
  nextState = utils.replaceEqualDeep(currentLocation.state, nextState);
339
+ const fullPath = `${nextPathname}${searchStr}${hashStr}`;
340
+ const url = new URL(fullPath, this.origin);
341
+ const rewrittenUrl = rewrite.executeRewriteOutput(this.rewrite, url);
319
342
  return {
343
+ publicHref: rewrittenUrl.pathname + rewrittenUrl.search + rewrittenUrl.hash,
344
+ href: fullPath,
345
+ url: rewrittenUrl.href,
320
346
  pathname: nextPathname,
321
347
  search: nextSearch,
322
348
  searchStr,
323
349
  state: nextState,
324
350
  hash: hash ?? "",
325
- href: `${nextPathname}${searchStr}${hashStr}`,
326
351
  unmaskOnReload: dest.unmaskOnReload
327
352
  };
328
353
  };
@@ -333,7 +358,6 @@ class RouterCore {
333
358
  let params = {};
334
359
  const foundMask = this.options.routeMasks?.find((d) => {
335
360
  const match = path.matchPathname(
336
- this.basepath,
337
361
  next.pathname,
338
362
  {
339
363
  to: d.from,
@@ -359,8 +383,7 @@ class RouterCore {
359
383
  }
360
384
  }
361
385
  if (maskedNext) {
362
- const maskedFinal = build(maskedDest);
363
- next.maskedLocation = maskedFinal;
386
+ next.maskedLocation = maskedNext;
364
387
  }
365
388
  return next;
366
389
  };
@@ -394,7 +417,7 @@ class RouterCore {
394
417
  });
395
418
  return isEqual;
396
419
  };
397
- const isSameUrl = this.latestLocation.href === next.href;
420
+ const isSameUrl = path.trimPathRight(this.latestLocation.href) === path.trimPathRight(next.href);
398
421
  const previousCommitPromise = this.commitLocationPromise;
399
422
  this.commitLocationPromise = utils.createControlledPromise(() => {
400
423
  previousCommitPromise?.resolve();
@@ -430,7 +453,7 @@ class RouterCore {
430
453
  nextHistory.state.__hashScrollIntoViewOptions = hashScrollIntoView ?? this.options.defaultHashScrollIntoView ?? true;
431
454
  this.shouldViewTransition = viewTransition;
432
455
  this.history[next.replace ? "replace" : "push"](
433
- nextHistory.href,
456
+ nextHistory.publicHref,
434
457
  nextHistory.state,
435
458
  { ignoreBlocker }
436
459
  );
@@ -483,7 +506,7 @@ class RouterCore {
483
506
  if (reloadDocument) {
484
507
  if (!href) {
485
508
  const location = this.buildLocation({ to, ...rest });
486
- href = this.history.createHref(location.href);
509
+ href = location.href;
487
510
  }
488
511
  if (rest.replace) {
489
512
  window.location.replace(href);
@@ -501,7 +524,7 @@ class RouterCore {
501
524
  };
502
525
  this.beforeLoad = () => {
503
526
  this.cancelMatches();
504
- this.latestLocation = this.parseLocation(this.latestLocation);
527
+ this.updateLatestLocation();
505
528
  if (this.isServer) {
506
529
  const nextLocation = this.buildLocation({
507
530
  to: this.latestLocation.pathname,
@@ -830,7 +853,6 @@ class RouterCore {
830
853
  const pending = opts?.pending === void 0 ? !this.state.isLoading : opts.pending;
831
854
  const baseLocation = pending ? this.latestLocation : this.state.resolvedLocation || this.state.location;
832
855
  const match = path.matchPathname(
833
- this.basepath,
834
856
  baseLocation.pathname,
835
857
  {
836
858
  ...opts,
@@ -916,25 +938,6 @@ class RouterCore {
916
938
  }
917
939
  return root.rootRouteId;
918
940
  })();
919
- const parseErrors = matchedRoutes.map((route) => {
920
- let parsedParamsError;
921
- const parseParams = route.options.params?.parse ?? route.options.parseParams;
922
- if (parseParams) {
923
- try {
924
- const parsedParams = parseParams(routeParams);
925
- Object.assign(routeParams, parsedParams);
926
- } catch (err) {
927
- parsedParamsError = new PathParamError(err.message, {
928
- cause: err
929
- });
930
- if (opts?.throwOnError) {
931
- throw parsedParamsError;
932
- }
933
- return parsedParamsError;
934
- }
935
- }
936
- return;
937
- });
938
941
  const matches = [];
939
942
  const getParentContext = (parentMatch) => {
940
943
  const parentMatchId = parentMatch?.id;
@@ -973,7 +976,7 @@ class RouterCore {
973
976
  search: preMatchSearch
974
977
  }) ?? "";
975
978
  const loaderDepsHash = loaderDeps ? JSON.stringify(loaderDeps) : "";
976
- const { usedParams, interpolatedPath } = path.interpolatePath({
979
+ const { interpolatedPath, usedParams } = path.interpolatePath({
977
980
  path: route.fullPath,
978
981
  params: routeParams,
979
982
  decodeCharMap: this.pathParamsDecodeCharMap
@@ -989,6 +992,27 @@ class RouterCore {
989
992
  const previousMatch = this.state.matches.find(
990
993
  (d) => d.routeId === route.id
991
994
  );
995
+ const strictParams = existingMatch?._strictParams ?? usedParams;
996
+ let paramsError = void 0;
997
+ if (!existingMatch) {
998
+ const strictParseParams = route.options.params?.parse ?? route.options.parseParams;
999
+ if (strictParseParams) {
1000
+ try {
1001
+ Object.assign(
1002
+ strictParams,
1003
+ strictParseParams(strictParams)
1004
+ );
1005
+ } catch (err) {
1006
+ paramsError = new PathParamError(err.message, {
1007
+ cause: err
1008
+ });
1009
+ if (opts?.throwOnError) {
1010
+ throw paramsError;
1011
+ }
1012
+ }
1013
+ }
1014
+ }
1015
+ Object.assign(routeParams, strictParams);
992
1016
  const cause = previousMatch ? "stay" : "enter";
993
1017
  let match;
994
1018
  if (existingMatch) {
@@ -996,7 +1020,7 @@ class RouterCore {
996
1020
  ...existingMatch,
997
1021
  cause,
998
1022
  params: previousMatch ? utils.replaceEqualDeep(previousMatch.params, routeParams) : routeParams,
999
- _strictParams: usedParams,
1023
+ _strictParams: strictParams,
1000
1024
  search: previousMatch ? utils.replaceEqualDeep(previousMatch.search, preMatchSearch) : utils.replaceEqualDeep(existingMatch.search, preMatchSearch),
1001
1025
  _strictSearch: strictMatchSearch
1002
1026
  };
@@ -1007,8 +1031,8 @@ class RouterCore {
1007
1031
  index,
1008
1032
  routeId: route.id,
1009
1033
  params: previousMatch ? utils.replaceEqualDeep(previousMatch.params, routeParams) : routeParams,
1010
- _strictParams: usedParams,
1011
- pathname: path.joinPaths([this.basepath, interpolatedPath]),
1034
+ _strictParams: strictParams,
1035
+ pathname: interpolatedPath,
1012
1036
  updatedAt: Date.now(),
1013
1037
  search: previousMatch ? utils.replaceEqualDeep(previousMatch.search, preMatchSearch) : preMatchSearch,
1014
1038
  _strictSearch: strictMatchSearch,
@@ -1016,7 +1040,7 @@ class RouterCore {
1016
1040
  status,
1017
1041
  isFetching: false,
1018
1042
  error: void 0,
1019
- paramsError: parseErrors[index],
1043
+ paramsError,
1020
1044
  __routeContext: void 0,
1021
1045
  _nonReactive: {
1022
1046
  loadPromise: utils.createControlledPromise()
@@ -1128,139 +1152,9 @@ function validateSearch(validateSearch2, input) {
1128
1152
  }
1129
1153
  return {};
1130
1154
  }
1131
- const REQUIRED_PARAM_BASE_SCORE = 0.5;
1132
- const OPTIONAL_PARAM_BASE_SCORE = 0.4;
1133
- const WILDCARD_PARAM_BASE_SCORE = 0.25;
1134
- const BOTH_PRESENCE_BASE_SCORE = 0.05;
1135
- const PREFIX_PRESENCE_BASE_SCORE = 0.02;
1136
- const SUFFIX_PRESENCE_BASE_SCORE = 0.01;
1137
- const PREFIX_LENGTH_SCORE_MULTIPLIER = 2e-4;
1138
- const SUFFIX_LENGTH_SCORE_MULTIPLIER = 1e-4;
1139
- function handleParam(segment, baseScore) {
1140
- if (segment.prefixSegment && segment.suffixSegment) {
1141
- return baseScore + BOTH_PRESENCE_BASE_SCORE + PREFIX_LENGTH_SCORE_MULTIPLIER * segment.prefixSegment.length + SUFFIX_LENGTH_SCORE_MULTIPLIER * segment.suffixSegment.length;
1142
- }
1143
- if (segment.prefixSegment) {
1144
- return baseScore + PREFIX_PRESENCE_BASE_SCORE + PREFIX_LENGTH_SCORE_MULTIPLIER * segment.prefixSegment.length;
1145
- }
1146
- if (segment.suffixSegment) {
1147
- return baseScore + SUFFIX_PRESENCE_BASE_SCORE + SUFFIX_LENGTH_SCORE_MULTIPLIER * segment.suffixSegment.length;
1148
- }
1149
- return baseScore;
1150
- }
1151
- function processRouteTree({
1152
- routeTree,
1153
- initRoute
1154
- }) {
1155
- const routesById = {};
1156
- const routesByPath = {};
1157
- const recurseRoutes = (childRoutes) => {
1158
- childRoutes.forEach((childRoute, i) => {
1159
- initRoute?.(childRoute, i);
1160
- const existingRoute = routesById[childRoute.id];
1161
- invariant(
1162
- !existingRoute,
1163
- `Duplicate routes found with id: ${String(childRoute.id)}`
1164
- );
1165
- routesById[childRoute.id] = childRoute;
1166
- if (!childRoute.isRoot && childRoute.path) {
1167
- const trimmedFullPath = path.trimPathRight(childRoute.fullPath);
1168
- if (!routesByPath[trimmedFullPath] || childRoute.fullPath.endsWith("/")) {
1169
- routesByPath[trimmedFullPath] = childRoute;
1170
- }
1171
- }
1172
- const children = childRoute.children;
1173
- if (children?.length) {
1174
- recurseRoutes(children);
1175
- }
1176
- });
1177
- };
1178
- recurseRoutes([routeTree]);
1179
- const scoredRoutes = [];
1180
- const routes = Object.values(routesById);
1181
- routes.forEach((d, i) => {
1182
- if (d.isRoot || !d.path) {
1183
- return;
1184
- }
1185
- const trimmed = path.trimPathLeft(d.fullPath);
1186
- let parsed = path.parsePathname(trimmed);
1187
- let skip = 0;
1188
- while (parsed.length > skip + 1 && parsed[skip]?.value === "/") {
1189
- skip++;
1190
- }
1191
- if (skip > 0) parsed = parsed.slice(skip);
1192
- let optionalParamCount = 0;
1193
- let hasStaticAfter = false;
1194
- const scores = parsed.map((segment, index) => {
1195
- if (segment.value === "/") {
1196
- return 0.75;
1197
- }
1198
- let baseScore = void 0;
1199
- if (segment.type === path.SEGMENT_TYPE_PARAM) {
1200
- baseScore = REQUIRED_PARAM_BASE_SCORE;
1201
- } else if (segment.type === path.SEGMENT_TYPE_OPTIONAL_PARAM) {
1202
- baseScore = OPTIONAL_PARAM_BASE_SCORE;
1203
- optionalParamCount++;
1204
- } else if (segment.type === path.SEGMENT_TYPE_WILDCARD) {
1205
- baseScore = WILDCARD_PARAM_BASE_SCORE;
1206
- }
1207
- if (baseScore) {
1208
- for (let i2 = index + 1; i2 < parsed.length; i2++) {
1209
- const nextSegment = parsed[i2];
1210
- if (nextSegment.type === path.SEGMENT_TYPE_PATHNAME && nextSegment.value !== "/") {
1211
- hasStaticAfter = true;
1212
- return handleParam(segment, baseScore + 0.2);
1213
- }
1214
- }
1215
- return handleParam(segment, baseScore);
1216
- }
1217
- return 1;
1218
- });
1219
- scoredRoutes.push({
1220
- child: d,
1221
- trimmed,
1222
- parsed,
1223
- index: i,
1224
- scores,
1225
- optionalParamCount,
1226
- hasStaticAfter
1227
- });
1228
- });
1229
- const flatRoutes = scoredRoutes.sort((a, b) => {
1230
- const minLength = Math.min(a.scores.length, b.scores.length);
1231
- for (let i = 0; i < minLength; i++) {
1232
- if (a.scores[i] !== b.scores[i]) {
1233
- return b.scores[i] - a.scores[i];
1234
- }
1235
- }
1236
- if (a.scores.length !== b.scores.length) {
1237
- if (a.optionalParamCount !== b.optionalParamCount) {
1238
- if (a.hasStaticAfter === b.hasStaticAfter) {
1239
- return a.optionalParamCount - b.optionalParamCount;
1240
- } else if (a.hasStaticAfter && !b.hasStaticAfter) {
1241
- return -1;
1242
- } else if (!a.hasStaticAfter && b.hasStaticAfter) {
1243
- return 1;
1244
- }
1245
- }
1246
- return b.scores.length - a.scores.length;
1247
- }
1248
- for (let i = 0; i < minLength; i++) {
1249
- if (a.parsed[i].value !== b.parsed[i].value) {
1250
- return a.parsed[i].value > b.parsed[i].value ? 1 : -1;
1251
- }
1252
- }
1253
- return a.index - b.index;
1254
- }).map((d, i) => {
1255
- d.child.rank = i;
1256
- return d.child;
1257
- });
1258
- return { routesById, routesByPath, flatRoutes };
1259
- }
1260
1155
  function getMatchedRoutes({
1261
1156
  pathname,
1262
1157
  routePathname,
1263
- basepath,
1264
1158
  caseSensitive,
1265
1159
  routesByPath,
1266
1160
  routesById,
@@ -1271,7 +1165,6 @@ function getMatchedRoutes({
1271
1165
  const trimmedPath = path.trimPathRight(pathname);
1272
1166
  const getMatchedParams = (route) => {
1273
1167
  const result = path.matchPathname(
1274
- basepath,
1275
1168
  trimmedPath,
1276
1169
  {
1277
1170
  to: route.fullPath,
@@ -1401,5 +1294,4 @@ exports.getInitialRouterState = getInitialRouterState;
1401
1294
  exports.getLocationChangeInfo = getLocationChangeInfo;
1402
1295
  exports.getMatchedRoutes = getMatchedRoutes;
1403
1296
  exports.lazyFn = lazyFn;
1404
- exports.processRouteTree = processRouteTree;
1405
1297
  //# sourceMappingURL=router.cjs.map