@tanstack/router-core 1.132.0-alpha.12 → 1.132.0-alpha.16

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.
@@ -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());
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) {
82
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 = {
@@ -149,19 +168,24 @@ class RouterCore {
149
168
  };
150
169
  this.parseLocation = (locationToParse, previousLocation) => {
151
170
  const parse = ({
152
- pathname,
153
- search,
154
- hash,
171
+ href,
155
172
  state
156
173
  }) => {
157
- 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);
158
177
  const searchStr = this.options.stringifySearch(parsedSearch);
178
+ url.search = searchStr;
179
+ const fullPath = url.href.replace(url.origin, "");
180
+ const { pathname, hash } = url;
159
181
  return {
182
+ href: fullPath,
183
+ publicHref: href,
184
+ url: url.href,
160
185
  pathname,
161
186
  searchStr,
162
187
  search: utils.replaceEqualDeep(previousLocation?.search, parsedSearch),
163
188
  hash: hash.split("#").reverse()[0] ?? "",
164
- href: `${pathname}${searchStr}${hash}`,
165
189
  state: utils.replaceEqualDeep(previousLocation?.state, state)
166
190
  };
167
191
  };
@@ -181,11 +205,9 @@ class RouterCore {
181
205
  };
182
206
  this.resolvePathWithBase = (from, path$1) => {
183
207
  const resolvedPath = path.resolvePath({
184
- basepath: this.basepath,
185
208
  base: from,
186
209
  to: path.cleanPath(path$1),
187
210
  trailingSlash: this.options.trailingSlash,
188
- caseSensitive: this.options.caseSensitive,
189
211
  parseCache: this.parsePathnameCache
190
212
  });
191
213
  return resolvedPath;
@@ -207,7 +229,6 @@ class RouterCore {
207
229
  return getMatchedRoutes({
208
230
  pathname,
209
231
  routePathname,
210
- basepath: this.basepath,
211
232
  caseSensitive: this.options.caseSensitive,
212
233
  routesByPath: this.routesByPath,
213
234
  routesById: this.routesById,
@@ -315,13 +336,18 @@ class RouterCore {
315
336
  const hashStr = hash ? `#${hash}` : "";
316
337
  let nextState = dest.state === true ? currentLocation.state : dest.state ? utils.functionalUpdate(dest.state, currentLocation.state) : {};
317
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);
318
342
  return {
343
+ publicHref: rewrittenUrl.pathname + rewrittenUrl.search + rewrittenUrl.hash,
344
+ href: fullPath,
345
+ url: rewrittenUrl.href,
319
346
  pathname: nextPathname,
320
347
  search: nextSearch,
321
348
  searchStr,
322
349
  state: nextState,
323
350
  hash: hash ?? "",
324
- href: `${nextPathname}${searchStr}${hashStr}`,
325
351
  unmaskOnReload: dest.unmaskOnReload
326
352
  };
327
353
  };
@@ -332,7 +358,6 @@ class RouterCore {
332
358
  let params = {};
333
359
  const foundMask = this.options.routeMasks?.find((d) => {
334
360
  const match = path.matchPathname(
335
- this.basepath,
336
361
  next.pathname,
337
362
  {
338
363
  to: d.from,
@@ -358,8 +383,7 @@ class RouterCore {
358
383
  }
359
384
  }
360
385
  if (maskedNext) {
361
- const maskedFinal = build(maskedDest);
362
- next.maskedLocation = maskedFinal;
386
+ next.maskedLocation = maskedNext;
363
387
  }
364
388
  return next;
365
389
  };
@@ -393,7 +417,7 @@ class RouterCore {
393
417
  });
394
418
  return isEqual;
395
419
  };
396
- const isSameUrl = this.latestLocation.href === next.href;
420
+ const isSameUrl = path.trimPathRight(this.latestLocation.href) === path.trimPathRight(next.href);
397
421
  const previousCommitPromise = this.commitLocationPromise;
398
422
  this.commitLocationPromise = utils.createControlledPromise(() => {
399
423
  previousCommitPromise?.resolve();
@@ -429,7 +453,7 @@ class RouterCore {
429
453
  nextHistory.state.__hashScrollIntoViewOptions = hashScrollIntoView ?? this.options.defaultHashScrollIntoView ?? true;
430
454
  this.shouldViewTransition = viewTransition;
431
455
  this.history[next.replace ? "replace" : "push"](
432
- nextHistory.href,
456
+ nextHistory.publicHref,
433
457
  nextHistory.state,
434
458
  { ignoreBlocker }
435
459
  );
@@ -482,7 +506,7 @@ class RouterCore {
482
506
  if (reloadDocument) {
483
507
  if (!href) {
484
508
  const location = this.buildLocation({ to, ...rest });
485
- href = this.history.createHref(location.href);
509
+ href = location.href;
486
510
  }
487
511
  if (rest.replace) {
488
512
  window.location.replace(href);
@@ -829,7 +853,6 @@ class RouterCore {
829
853
  const pending = opts?.pending === void 0 ? !this.state.isLoading : opts.pending;
830
854
  const baseLocation = pending ? this.latestLocation : this.state.resolvedLocation || this.state.location;
831
855
  const match = path.matchPathname(
832
- this.basepath,
833
856
  baseLocation.pathname,
834
857
  {
835
858
  ...opts,
@@ -1007,7 +1030,7 @@ class RouterCore {
1007
1030
  routeId: route.id,
1008
1031
  params: previousMatch ? utils.replaceEqualDeep(previousMatch.params, routeParams) : routeParams,
1009
1032
  _strictParams: usedParams,
1010
- pathname: path.joinPaths([this.basepath, interpolatedPath]),
1033
+ pathname: interpolatedPath,
1011
1034
  updatedAt: Date.now(),
1012
1035
  search: previousMatch ? utils.replaceEqualDeep(previousMatch.search, preMatchSearch) : preMatchSearch,
1013
1036
  _strictSearch: strictMatchSearch,
@@ -1259,7 +1282,6 @@ function processRouteTree({
1259
1282
  function getMatchedRoutes({
1260
1283
  pathname,
1261
1284
  routePathname,
1262
- basepath,
1263
1285
  caseSensitive,
1264
1286
  routesByPath,
1265
1287
  routesById,
@@ -1270,7 +1292,6 @@ function getMatchedRoutes({
1270
1292
  const trimmedPath = path.trimPathRight(pathname);
1271
1293
  const getMatchedParams = (route) => {
1272
1294
  const result = path.matchPathname(
1273
- basepath,
1274
1295
  trimmedPath,
1275
1296
  {
1276
1297
  to: route.fullPath,