@tanstack/react-router 0.0.1-beta.231 → 0.0.1-beta.232
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/build/cjs/RouterProvider.js +32 -36
- package/build/cjs/RouterProvider.js.map +1 -1
- package/build/cjs/index.js +3 -3
- package/build/cjs/router.js +67 -69
- package/build/cjs/router.js.map +1 -1
- package/build/esm/index.js +93 -99
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +287 -287
- package/build/types/RouterProvider.d.ts +0 -5
- package/build/types/router.d.ts +14 -2
- package/build/umd/index.development.js +92 -98
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +2 -2
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/RouterProvider.tsx +39 -45
- package/src/router.ts +101 -83
package/build/esm/index.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @license MIT
|
|
10
10
|
*/
|
|
11
|
-
import { createBrowserHistory } from '@tanstack/history';
|
|
11
|
+
import { createBrowserHistory, createMemoryHistory } from '@tanstack/history';
|
|
12
12
|
export * from '@tanstack/history';
|
|
13
13
|
import invariant from 'tiny-invariant';
|
|
14
14
|
export { default as invariant } from 'tiny-invariant';
|
|
@@ -797,24 +797,12 @@ const routerContext = /*#__PURE__*/React.createContext(null);
|
|
|
797
797
|
if (typeof document !== 'undefined') {
|
|
798
798
|
window.__TSR_ROUTER_CONTEXT__ = routerContext;
|
|
799
799
|
}
|
|
800
|
-
class SearchParamError extends Error {}
|
|
801
|
-
class PathParamError extends Error {}
|
|
802
|
-
function getInitialRouterState(location) {
|
|
803
|
-
return {
|
|
804
|
-
status: 'idle',
|
|
805
|
-
resolvedLocation: location,
|
|
806
|
-
location,
|
|
807
|
-
matches: [],
|
|
808
|
-
pendingMatches: [],
|
|
809
|
-
lastUpdated: Date.now()
|
|
810
|
-
};
|
|
811
|
-
}
|
|
812
800
|
function RouterProvider({
|
|
813
801
|
router,
|
|
814
802
|
...rest
|
|
815
803
|
}) {
|
|
816
804
|
// Allow the router to update options on the router instance
|
|
817
|
-
router.
|
|
805
|
+
router.update({
|
|
818
806
|
...router.options,
|
|
819
807
|
...rest,
|
|
820
808
|
context: {
|
|
@@ -822,6 +810,17 @@ function RouterProvider({
|
|
|
822
810
|
...rest?.context
|
|
823
811
|
}
|
|
824
812
|
});
|
|
813
|
+
const inner = /*#__PURE__*/React.createElement(RouterProviderInner, {
|
|
814
|
+
router: router
|
|
815
|
+
});
|
|
816
|
+
if (router.options.Wrap) {
|
|
817
|
+
return /*#__PURE__*/React.createElement(router.options.Wrap, null, inner);
|
|
818
|
+
}
|
|
819
|
+
return inner;
|
|
820
|
+
}
|
|
821
|
+
function RouterProviderInner({
|
|
822
|
+
router
|
|
823
|
+
}) {
|
|
825
824
|
const [preState, setState] = React.useState(() => router.state);
|
|
826
825
|
const [isTransitioning, startReactTransition] = React.useTransition();
|
|
827
826
|
const isAnyTransitioning = isTransitioning || preState.matches.some(d => d.status === 'pending');
|
|
@@ -834,18 +833,21 @@ function RouterProvider({
|
|
|
834
833
|
router.setState = setState;
|
|
835
834
|
router.state = state;
|
|
836
835
|
router.startReactTransition = startReactTransition;
|
|
837
|
-
|
|
836
|
+
const tryLoad = () => {
|
|
837
|
+
if (state.location !== router.latestLocation) {
|
|
838
|
+
startReactTransition(() => {
|
|
839
|
+
try {
|
|
840
|
+
router.load();
|
|
841
|
+
} catch (err) {
|
|
842
|
+
console.error(err);
|
|
843
|
+
}
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
useLayoutEffect$1(() => {
|
|
838
848
|
const unsub = router.history.subscribe(() => {
|
|
839
849
|
router.latestLocation = router.parseLocation(router.latestLocation);
|
|
840
|
-
|
|
841
|
-
startReactTransition(() => {
|
|
842
|
-
try {
|
|
843
|
-
router.load();
|
|
844
|
-
} catch (err) {
|
|
845
|
-
console.error(err);
|
|
846
|
-
}
|
|
847
|
-
});
|
|
848
|
-
}
|
|
850
|
+
tryLoad();
|
|
849
851
|
});
|
|
850
852
|
const nextLocation = router.buildLocation({
|
|
851
853
|
search: true,
|
|
@@ -863,7 +865,7 @@ function RouterProvider({
|
|
|
863
865
|
unsub();
|
|
864
866
|
};
|
|
865
867
|
}, [router.history]);
|
|
866
|
-
|
|
868
|
+
useLayoutEffect$1(() => {
|
|
867
869
|
if (!isTransitioning && state.resolvedLocation !== state.location) {
|
|
868
870
|
router.emit({
|
|
869
871
|
type: 'onResolved',
|
|
@@ -878,14 +880,10 @@ function RouterProvider({
|
|
|
878
880
|
}));
|
|
879
881
|
}
|
|
880
882
|
});
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
} catch (err) {
|
|
886
|
-
console.error(err);
|
|
887
|
-
}
|
|
888
|
-
});
|
|
883
|
+
useLayoutEffect$1(() => {
|
|
884
|
+
if (!window.__TSR_DEHYDRATED__) {
|
|
885
|
+
tryLoad();
|
|
886
|
+
}
|
|
889
887
|
}, []);
|
|
890
888
|
return /*#__PURE__*/React.createElement(routerContext.Provider, {
|
|
891
889
|
value: router
|
|
@@ -902,7 +900,7 @@ function useRouterState(opts) {
|
|
|
902
900
|
return opts?.select ? opts.select(state) : state;
|
|
903
901
|
}
|
|
904
902
|
function useRouter() {
|
|
905
|
-
const resolvedContext = window.__TSR_ROUTER_CONTEXT__ || routerContext;
|
|
903
|
+
const resolvedContext = typeof document !== 'undefined' ? window.__TSR_ROUTER_CONTEXT__ || routerContext : routerContext;
|
|
906
904
|
const value = React.useContext(resolvedContext);
|
|
907
905
|
warning(value, 'useRouter must be used inside a <RouterProvider> component!');
|
|
908
906
|
return value;
|
|
@@ -1228,6 +1226,8 @@ function stringifySearchWith(stringify, parser) {
|
|
|
1228
1226
|
};
|
|
1229
1227
|
}
|
|
1230
1228
|
|
|
1229
|
+
// import warning from 'tiny-warning'
|
|
1230
|
+
|
|
1231
1231
|
//
|
|
1232
1232
|
|
|
1233
1233
|
const componentTypes = ['component', 'errorComponent', 'pendingComponent'];
|
|
@@ -1245,7 +1245,7 @@ class Router {
|
|
|
1245
1245
|
// Must build in constructor
|
|
1246
1246
|
|
|
1247
1247
|
constructor(options) {
|
|
1248
|
-
this.
|
|
1248
|
+
this.update({
|
|
1249
1249
|
defaultPreloadDelay: 50,
|
|
1250
1250
|
defaultPendingMs: 1000,
|
|
1251
1251
|
defaultPendingMinMs: 500,
|
|
@@ -1255,20 +1255,22 @@ class Router {
|
|
|
1255
1255
|
parseSearch: options?.parseSearch ?? defaultParseSearch
|
|
1256
1256
|
});
|
|
1257
1257
|
}
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1258
|
+
|
|
1259
|
+
// These are default implementations that can optionally be overridden
|
|
1260
|
+
// by the router provider once rendered. We provide these so that the
|
|
1261
|
+
// router can be used in a non-react environment if necessary
|
|
1262
|
+
startReactTransition = fn => fn();
|
|
1263
|
+
setState = updater => {
|
|
1264
|
+
this.state = functionalUpdate(updater, this.state);
|
|
1263
1265
|
};
|
|
1264
|
-
|
|
1266
|
+
update = newOptions => {
|
|
1265
1267
|
this.options = {
|
|
1266
1268
|
...this.options,
|
|
1267
1269
|
...newOptions
|
|
1268
1270
|
};
|
|
1269
1271
|
this.basepath = `/${trimPath(newOptions.basepath ?? '') ?? ''}`;
|
|
1270
1272
|
if (!this.history || this.options.history && this.options.history !== this.history) {
|
|
1271
|
-
this.history = this.options.history ?? createBrowserHistory();
|
|
1273
|
+
this.history = this.options.history ?? (typeof document !== 'undefined' ? createBrowserHistory() : createMemoryHistory());
|
|
1272
1274
|
this.latestLocation = this.parseLocation();
|
|
1273
1275
|
}
|
|
1274
1276
|
if (this.options.routeTree !== this.routeTree) {
|
|
@@ -2026,6 +2028,7 @@ class Router {
|
|
|
2026
2028
|
// ...s,
|
|
2027
2029
|
// status: 'idle',
|
|
2028
2030
|
// resolvedLocation: s.location,
|
|
2031
|
+
// matches,
|
|
2029
2032
|
// }))
|
|
2030
2033
|
|
|
2031
2034
|
//
|
|
@@ -2226,63 +2229,42 @@ class Router {
|
|
|
2226
2229
|
}
|
|
2227
2230
|
return undefined;
|
|
2228
2231
|
};
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
// invariant(
|
|
2266
|
-
// dehydratedMatch,
|
|
2267
|
-
// `Could not find a client-side match for dehydrated match with id: ${match.id}!`,
|
|
2268
|
-
// )
|
|
2269
|
-
|
|
2270
|
-
// if (dehydratedMatch) {
|
|
2271
|
-
// return {
|
|
2272
|
-
// ...match,
|
|
2273
|
-
// ...dehydratedMatch,
|
|
2274
|
-
// }
|
|
2275
|
-
// }
|
|
2276
|
-
// return match
|
|
2277
|
-
// })
|
|
2278
|
-
|
|
2279
|
-
// this.setState((s) => {
|
|
2280
|
-
// return {
|
|
2281
|
-
// ...s,
|
|
2282
|
-
// matches: dehydratedState.dehydratedMatches as any,
|
|
2283
|
-
// }
|
|
2284
|
-
// })
|
|
2285
|
-
// }
|
|
2232
|
+
dehydrate = () => {
|
|
2233
|
+
return {
|
|
2234
|
+
state: {
|
|
2235
|
+
dehydratedMatches: this.state.matches.map(d => pick(d, ['fetchedAt', 'invalid', 'id', 'status', 'updatedAt', 'loaderData']))
|
|
2236
|
+
}
|
|
2237
|
+
};
|
|
2238
|
+
};
|
|
2239
|
+
hydrate = async __do_not_use_server_ctx => {
|
|
2240
|
+
let _ctx = __do_not_use_server_ctx;
|
|
2241
|
+
// Client hydrates from window
|
|
2242
|
+
if (typeof document !== 'undefined') {
|
|
2243
|
+
_ctx = window.__TSR_DEHYDRATED__;
|
|
2244
|
+
}
|
|
2245
|
+
invariant(_ctx, 'Expected to find a __TSR_DEHYDRATED__ property on window... but we did not. Did you forget to render <DehydrateRouter /> in your app?');
|
|
2246
|
+
const ctx = _ctx;
|
|
2247
|
+
this.dehydratedData = ctx.payload;
|
|
2248
|
+
this.options.hydrate?.(ctx.payload);
|
|
2249
|
+
const dehydratedState = ctx.router.state;
|
|
2250
|
+
let matches = this.matchRoutes(this.state.location.pathname, this.state.location.search).map(match => {
|
|
2251
|
+
const dehydratedMatch = dehydratedState.dehydratedMatches.find(d => d.id === match.id);
|
|
2252
|
+
invariant(dehydratedMatch, `Could not find a client-side match for dehydrated match with id: ${match.id}!`);
|
|
2253
|
+
if (dehydratedMatch) {
|
|
2254
|
+
return {
|
|
2255
|
+
...match,
|
|
2256
|
+
...dehydratedMatch
|
|
2257
|
+
};
|
|
2258
|
+
}
|
|
2259
|
+
return match;
|
|
2260
|
+
});
|
|
2261
|
+
this.setState(s => {
|
|
2262
|
+
return {
|
|
2263
|
+
...s,
|
|
2264
|
+
matches: matches
|
|
2265
|
+
};
|
|
2266
|
+
});
|
|
2267
|
+
};
|
|
2286
2268
|
|
|
2287
2269
|
// resolveMatchPromise = (matchId: string, key: string, value: any) => {
|
|
2288
2270
|
// state.matches
|
|
@@ -2303,6 +2285,18 @@ function lazyFn(fn, key) {
|
|
|
2303
2285
|
function isCtrlEvent(e) {
|
|
2304
2286
|
return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
|
|
2305
2287
|
}
|
|
2288
|
+
class SearchParamError extends Error {}
|
|
2289
|
+
class PathParamError extends Error {}
|
|
2290
|
+
function getInitialRouterState(location) {
|
|
2291
|
+
return {
|
|
2292
|
+
status: 'idle',
|
|
2293
|
+
resolvedLocation: location,
|
|
2294
|
+
location,
|
|
2295
|
+
matches: [],
|
|
2296
|
+
pendingMatches: [],
|
|
2297
|
+
lastUpdated: Date.now()
|
|
2298
|
+
};
|
|
2299
|
+
}
|
|
2306
2300
|
|
|
2307
2301
|
const useLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
|
|
2308
2302
|
const windowKey = 'window';
|