@tanstack/router-core 0.0.1-beta.194 → 0.0.1-beta.195

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.
@@ -11,34 +11,34 @@ export interface RouteMeta {
11
11
  }
12
12
  export interface RouteContext {
13
13
  }
14
- export interface RegisterRouteComponent<TLoader = unknown, TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TRouteContext extends Record<string, any> = AnyContext, TAllContext extends Record<string, any> = AnyContext> {
14
+ export interface RegisterRouteComponent<TLoader = unknown, TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TAllContext extends Record<string, any> = AnyContext> {
15
15
  }
16
- export interface RegisterErrorRouteComponent<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TRouteContext extends Record<string, any> = AnyContext, TAllContext extends Record<string, any> = AnyContext> {
16
+ export interface RegisterErrorRouteComponent<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TAllContext extends Record<string, any> = AnyContext> {
17
17
  }
18
- export interface RegisterPendingRouteComponent<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TRouteContext extends Record<string, any> = AnyContext, TAllContext extends Record<string, any> = AnyContext> {
18
+ export interface RegisterPendingRouteComponent<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TAllContext extends Record<string, any> = AnyContext> {
19
19
  }
20
- export interface RegisterRouteProps<TLoader = unknown, TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TRouteContext extends Record<string, any> = AnyContext, TAllContext extends Record<string, any> = AnyContext> {
20
+ export interface RegisterRouteProps<TLoader = unknown, TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TAllContext extends Record<string, any> = AnyContext> {
21
21
  }
22
- export interface RegisterErrorRouteProps<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TRouteContext extends Record<string, any> = AnyContext, TAllContext extends Record<string, any> = AnyContext> {
22
+ export interface RegisterErrorRouteProps<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TAllContext extends Record<string, any> = AnyContext> {
23
23
  }
24
- export interface RegisterPendingRouteProps<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TRouteContext extends Record<string, any> = AnyContext, TAllContext extends Record<string, any> = AnyContext> {
24
+ export interface RegisterPendingRouteProps<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TAllContext extends Record<string, any> = AnyContext> {
25
25
  }
26
- export type RegisteredRouteComponent<TLoader = unknown, TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TRouteContext extends Record<string, any> = AnyContext, TAllContext extends Record<string, any> = AnyContext> = RegisterRouteComponent<TLoader, TFullSearchSchema, TAllParams, TRouteContext, TAllContext> extends {
26
+ export type RegisteredRouteComponent<TLoader = unknown, TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TAllContext extends Record<string, any> = AnyContext> = RegisterRouteComponent<TLoader, TFullSearchSchema, TAllParams, TAllContext> extends {
27
27
  RouteComponent: infer T;
28
28
  } ? T : () => unknown;
29
- export type RegisteredErrorRouteComponent<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TRouteContext extends Record<string, any> = AnyContext, TAllContext extends Record<string, any> = AnyContext> = RegisterErrorRouteComponent<TFullSearchSchema, TAllParams, TRouteContext, TAllContext> extends {
29
+ export type RegisteredErrorRouteComponent<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TAllContext extends Record<string, any> = AnyContext> = RegisterErrorRouteComponent<TFullSearchSchema, TAllParams, TAllContext> extends {
30
30
  ErrorRouteComponent: infer T;
31
31
  } ? T : () => unknown;
32
- export type RegisteredPendingRouteComponent<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TRouteContext extends Record<string, any> = AnyContext, TAllContext extends Record<string, any> = AnyContext> = RegisterPendingRouteComponent<TFullSearchSchema, TAllParams, TRouteContext, TAllContext> extends {
32
+ export type RegisteredPendingRouteComponent<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TAllContext extends Record<string, any> = AnyContext> = RegisterPendingRouteComponent<TFullSearchSchema, TAllParams, TAllContext> extends {
33
33
  PendingRouteComponent: infer T;
34
34
  } ? T : () => unknown;
35
- export type RegisteredRouteProps<TLoader = unknown, TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TRouteContext extends Record<string, any> = AnyContext, TAllContext extends Record<string, any> = AnyContext> = RegisterRouteProps<TLoader, TFullSearchSchema, TAllParams, TRouteContext, TAllContext> extends {
35
+ export type RegisteredRouteProps<TLoader = unknown, TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TAllContext extends Record<string, any> = AnyContext> = RegisterRouteProps<TLoader, TFullSearchSchema, TAllParams, TAllContext> extends {
36
36
  RouteProps: infer T;
37
37
  } ? T : {};
38
- export type RegisteredErrorRouteProps<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TRouteContext extends Record<string, any> = AnyContext, TAllContext extends Record<string, any> = AnyContext> = RegisterRouteProps<TFullSearchSchema, TAllParams, TRouteContext, TAllContext> extends {
38
+ export type RegisteredErrorRouteProps<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TAllContext extends Record<string, any> = AnyContext> = RegisterRouteProps<TFullSearchSchema, TAllParams, TAllContext> extends {
39
39
  ErrorRouteProps: infer T;
40
40
  } ? T : {};
41
- export type RegisteredPendingRouteProps<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TRouteContext extends Record<string, any> = AnyContext, TAllContext extends Record<string, any> = AnyContext> = RegisterRouteProps<TFullSearchSchema, TAllParams, TRouteContext, TAllContext> extends {
41
+ export type RegisteredPendingRouteProps<TFullSearchSchema extends Record<string, any> = AnySearchSchema, TAllParams extends AnyPathParams = AnyPathParams, TAllContext extends Record<string, any> = AnyContext> = RegisterRouteProps<TFullSearchSchema, TAllParams, TAllContext> extends {
42
42
  PendingRouteProps: infer T;
43
43
  } ? T : {};
44
44
  export type PreloadableObj = {
@@ -55,8 +55,8 @@ export type MetaOptions = keyof PickRequired<RouteMeta> extends never ? {
55
55
  } : {
56
56
  meta: RouteMeta;
57
57
  };
58
- export type AnyRouteProps = RegisteredRouteProps<any, any, any, any, any>;
59
- export type RouteOptions<TParentRoute extends AnyRoute = AnyRoute, TCustomId extends string = string, TPath extends string = string, TLoaderContext extends Record<string, any> = AnyContext, TLoader = unknown, TSearchSchema extends Record<string, any> = {}, TFullSearchSchema extends Record<string, any> = TSearchSchema, TParams extends AnyPathParams = AnyPathParams, TAllParams extends AnyPathParams = TParams, TRouteContext extends RouteContext = RouteContext, TAllContext extends Record<string, any> = AnyContext> = BaseRouteOptions<TParentRoute, TCustomId, TPath, TLoaderContext, TLoader, TSearchSchema, TFullSearchSchema, TParams, TAllParams, TRouteContext, TAllContext> & UpdatableRouteOptions<TLoader, TSearchSchema, TFullSearchSchema, TAllParams, TRouteContext, TAllContext>;
58
+ export type AnyRouteProps = RegisteredRouteProps<any, any, any, any>;
59
+ export type RouteOptions<TParentRoute extends AnyRoute = AnyRoute, TCustomId extends string = string, TPath extends string = string, TLoaderContext extends Record<string, any> = AnyContext, TLoader = unknown, TSearchSchema extends Record<string, any> = {}, TFullSearchSchema extends Record<string, any> = TSearchSchema, TParams extends AnyPathParams = AnyPathParams, TAllParams extends AnyPathParams = TParams, TRouteContext extends RouteContext = RouteContext, TAllContext extends Record<string, any> = AnyContext> = BaseRouteOptions<TParentRoute, TCustomId, TPath, TLoaderContext, TLoader, TSearchSchema, TFullSearchSchema, TParams, TAllParams, TRouteContext, TAllContext> & UpdatableRouteOptions<TLoader, TFullSearchSchema, TAllParams, TAllContext>;
60
60
  export type ParamsFallback<TPath extends string, TParams> = unknown extends TParams ? Record<ParsePathParams<TPath>, string> : TParams;
61
61
  export type BaseRouteOptions<TParentRoute extends AnyRoute = AnyRoute, TCustomId extends string = string, TPath extends string = string, TLoaderContext extends Record<string, any> = AnyContext, TLoader = unknown, TSearchSchema extends Record<string, any> = {}, TFullSearchSchema extends Record<string, any> = TSearchSchema, TParams extends AnyPathParams = {}, TAllParams = ParamsFallback<TPath, TParams>, TRouteContext extends RouteContext = RouteContext, TAllContext extends Record<string, any> = AnyContext> = RoutePathOptions<TCustomId, TPath> & {
62
62
  getParentRoute: () => TParentRoute;
@@ -85,12 +85,12 @@ type BeforeLoadFn<TParentRoute extends AnyRoute, TAllParams, TLoaderContext, TRo
85
85
  params: TAllParams;
86
86
  context: Expand<TParentRoute['types']['context'] & TLoaderContext>;
87
87
  }) => Promise<TRouteContext> | TRouteContext | void;
88
- export type UpdatableRouteOptions<TLoader, TSearchSchema extends Record<string, any>, TFullSearchSchema extends Record<string, any>, TAllParams extends AnyPathParams, TRouteContext extends Record<string, any>, TAllContext extends Record<string, any>> = MetaOptions & {
88
+ export type UpdatableRouteOptions<TLoader, TFullSearchSchema extends Record<string, any>, TAllParams extends AnyPathParams, TAllContext extends Record<string, any>> = MetaOptions & {
89
89
  caseSensitive?: boolean;
90
90
  wrapInSuspense?: boolean;
91
- component?: RegisteredRouteComponent<TLoader, TFullSearchSchema, TAllParams, TRouteContext, TAllContext>;
92
- errorComponent?: RegisteredErrorRouteComponent<TFullSearchSchema, TAllParams, TRouteContext, TAllContext>;
93
- pendingComponent?: RegisteredPendingRouteComponent<TFullSearchSchema, TAllParams, TRouteContext, TAllContext>;
91
+ component?: RegisteredRouteComponent<TLoader, TFullSearchSchema, TAllParams, TAllContext>;
92
+ errorComponent?: RegisteredErrorRouteComponent<TFullSearchSchema, TAllParams, TAllContext>;
93
+ pendingComponent?: RegisteredPendingRouteComponent<TFullSearchSchema, TAllParams, TAllContext>;
94
94
  preSearchFilters?: SearchFilter<TFullSearchSchema>[];
95
95
  postSearchFilters?: SearchFilter<TFullSearchSchema>[];
96
96
  preloadMaxAge?: number;
@@ -123,7 +123,7 @@ export interface LoaderContext<TAllParams = {}, TLoaderContext = {}, TAllContext
123
123
  abortController: AbortController;
124
124
  preload: boolean;
125
125
  params: TAllParams;
126
- context: DeepMergeAll<[TAllContext, TLoaderContext, TRouteContext]>;
126
+ context: Expand<DeepMergeAll<[TAllContext, TLoaderContext, TRouteContext]>>;
127
127
  }
128
128
  export type SearchFilter<T, U = T> = (prev: T) => U;
129
129
  export type ResolveId<TParentRoute, TCustomId extends string, TPath extends string> = TParentRoute extends {
@@ -198,13 +198,13 @@ export declare class Route<TParentRoute extends RouteConstraints['TParentRoute']
198
198
  originalIndex?: number;
199
199
  router?: AnyRouter;
200
200
  rank: number;
201
- constructor(options: RouteOptions<TParentRoute, TCustomId, TPath, TLoaderContext, TLoader, TSearchSchema, TFullSearchSchema, TParams, TAllParams, TRouteContext, TAllContext> & UpdatableRouteOptions<TLoader, TSearchSchema, TFullSearchSchema, TAllParams, TRouteContext, TAllContext>);
201
+ constructor(options: RouteOptions<TParentRoute, TCustomId, TPath, TLoaderContext, TLoader, TSearchSchema, TFullSearchSchema, TParams, TAllParams, TRouteContext, TAllContext> & UpdatableRouteOptions<TLoader, TFullSearchSchema, TAllParams, TAllContext>);
202
202
  init: (opts: {
203
203
  originalIndex: number;
204
204
  router: AnyRouter;
205
205
  }) => void;
206
206
  addChildren: <TNewChildren extends AnyRoute[]>(children: TNewChildren) => Route<TParentRoute, TPath, TFullPath, TCustomId, TId, TLoaderContext, TLoader, TSearchSchema, TFullSearchSchema, TParams, TAllParams, TRouteContext, TAllContext, TRouterContext, TNewChildren, TRouteTree>;
207
- update: (options: UpdatableRouteOptions<TLoader, TSearchSchema, TFullSearchSchema, TAllParams, TRouteContext, TAllContext>) => this;
207
+ update: (options: UpdatableRouteOptions<TLoader, TFullSearchSchema, TAllParams, TAllContext>) => this;
208
208
  static __onInit: (route: any) => void;
209
209
  }
210
210
  export type AnyRootRoute = RootRoute<any, any, any, any>;
@@ -18,5 +18,5 @@ export type RouteByPath<TRouteTree extends AnyRoute, TPath> = Extract<ParseRoute
18
18
  fullPath: TPath;
19
19
  }>;
20
20
  export type RoutePaths<TRouteTree extends AnyRoute> = ParseRoute<TRouteTree>['fullPath'] | '/';
21
- export type FullSearchSchema<TRouteTree extends AnyRoute> = Partial<Expand<UnionToIntersection<ParseRoute<TRouteTree>['types']['fullSearchSchema']> & {}>>;
21
+ export type FullSearchSchema<TRouteTree extends AnyRoute> = Partial<Expand<UnionToIntersection<ParseRoute<TRouteTree>['types']['fullSearchSchema']>>>;
22
22
  export type AllParams<TRouteTree extends AnyRoute> = Expand<UnionToIntersection<ParseRoute<TRouteTree>['types']['allParams']>>;
@@ -75,9 +75,9 @@ export interface RouterOptions<TRouteTree extends AnyRoute, TDehydrated extends
75
75
  defaultPreload?: false | 'intent';
76
76
  defaultPreloadDelay?: number;
77
77
  reloadOnWindowFocus?: boolean;
78
- defaultComponent?: RegisteredRouteComponent<unknown, AnySearchSchema, AnyPathParams, AnyContext, AnyContext>;
79
- defaultErrorComponent?: RegisteredErrorRouteComponent<AnySearchSchema, AnyPathParams, AnyContext, AnyContext>;
80
- defaultPendingComponent?: RegisteredPendingRouteComponent<AnySearchSchema, AnyPathParams, AnyContext, AnyContext>;
78
+ defaultComponent?: RegisteredRouteComponent<unknown, AnySearchSchema, AnyPathParams, AnyContext>;
79
+ defaultErrorComponent?: RegisteredErrorRouteComponent<AnySearchSchema, AnyPathParams, AnyContext>;
80
+ defaultPendingComponent?: RegisteredPendingRouteComponent<AnySearchSchema, AnyPathParams, AnyContext>;
81
81
  defaultMaxAge?: number;
82
82
  defaultGcMaxAge?: number;
83
83
  defaultPreloadMaxAge?: number;
@@ -107,13 +107,13 @@
107
107
  push: (path, state) => {
108
108
  assignKey(state);
109
109
  queueTask(() => {
110
- opts.pushState(path, state);
110
+ opts.pushState(path, state, onUpdate);
111
111
  });
112
112
  },
113
113
  replace: (path, state) => {
114
114
  assignKey(state);
115
115
  queueTask(() => {
116
- opts.replaceState(path, state);
116
+ opts.replaceState(path, state, onUpdate);
117
117
  });
118
118
  },
119
119
  go: index => {
@@ -145,7 +145,8 @@
145
145
  stopBlocking();
146
146
  }
147
147
  };
148
- }
148
+ },
149
+ flush: () => opts.flush?.()
149
150
  };
150
151
  }
151
152
  function assignKey(state) {
@@ -158,25 +159,100 @@
158
159
  // }
159
160
  }
160
161
 
162
+ /**
163
+ * Creates a history object that can be used to interact with the browser's
164
+ * navigation. This is a lightweight API wrapping the browser's native methods.
165
+ * It is designed to work with TanStack Router, but could be used as a standalone API as well.
166
+ * IMPORTANT: This API implements history throttling via a microtask to prevent
167
+ * excessive calls to the history API. In some browsers, calling history.pushState or
168
+ * history.replaceState in quick succession can cause the browser to ignore subsequent
169
+ * calls. This API smooths out those differences and ensures that your application
170
+ * state will *eventually* match the browser state. In most cases, this is not a problem,
171
+ * but if you need to ensure that the browser state is up to date, you can use the
172
+ * `history.flush` method to immediately flush all pending state changes to the browser URL.
173
+ * @param opts
174
+ * @param opts.getHref A function that returns the current href (path + search + hash)
175
+ * @param opts.createHref A function that takes a path and returns a href (path + search + hash)
176
+ * @returns A history instance
177
+ */
161
178
  function createBrowserHistory(opts) {
162
179
  const getHref = opts?.getHref ?? (() => `${window.location.pathname}${window.location.search}${window.location.hash}`);
163
180
  const createHref = opts?.createHref ?? (path => path);
164
- const getLocation = () => parseLocation(getHref(), window.history.state);
181
+ let currentLocation = parseLocation(getHref(), window.history.state);
182
+ const getLocation = () => currentLocation;
183
+ let next;
184
+
185
+ // Because we are proactively updating the location
186
+ // in memory before actually updating the browser history,
187
+ // we need to track when we are doing this so we don't
188
+ // notify subscribers twice on the last update.
189
+ let tracking = true;
190
+
191
+ // We need to track the current scheduled update to prevent
192
+ // multiple updates from being scheduled at the same time.
193
+ let scheduled;
194
+
195
+ // This function is a wrapper to prevent any of the callback's
196
+ // side effects from causing a subscriber notification
197
+ const untrack = fn => {
198
+ tracking = false;
199
+ fn();
200
+ tracking = true;
201
+ };
202
+
203
+ // This function flushes the next update to the browser history
204
+ const flush = () => {
205
+ // Do not notify subscribers about this push/replace call
206
+ untrack(() => {
207
+ if (!next) return;
208
+ window.history[next.isPush ? 'pushState' : 'replaceState'](next.state, '', next.href);
209
+ // Reset the nextIsPush flag and clear the scheduled update
210
+ next = undefined;
211
+ scheduled = undefined;
212
+ });
213
+ };
214
+
215
+ // This function queues up a call to update the browser history
216
+ const queueHistoryAction = (type, path, state, onUpdate) => {
217
+ const href = createHref(path);
218
+
219
+ // Update the location in memory
220
+ currentLocation = parseLocation(href, state);
221
+
222
+ // Keep track of the next location we need to flush to the URL
223
+ next = {
224
+ href,
225
+ state,
226
+ isPush: next?.isPush || type === 'push'
227
+ };
228
+ // Notify subscribers
229
+ onUpdate();
230
+ if (!scheduled) {
231
+ // Schedule an update to the browser history
232
+ scheduled = Promise.resolve().then(() => flush());
233
+ }
234
+ };
165
235
  return createHistory({
166
236
  getLocation,
167
237
  subscriber: onUpdate => {
168
- window.addEventListener(pushStateEvent, onUpdate);
169
- window.addEventListener(popStateEvent, onUpdate);
238
+ window.addEventListener(pushStateEvent, () => {
239
+ currentLocation = parseLocation(getHref(), window.history.state);
240
+ onUpdate();
241
+ });
242
+ window.addEventListener(popStateEvent, () => {
243
+ currentLocation = parseLocation(getHref(), window.history.state);
244
+ onUpdate();
245
+ });
170
246
  var pushState = window.history.pushState;
171
247
  window.history.pushState = function () {
172
248
  let res = pushState.apply(history, arguments);
173
- onUpdate();
249
+ if (tracking) onUpdate();
174
250
  return res;
175
251
  };
176
252
  var replaceState = window.history.replaceState;
177
253
  window.history.replaceState = function () {
178
254
  let res = replaceState.apply(history, arguments);
179
- onUpdate();
255
+ if (tracking) onUpdate();
180
256
  return res;
181
257
  };
182
258
  return () => {
@@ -186,16 +262,13 @@
186
262
  window.removeEventListener(popStateEvent, onUpdate);
187
263
  };
188
264
  },
189
- pushState: (path, state) => {
190
- window.history.pushState(state, '', createHref(path));
191
- },
192
- replaceState: (path, state) => {
193
- window.history.replaceState(state, '', createHref(path));
194
- },
265
+ pushState: (path, state, onUpdate) => queueHistoryAction('push', path, state, onUpdate),
266
+ replaceState: (path, state, onUpdate) => queueHistoryAction('replace', path, state, onUpdate),
195
267
  back: () => window.history.back(),
196
268
  forward: () => window.history.forward(),
197
269
  go: n => window.history.go(n),
198
- createHref: path => createHref(path)
270
+ createHref: path => createHref(path),
271
+ flush
199
272
  });
200
273
  }
201
274
  function createHashHistory() {
@@ -252,6 +325,8 @@
252
325
  return (Math.random() + 1).toString(36).substring(7);
253
326
  }
254
327
 
328
+ // export type Expand<T> = T
329
+
255
330
  // type Compute<T> = { [K in keyof T]: T[K] } | never
256
331
 
257
332
  // type AllKeys<T> = T extends any ? keyof T : never