@tanstack/react-router 0.0.1-beta.215 → 0.0.1-beta.216

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/react-router",
3
3
  "author": "Tanner Linsley",
4
- "version": "0.0.1-beta.215",
4
+ "version": "0.0.1-beta.216",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
7
  "homepage": "https://tanstack.com/router",
@@ -42,7 +42,7 @@
42
42
  "@babel/runtime": "^7.16.7",
43
43
  "tiny-invariant": "^1.3.1",
44
44
  "tiny-warning": "^1.0.3",
45
- "@tanstack/history": "0.0.1-beta.215"
45
+ "@tanstack/history": "0.0.1-beta.216"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "rollup --config rollup.config.js"
@@ -27,7 +27,13 @@ import {
27
27
  trimPathRight,
28
28
  } from './path'
29
29
  import { isRedirect } from './redirects'
30
- import { AnyPathParams, AnyRoute, AnySearchSchema, Route } from './route'
30
+ import {
31
+ AnyPathParams,
32
+ AnyRoute,
33
+ AnySearchSchema,
34
+ LoaderFnContext,
35
+ Route,
36
+ } from './route'
31
37
  import {
32
38
  FullSearchSchema,
33
39
  ParseRoute,
@@ -960,22 +966,11 @@ export function RouterProvider<
960
966
  if (match.isFetching) {
961
967
  loadPromise = getRouteMatch(state, match.id)?.loadPromise
962
968
  } else {
963
- matches[index] = match = {
964
- ...match,
965
- isFetching: true,
966
- }
969
+ const cause = state.matches.find((d) => d.id === match.id)
970
+ ? 'stay'
971
+ : 'enter'
967
972
 
968
- const componentsPromise = Promise.all(
969
- componentTypes.map(async (type) => {
970
- const component = route.options[type]
971
-
972
- if ((component as any)?.preload) {
973
- await (component as any).preload()
974
- }
975
- }),
976
- )
977
-
978
- const loaderPromise = route.options.loader?.({
973
+ const loaderContext: LoaderFnContext = {
979
974
  params: match.params,
980
975
  search: match.search,
981
976
  preload: !!preload,
@@ -985,12 +980,42 @@ export function RouterProvider<
985
980
  location: state.location,
986
981
  navigate: (opts) =>
987
982
  navigate({ ...opts, from: match.pathname } as any),
988
- })
983
+ cause,
984
+ }
985
+
986
+ // Default to reloading the route all the time
987
+ const shouldReload =
988
+ route.options.shouldReload?.(loaderContext) ?? true
989
989
 
990
- loadPromise = Promise.all([
991
- componentsPromise,
992
- loaderPromise,
993
- ]).then((d) => d[1])
990
+ // If the user doesn't want the route to reload, just
991
+ // resolve with the existing loader data
992
+
993
+ if (!shouldReload) {
994
+ loadPromise = Promise.resolve(match.loaderData)
995
+ } else {
996
+ // Otherwise, load the route
997
+ matches[index] = match = {
998
+ ...match,
999
+ isFetching: true,
1000
+ }
1001
+
1002
+ const componentsPromise = Promise.all(
1003
+ componentTypes.map(async (type) => {
1004
+ const component = route.options[type]
1005
+
1006
+ if ((component as any)?.preload) {
1007
+ await (component as any).preload()
1008
+ }
1009
+ }),
1010
+ )
1011
+
1012
+ const loaderPromise = route.options.loader?.(loaderContext)
1013
+
1014
+ loadPromise = Promise.all([
1015
+ componentsPromise,
1016
+ loaderPromise,
1017
+ ]).then((d) => d[1])
1018
+ }
994
1019
  }
995
1020
 
996
1021
  matches[index] = match = {
package/src/route.ts CHANGED
@@ -108,6 +108,14 @@ export type BaseRouteOptions<
108
108
  > = RoutePathOptions<TCustomId, TPath> & {
109
109
  getParentRoute: () => TParentRoute
110
110
  validateSearch?: SearchSchemaValidator<TSearchSchema>
111
+ shouldReload?: (
112
+ match: LoaderFnContext<
113
+ TAllParams,
114
+ TFullSearchSchema,
115
+ TAllContext,
116
+ TRouteContext
117
+ >,
118
+ ) => any
111
119
  } & (keyof PickRequired<RouteContext> extends never
112
120
  ? // This async function is called before a route is loaded.
113
121
  // If an error is thrown here, the route's loader will not be called.
@@ -268,9 +276,7 @@ export type RouteLoadFn<
268
276
  TFullSearchSchema,
269
277
  TAllContext,
270
278
  TRouteContext
271
- > & {
272
- parentMatchPromise?: Promise<void>
273
- },
279
+ >,
274
280
  ) => Promise<TLoaderData> | TLoaderData
275
281
 
276
282
  export interface LoaderFnContext<
@@ -286,6 +292,8 @@ export interface LoaderFnContext<
286
292
  context: Expand<Assign<TAllContext, TRouteContext>>
287
293
  location: ParsedLocation<TFullSearchSchema>
288
294
  navigate: (opts: NavigateOptions<AnyRoute>) => Promise<void>
295
+ parentMatchPromise?: Promise<void>
296
+ cause: 'enter' | 'stay'
289
297
  }
290
298
 
291
299
  export type SearchFilter<T, U = T> = (prev: T) => U