@tanstack/react-router 0.0.1-beta.231 → 0.0.1-beta.233

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.
@@ -13,6 +13,7 @@
13
13
  var React = require('react');
14
14
  var warning = require('tiny-warning');
15
15
  var Matches = require('./Matches.js');
16
+ var utils = require('./utils.js');
16
17
 
17
18
  function _interopNamespaceDefault(e) {
18
19
  var n = Object.create(null);
@@ -37,24 +38,12 @@ const routerContext = /*#__PURE__*/React__namespace.createContext(null);
37
38
  if (typeof document !== 'undefined') {
38
39
  window.__TSR_ROUTER_CONTEXT__ = routerContext;
39
40
  }
40
- class SearchParamError extends Error {}
41
- class PathParamError extends Error {}
42
- function getInitialRouterState(location) {
43
- return {
44
- status: 'idle',
45
- resolvedLocation: location,
46
- location,
47
- matches: [],
48
- pendingMatches: [],
49
- lastUpdated: Date.now()
50
- };
51
- }
52
41
  function RouterProvider({
53
42
  router,
54
43
  ...rest
55
44
  }) {
56
45
  // Allow the router to update options on the router instance
57
- router.updateOptions({
46
+ router.update({
58
47
  ...router.options,
59
48
  ...rest,
60
49
  context: {
@@ -62,6 +51,17 @@ function RouterProvider({
62
51
  ...rest?.context
63
52
  }
64
53
  });
54
+ const inner = /*#__PURE__*/React__namespace.createElement(RouterProviderInner, {
55
+ router: router
56
+ });
57
+ if (router.options.Wrap) {
58
+ return /*#__PURE__*/React__namespace.createElement(router.options.Wrap, null, inner);
59
+ }
60
+ return inner;
61
+ }
62
+ function RouterProviderInner({
63
+ router
64
+ }) {
65
65
  const [preState, setState] = React__namespace.useState(() => router.state);
66
66
  const [isTransitioning, startReactTransition] = React__namespace.useTransition();
67
67
  const isAnyTransitioning = isTransitioning || preState.matches.some(d => d.status === 'pending');
@@ -74,18 +74,21 @@ function RouterProvider({
74
74
  router.setState = setState;
75
75
  router.state = state;
76
76
  router.startReactTransition = startReactTransition;
77
- React__namespace.useLayoutEffect(() => {
77
+ const tryLoad = () => {
78
+ if (state.location !== router.latestLocation) {
79
+ startReactTransition(() => {
80
+ try {
81
+ router.load();
82
+ } catch (err) {
83
+ console.error(err);
84
+ }
85
+ });
86
+ }
87
+ };
88
+ utils.useLayoutEffect(() => {
78
89
  const unsub = router.history.subscribe(() => {
79
90
  router.latestLocation = router.parseLocation(router.latestLocation);
80
- if (state.location !== router.latestLocation) {
81
- startReactTransition(() => {
82
- try {
83
- router.load();
84
- } catch (err) {
85
- console.error(err);
86
- }
87
- });
88
- }
91
+ tryLoad();
89
92
  });
90
93
  const nextLocation = router.buildLocation({
91
94
  search: true,
@@ -103,7 +106,7 @@ function RouterProvider({
103
106
  unsub();
104
107
  };
105
108
  }, [router.history]);
106
- React__namespace.useLayoutEffect(() => {
109
+ utils.useLayoutEffect(() => {
107
110
  if (!isTransitioning && state.resolvedLocation !== state.location) {
108
111
  router.emit({
109
112
  type: 'onResolved',
@@ -118,14 +121,10 @@ function RouterProvider({
118
121
  }));
119
122
  }
120
123
  });
121
- React__namespace.useLayoutEffect(() => {
122
- startReactTransition(() => {
123
- try {
124
- router.load();
125
- } catch (err) {
126
- console.error(err);
127
- }
128
- });
124
+ utils.useLayoutEffect(() => {
125
+ if (!window.__TSR_DEHYDRATED__) {
126
+ tryLoad();
127
+ }
129
128
  }, []);
130
129
  return /*#__PURE__*/React__namespace.createElement(routerContext.Provider, {
131
130
  value: router
@@ -142,16 +141,13 @@ function useRouterState(opts) {
142
141
  return opts?.select ? opts.select(state) : state;
143
142
  }
144
143
  function useRouter() {
145
- const resolvedContext = window.__TSR_ROUTER_CONTEXT__ || routerContext;
144
+ const resolvedContext = typeof document !== 'undefined' ? window.__TSR_ROUTER_CONTEXT__ || routerContext : routerContext;
146
145
  const value = React__namespace.useContext(resolvedContext);
147
146
  warning(value, 'useRouter must be used inside a <RouterProvider> component!');
148
147
  return value;
149
148
  }
150
149
 
151
- exports.PathParamError = PathParamError;
152
150
  exports.RouterProvider = RouterProvider;
153
- exports.SearchParamError = SearchParamError;
154
- exports.getInitialRouterState = getInitialRouterState;
155
151
  exports.getRouteMatch = getRouteMatch;
156
152
  exports.routerContext = routerContext;
157
153
  exports.useRouter = useRouter;
@@ -1 +1 @@
1
- {"version":3,"file":"RouterProvider.js","sources":["../../src/RouterProvider.tsx"],"sourcesContent":["import * as React from 'react'\nimport warning from 'tiny-warning'\nimport { Matches } from './Matches'\nimport {\n LinkInfo,\n LinkOptions,\n NavigateOptions,\n ResolveRelativePath,\n ToOptions,\n} from './link'\nimport { ParsedLocation } from './location'\nimport { AnyRoute } from './route'\nimport { RouteById, RoutePaths } from './routeInfo'\nimport {\n BuildNextOptions,\n RegisteredRouter,\n Router,\n RouterOptions,\n RouterState,\n} from './router'\nimport { NoInfer, PickAsRequired } from './utils'\nimport { MatchRouteOptions } from './Matches'\nimport { RouteMatch } from './Matches'\n\nexport interface CommitLocationOptions {\n replace?: boolean\n resetScroll?: boolean\n startTransition?: boolean\n}\n\nexport interface MatchLocation {\n to?: string | number | null\n fuzzy?: boolean\n caseSensitive?: boolean\n from?: string\n}\n\nexport type BuildLinkFn<TRouteTree extends AnyRoute> = <\n TFrom extends RoutePaths<TRouteTree> = '/',\n TTo extends string = '',\n>(\n dest: LinkOptions<TRouteTree, TFrom, TTo>,\n) => LinkInfo\n\nexport type NavigateFn<TRouteTree extends AnyRoute> = <\n TFrom extends RoutePaths<TRouteTree> = '/',\n TTo extends string = '',\n TMaskFrom extends RoutePaths<TRouteTree> = TFrom,\n TMaskTo extends string = '',\n>(\n opts: NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,\n) => Promise<void>\n\nexport type MatchRouteFn<TRouteTree extends AnyRoute> = <\n TFrom extends RoutePaths<TRouteTree> = '/',\n TTo extends string = '',\n TResolved = ResolveRelativePath<TFrom, NoInfer<TTo>>,\n>(\n location: ToOptions<TRouteTree, TFrom, TTo>,\n opts?: MatchRouteOptions,\n) => false | RouteById<TRouteTree, TResolved>['types']['allParams']\n\nexport type BuildLocationFn<TRouteTree extends AnyRoute> = (\n opts: BuildNextOptions,\n) => ParsedLocation\n\nexport type InjectedHtmlEntry = string | (() => Promise<string> | string)\n\nexport const routerContext = React.createContext<Router<any>>(null!)\n\nif (typeof document !== 'undefined') {\n window.__TSR_ROUTER_CONTEXT__ = routerContext as any\n}\n\nexport class SearchParamError extends Error {}\n\nexport class PathParamError extends Error {}\n\nexport function getInitialRouterState(\n location: ParsedLocation,\n): RouterState<any> {\n return {\n status: 'idle',\n resolvedLocation: location,\n location,\n matches: [],\n pendingMatches: [],\n lastUpdated: Date.now(),\n }\n}\n\nexport function RouterProvider<\n TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],\n TDehydrated extends Record<string, any> = Record<string, any>,\n>({ router, ...rest }: RouterProps<TRouteTree, TDehydrated>) {\n // Allow the router to update options on the router instance\n router.updateOptions({\n ...router.options,\n ...rest,\n\n context: {\n ...router.options.context,\n ...rest?.context,\n },\n } as PickAsRequired<\n RouterOptions<TRouteTree, TDehydrated>,\n 'stringifySearch' | 'parseSearch' | 'context'\n >)\n\n const [preState, setState] = React.useState(() => router.state)\n const [isTransitioning, startReactTransition] = React.useTransition()\n const isAnyTransitioning =\n isTransitioning || preState.matches.some((d) => d.status === 'pending')\n\n const state = React.useMemo<RouterState<TRouteTree>>(\n () => ({\n ...preState,\n status: isAnyTransitioning ? 'pending' : 'idle',\n location: isTransitioning ? router.latestLocation : preState.location,\n pendingMatches: router.pendingMatches,\n }),\n [preState, isTransitioning],\n )\n\n router.setState = setState\n router.state = state\n router.startReactTransition = startReactTransition\n\n React.useLayoutEffect(() => {\n const unsub = router.history.subscribe(() => {\n router.latestLocation = router.parseLocation(router.latestLocation)\n\n if (state.location !== router.latestLocation) {\n startReactTransition(() => {\n try {\n router.load()\n } catch (err) {\n console.error(err)\n }\n })\n }\n })\n\n const nextLocation = router.buildLocation({\n search: true,\n params: true,\n hash: true,\n state: true,\n })\n\n if (state.location.href !== nextLocation.href) {\n router.commitLocation({ ...nextLocation, replace: true })\n }\n\n return () => {\n unsub()\n }\n }, [router.history])\n\n React.useLayoutEffect(() => {\n if (!isTransitioning && state.resolvedLocation !== state.location) {\n router.emit({\n type: 'onResolved',\n fromLocation: state.resolvedLocation,\n toLocation: state.location,\n pathChanged: state.location!.href !== state.resolvedLocation?.href,\n })\n router.pendingMatches = []\n\n setState((s) => ({\n ...s,\n resolvedLocation: s.location,\n }))\n }\n })\n\n React.useLayoutEffect(() => {\n startReactTransition(() => {\n try {\n router.load()\n } catch (err) {\n console.error(err)\n }\n })\n }, [])\n\n return (\n <routerContext.Provider value={router}>\n <Matches />\n </routerContext.Provider>\n )\n}\n\nexport function getRouteMatch<TRouteTree extends AnyRoute>(\n state: RouterState<TRouteTree>,\n id: string,\n): undefined | RouteMatch<TRouteTree> {\n return [...state.pendingMatches, ...state.matches].find((d) => d.id === id)\n}\n\nexport function useRouterState<\n TSelected = RouterState<RegisteredRouter['routeTree']>,\n>(opts?: {\n select: (state: RouterState<RegisteredRouter['routeTree']>) => TSelected\n}): TSelected {\n const { state } = useRouter()\n // return useStore(router.__store, opts?.select as any)\n return opts?.select ? opts.select(state) : (state as any)\n}\n\nexport type RouterProps<\n TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],\n TDehydrated extends Record<string, any> = Record<string, any>,\n> = Omit<RouterOptions<TRouteTree, TDehydrated>, 'context'> & {\n router: Router<TRouteTree>\n context?: Partial<RouterOptions<TRouteTree, TDehydrated>['context']>\n}\n\nexport function useRouter<\n TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],\n>(): Router<TRouteTree> {\n const resolvedContext = window.__TSR_ROUTER_CONTEXT__ || routerContext\n const value = React.useContext(resolvedContext)\n warning(value, 'useRouter must be used inside a <RouterProvider> component!')\n return value as any\n}\n"],"names":["routerContext","React","createContext","document","window","__TSR_ROUTER_CONTEXT__","SearchParamError","Error","PathParamError","getInitialRouterState","location","status","resolvedLocation","matches","pendingMatches","lastUpdated","Date","now","RouterProvider","router","rest","updateOptions","options","context","preState","setState","useState","state","isTransitioning","startReactTransition","useTransition","isAnyTransitioning","some","d","useMemo","latestLocation","useLayoutEffect","unsub","history","subscribe","parseLocation","load","err","console","error","nextLocation","buildLocation","search","params","hash","href","commitLocation","replace","emit","type","fromLocation","toLocation","pathChanged","s","createElement","Provider","value","Matches","getRouteMatch","id","find","useRouterState","opts","useRouter","select","resolvedContext","useContext","warning"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoEO,MAAMA,aAAa,gBAAGC,gBAAK,CAACC,aAAa,CAAc,IAAK,EAAC;AAEpE,IAAI,OAAOC,QAAQ,KAAK,WAAW,EAAE;EACnCC,MAAM,CAACC,sBAAsB,GAAGL,aAAoB,CAAA;AACtD,CAAA;AAEO,MAAMM,gBAAgB,SAASC,KAAK,CAAC,EAAA;AAErC,MAAMC,cAAc,SAASD,KAAK,CAAC,EAAA;AAEnC,SAASE,qBAAqBA,CACnCC,QAAwB,EACN;EAClB,OAAO;AACLC,IAAAA,MAAM,EAAE,MAAM;AACdC,IAAAA,gBAAgB,EAAEF,QAAQ;IAC1BA,QAAQ;AACRG,IAAAA,OAAO,EAAE,EAAE;AACXC,IAAAA,cAAc,EAAE,EAAE;AAClBC,IAAAA,WAAW,EAAEC,IAAI,CAACC,GAAG,EAAC;GACvB,CAAA;AACH,CAAA;AAEO,SAASC,cAAcA,CAG5B;EAAEC,MAAM;EAAE,GAAGC,IAAAA;AAA2C,CAAC,EAAE;AAC3D;EACAD,MAAM,CAACE,aAAa,CAAC;IACnB,GAAGF,MAAM,CAACG,OAAO;AACjB,IAAA,GAAGF,IAAI;AAEPG,IAAAA,OAAO,EAAE;AACP,MAAA,GAAGJ,MAAM,CAACG,OAAO,CAACC,OAAO;AACzB,MAAA,GAAGH,IAAI,EAAEG,OAAAA;AACX,KAAA;AACF,GAGC,CAAC,CAAA;AAEF,EAAA,MAAM,CAACC,QAAQ,EAAEC,QAAQ,CAAC,GAAGxB,gBAAK,CAACyB,QAAQ,CAAC,MAAMP,MAAM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../../src/RouterProvider.tsx"],"sourcesContent":["import * as React from 'react'\nimport warning from 'tiny-warning'\nimport { Matches } from './Matches'\nimport {\n LinkInfo,\n LinkOptions,\n NavigateOptions,\n ResolveRelativePath,\n ToOptions,\n} from './link'\nimport { ParsedLocation } from './location'\nimport { AnyRoute } from './route'\nimport { RouteById, RoutePaths } from './routeInfo'\nimport {\n BuildNextOptions,\n RegisteredRouter,\n Router,\n RouterOptions,\n RouterState,\n} from './router'\nimport { NoInfer, PickAsRequired, useLayoutEffect } from './utils'\nimport { MatchRouteOptions } from './Matches'\nimport { RouteMatch } from './Matches'\n\nexport interface CommitLocationOptions {\n replace?: boolean\n resetScroll?: boolean\n startTransition?: boolean\n}\n\nexport interface MatchLocation {\n to?: string | number | null\n fuzzy?: boolean\n caseSensitive?: boolean\n from?: string\n}\n\nexport type BuildLinkFn<TRouteTree extends AnyRoute> = <\n TFrom extends RoutePaths<TRouteTree> = '/',\n TTo extends string = '',\n>(\n dest: LinkOptions<TRouteTree, TFrom, TTo>,\n) => LinkInfo\n\nexport type NavigateFn<TRouteTree extends AnyRoute> = <\n TFrom extends RoutePaths<TRouteTree> = '/',\n TTo extends string = '',\n TMaskFrom extends RoutePaths<TRouteTree> = TFrom,\n TMaskTo extends string = '',\n>(\n opts: NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,\n) => Promise<void>\n\nexport type MatchRouteFn<TRouteTree extends AnyRoute> = <\n TFrom extends RoutePaths<TRouteTree> = '/',\n TTo extends string = '',\n TResolved = ResolveRelativePath<TFrom, NoInfer<TTo>>,\n>(\n location: ToOptions<TRouteTree, TFrom, TTo>,\n opts?: MatchRouteOptions,\n) => false | RouteById<TRouteTree, TResolved>['types']['allParams']\n\nexport type BuildLocationFn<TRouteTree extends AnyRoute> = (\n opts: BuildNextOptions,\n) => ParsedLocation\n\nexport type InjectedHtmlEntry = string | (() => Promise<string> | string)\n\nexport const routerContext = React.createContext<Router<any>>(null!)\n\nif (typeof document !== 'undefined') {\n window.__TSR_ROUTER_CONTEXT__ = routerContext as any\n}\n\nexport function RouterProvider<\n TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],\n TDehydrated extends Record<string, any> = Record<string, any>,\n>({ router, ...rest }: RouterProps<TRouteTree, TDehydrated>) {\n // Allow the router to update options on the router instance\n router.update({\n ...router.options,\n ...rest,\n context: {\n ...router.options.context,\n ...rest?.context,\n },\n } as any)\n\n const inner = <RouterProviderInner<TRouteTree, TDehydrated> router={router} />\n\n if (router.options.Wrap) {\n return <router.options.Wrap>{inner}</router.options.Wrap>\n }\n\n return inner\n}\n\nfunction RouterProviderInner<\n TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],\n TDehydrated extends Record<string, any> = Record<string, any>,\n>({ router }: RouterProps<TRouteTree, TDehydrated>) {\n const [preState, setState] = React.useState(() => router.state)\n const [isTransitioning, startReactTransition] = React.useTransition()\n const isAnyTransitioning =\n isTransitioning || preState.matches.some((d) => d.status === 'pending')\n\n const state = React.useMemo<RouterState<TRouteTree>>(\n () => ({\n ...preState,\n status: isAnyTransitioning ? 'pending' : 'idle',\n location: isTransitioning ? router.latestLocation : preState.location,\n pendingMatches: router.pendingMatches,\n }),\n [preState, isTransitioning],\n )\n\n router.setState = setState\n router.state = state\n router.startReactTransition = startReactTransition\n\n const tryLoad = () => {\n if (state.location !== router.latestLocation) {\n startReactTransition(() => {\n try {\n router.load()\n } catch (err) {\n console.error(err)\n }\n })\n }\n }\n\n useLayoutEffect(() => {\n const unsub = router.history.subscribe(() => {\n router.latestLocation = router.parseLocation(router.latestLocation)\n tryLoad()\n })\n\n const nextLocation = router.buildLocation({\n search: true,\n params: true,\n hash: true,\n state: true,\n })\n\n if (state.location.href !== nextLocation.href) {\n router.commitLocation({ ...nextLocation, replace: true })\n }\n\n return () => {\n unsub()\n }\n }, [router.history])\n\n useLayoutEffect(() => {\n if (!isTransitioning && state.resolvedLocation !== state.location) {\n router.emit({\n type: 'onResolved',\n fromLocation: state.resolvedLocation,\n toLocation: state.location,\n pathChanged: state.location!.href !== state.resolvedLocation?.href,\n })\n router.pendingMatches = []\n\n setState((s) => ({\n ...s,\n resolvedLocation: s.location,\n }))\n }\n })\n\n useLayoutEffect(() => {\n if (!window.__TSR_DEHYDRATED__) {\n tryLoad()\n }\n }, [])\n\n return (\n <routerContext.Provider value={router}>\n <Matches />\n </routerContext.Provider>\n )\n}\n\nexport function getRouteMatch<TRouteTree extends AnyRoute>(\n state: RouterState<TRouteTree>,\n id: string,\n): undefined | RouteMatch<TRouteTree> {\n return [...state.pendingMatches, ...state.matches].find((d) => d.id === id)\n}\n\nexport function useRouterState<\n TSelected = RouterState<RegisteredRouter['routeTree']>,\n>(opts?: {\n select: (state: RouterState<RegisteredRouter['routeTree']>) => TSelected\n}): TSelected {\n const { state } = useRouter()\n // return useStore(router.__store, opts?.select as any)\n return opts?.select ? opts.select(state) : (state as any)\n}\n\nexport type RouterProps<\n TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],\n TDehydrated extends Record<string, any> = Record<string, any>,\n> = Omit<RouterOptions<TRouteTree, TDehydrated>, 'context'> & {\n router: Router<TRouteTree>\n context?: Partial<RouterOptions<TRouteTree, TDehydrated>['context']>\n}\n\nexport function useRouter<\n TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],\n>(): Router<TRouteTree> {\n const resolvedContext =\n typeof document !== 'undefined'\n ? window.__TSR_ROUTER_CONTEXT__ || routerContext\n : routerContext\n const value = React.useContext(resolvedContext)\n warning(value, 'useRouter must be used inside a <RouterProvider> component!')\n return value as any\n}\n"],"names":["routerContext","React","createContext","document","window","__TSR_ROUTER_CONTEXT__","RouterProvider","router","rest","update","options","context","inner","createElement","RouterProviderInner","Wrap","preState","setState","useState","state","isTransitioning","startReactTransition","useTransition","isAnyTransitioning","matches","some","d","status","useMemo","location","latestLocation","pendingMatches","tryLoad","load","err","console","error","useLayoutEffect","unsub","history","subscribe","parseLocation","nextLocation","buildLocation","search","params","hash","href","commitLocation","replace","resolvedLocation","emit","type","fromLocation","toLocation","pathChanged","s","__TSR_DEHYDRATED__","Provider","value","Matches","getRouteMatch","id","find","useRouterState","opts","useRouter","select","resolvedContext","useContext","warning"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoEO,MAAMA,aAAa,gBAAGC,gBAAK,CAACC,aAAa,CAAc,IAAK,EAAC;AAEpE,IAAI,OAAOC,QAAQ,KAAK,WAAW,EAAE;EACnCC,MAAM,CAACC,sBAAsB,GAAGL,aAAoB,CAAA;AACtD,CAAA;AAEO,SAASM,cAAcA,CAG5B;EAAEC,MAAM;EAAE,GAAGC,IAAAA;AAA2C,CAAC,EAAE;AAC3D;EACAD,MAAM,CAACE,MAAM,CAAC;IACZ,GAAGF,MAAM,CAACG,OAAO;AACjB,IAAA,GAAGF,IAAI;AACPG,IAAAA,OAAO,EAAE;AACP,MAAA,GAAGJ,MAAM,CAACG,OAAO,CAACC,OAAO;AACzB,MAAA,GAAGH,IAAI,EAAEG,OAAAA;AACX,KAAA;AACF,GAAQ,CAAC,CAAA;AAET,EAAA,MAAMC,KAAK,gBAAGX,gBAAA,CAAAY,aAAA,CAACC,mBAAmB,EAAA;AAA0BP,IAAAA,MAAM,EAAEA,MAAAA;AAAO,GAAE,CAAC,CAAA;AAE9E,EAAA,IAAIA,MAAM,CAACG,OAAO,CAACK,IAAI,EAAE;IACvB,oBAAOd,gBAAA,CAAAY,aAAA,CAACN,MAAM,CAACG,OAAO,CAACK,IAAI,EAAEH,IAAAA,EAAAA,KAA2B,CAAC,CAAA;AAC3D,GAAA;AAEA,EAAA,OAAOA,KAAK,CAAA;AACd,CAAA;AAEA,SAASE,mBAAmBA,CAG1B;AAAEP,EAAAA,MAAAA;AAA6C,CAAC,EAAE;AAClD,EAAA,MAAM,CAACS,QAAQ,EAAEC,QAAQ,CAAC,GAAGhB,gBAAK,CAACiB,QAAQ,CAAC,MAAMX,MAAM,CAACY,KAAK,CAAC,CAAA;EAC/D,MAAM,CAACC,eAA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BAAe,CAAC,MAAM;AACpB,IAAA,IAAI,CAACjC,MAAM,CAACqD,kBAAkB,EAAE;AAC9BzB,MAAAA,OAAO,EAAE,CAAA;AACX,KAAA;GACD,EAAE,EAAE,CAAC,CAAA;AAEN,EAAA,oBACE/B,gBAAA,CAAAY,aAAA,CAACb,aAAa,CAAC0D,QAAQ,EAAA;AAACC,IAAAA,KAAK,EAAEpD,MAAAA;AAAO,GAAA,eACpCN,gBAAA,CAAAY,aAAA,CAAC+C,eAAO,EAAA,IAAE,CACY,CAAC,CAAA;AAE7B,CAAA;AAEO,SAASC,aAAaA,CAC3B1C,KAA8B,EAC9B2C,EAAU,EAC0B;EACpC,OAAO,CAAC,GAAG3C,KAAK,CAACY,cAAc,EAAE,GAAGZ,KAAK,CAACK,OAAO,CAAC,CAACuC,IAAI,CAAErC,CAAC,IAAKA,CAAC,CAACoC,EAAE,KAAKA,EAAE,CAAC,CAAA;AAC7E,CAAA;AAEO,SAASE,cAAcA,CAE5BC,IAED,EAAa;EACZ,MAAM;AAAE9C,IAAAA,KAAAA;GAAO,GAAG+C,SAAS,EAAE,CAAA;AAC7B;EACA,OAAOD,IAAI,EAAEE,MAAM,GAAGF,IAAI,CAACE,MAAM,CAAChD,KAAK,CAAC,GAAIA,KAAa,CAAA;AAC3D,CAAA;AAUO,SAAS+C,SAASA,GAED;AACtB,EAAA,MAAME,eAAe,GACnB,OAAOjE,QAAQ,KAAK,WAAW,GAC3BC,MAAM,CAACC,sBAAsB,IAAIL,aAAa,GAC9CA,aAAa,CAAA;AACnB,EAAA,MAAM2D,KAAK,GAAG1D,gBAAK,CAACoE,UAAU,CAACD,eAAe,CAAC,CAAA;AAC/CE,EAAAA,OAAO,CAACX,KAAK,EAAE,6DAA6D,CAAC,CAAA;AAC7E,EAAA,OAAOA,KAAK,CAAA;AACd;;;;;;;;"}
@@ -77,13 +77,13 @@ exports.Route = route.Route;
77
77
  exports.createRouteMask = route.createRouteMask;
78
78
  exports.rootRouteId = route.rootRouteId;
79
79
  exports.rootRouteWithContext = route.rootRouteWithContext;
80
+ exports.PathParamError = router.PathParamError;
80
81
  exports.Router = router.Router;
82
+ exports.SearchParamError = router.SearchParamError;
81
83
  exports.componentTypes = router.componentTypes;
84
+ exports.getInitialRouterState = router.getInitialRouterState;
82
85
  exports.lazyFn = router.lazyFn;
83
- exports.PathParamError = RouterProvider.PathParamError;
84
86
  exports.RouterProvider = RouterProvider.RouterProvider;
85
- exports.SearchParamError = RouterProvider.SearchParamError;
86
- exports.getInitialRouterState = RouterProvider.getInitialRouterState;
87
87
  exports.getRouteMatch = RouterProvider.getRouteMatch;
88
88
  exports.routerContext = RouterProvider.routerContext;
89
89
  exports.useRouter = RouterProvider.useRouter;
@@ -17,7 +17,8 @@ var RouterProvider = require('./RouterProvider.js');
17
17
  var path = require('./path.js');
18
18
  var invariant = require('tiny-invariant');
19
19
  var redirects = require('./redirects.js');
20
- var warning = require('tiny-warning');
20
+
21
+ // import warning from 'tiny-warning'
21
22
 
22
23
  //
23
24
 
@@ -36,7 +37,7 @@ class Router {
36
37
  // Must build in constructor
37
38
 
38
39
  constructor(options) {
39
- this.updateOptions({
40
+ this.update({
40
41
  defaultPreloadDelay: 50,
41
42
  defaultPendingMs: 1000,
42
43
  defaultPendingMinMs: 500,
@@ -46,20 +47,22 @@ class Router {
46
47
  parseSearch: options?.parseSearch ?? searchParams.defaultParseSearch
47
48
  });
48
49
  }
49
- startReactTransition = () => {
50
- warning(false, 'startReactTransition implementation is missing. If you see this, please file an issue.');
51
- };
52
- setState = () => {
53
- warning(false, 'setState implementation is missing. If you see this, please file an issue.');
50
+
51
+ // These are default implementations that can optionally be overridden
52
+ // by the router provider once rendered. We provide these so that the
53
+ // router can be used in a non-react environment if necessary
54
+ startReactTransition = fn => fn();
55
+ setState = updater => {
56
+ this.state = utils.functionalUpdate(updater, this.state);
54
57
  };
55
- updateOptions = newOptions => {
58
+ update = newOptions => {
56
59
  this.options = {
57
60
  ...this.options,
58
61
  ...newOptions
59
62
  };
60
63
  this.basepath = `/${path.trimPath(newOptions.basepath ?? '') ?? ''}`;
61
64
  if (!this.history || this.options.history && this.options.history !== this.history) {
62
- this.history = this.options.history ?? history.createBrowserHistory();
65
+ this.history = this.options.history ?? (typeof document !== 'undefined' ? history.createBrowserHistory() : history.createMemoryHistory());
63
66
  this.latestLocation = this.parseLocation();
64
67
  }
65
68
  if (this.options.routeTree !== this.routeTree) {
@@ -67,7 +70,7 @@ class Router {
67
70
  this.buildRouteTree();
68
71
  }
69
72
  if (!this.state) {
70
- this.state = RouterProvider.getInitialRouterState(this.latestLocation);
73
+ this.state = getInitialRouterState(this.latestLocation);
71
74
  }
72
75
  };
73
76
  buildRouteTree = () => {
@@ -248,7 +251,7 @@ class Router {
248
251
  // Add the parsed params to the accumulated params bag
249
252
  Object.assign(routeParams, parsedParams);
250
253
  } catch (err) {
251
- parsedParamsError = new RouterProvider.PathParamError(err.message, {
254
+ parsedParamsError = new PathParamError(err.message, {
252
255
  cause: err
253
256
  });
254
257
  if (opts?.throwOnError) {
@@ -280,7 +283,7 @@ class Router {
280
283
  ...search
281
284
  }, undefined];
282
285
  } catch (err) {
283
- const searchError = new RouterProvider.SearchParamError(err.message, {
286
+ const searchError = new SearchParamError(err.message, {
284
287
  cause: err
285
288
  });
286
289
  if (opts?.throwOnError) {
@@ -817,6 +820,7 @@ class Router {
817
820
  // ...s,
818
821
  // status: 'idle',
819
822
  // resolvedLocation: s.location,
823
+ // matches,
820
824
  // }))
821
825
 
822
826
  //
@@ -1017,63 +1021,42 @@ class Router {
1017
1021
  }
1018
1022
  return undefined;
1019
1023
  };
1020
-
1021
- // dehydrate = (): DehydratedRouter => {
1022
- // return {
1023
- // state: {
1024
- // dehydratedMatches: this.state.matches.map((d) =>
1025
- // pick(d, ['fetchedAt', 'invalid', 'id', 'status', 'updatedAt']),
1026
- // ),
1027
- // },
1028
- // }
1029
- // }
1030
-
1031
- // hydrate = async (__do_not_use_server_ctx?: HydrationCtx) => {
1032
- // let _ctx = __do_not_use_server_ctx
1033
- // // Client hydrates from window
1034
- // if (typeof document !== 'undefined') {
1035
- // _ctx = window.__TSR_DEHYDRATED__
1036
- // }
1037
-
1038
- // invariant(
1039
- // _ctx,
1040
- // 'Expected to find a __TSR_DEHYDRATED__ property on window... but we did not. Did you forget to render <DehydrateRouter /> in your app?',
1041
- // )
1042
-
1043
- // const ctx = _ctx
1044
- // this.dehydratedData = ctx.payload as any
1045
- // this.options.hydrate?.(ctx.payload as any)
1046
- // const dehydratedState = ctx.router.state
1047
-
1048
- // let matches = this.matchRoutes(
1049
- // this.state.location.pathname,
1050
- // this.state.location.search,
1051
- // ).map((match) => {
1052
- // const dehydratedMatch = dehydratedState.dehydratedMatches.find(
1053
- // (d) => d.id === match.id,
1054
- // )
1055
-
1056
- // invariant(
1057
- // dehydratedMatch,
1058
- // `Could not find a client-side match for dehydrated match with id: ${match.id}!`,
1059
- // )
1060
-
1061
- // if (dehydratedMatch) {
1062
- // return {
1063
- // ...match,
1064
- // ...dehydratedMatch,
1065
- // }
1066
- // }
1067
- // return match
1068
- // })
1069
-
1070
- // this.setState((s) => {
1071
- // return {
1072
- // ...s,
1073
- // matches: dehydratedState.dehydratedMatches as any,
1074
- // }
1075
- // })
1076
- // }
1024
+ dehydrate = () => {
1025
+ return {
1026
+ state: {
1027
+ dehydratedMatches: this.state.matches.map(d => utils.pick(d, ['fetchedAt', 'invalid', 'id', 'status', 'updatedAt', 'loaderData']))
1028
+ }
1029
+ };
1030
+ };
1031
+ hydrate = async __do_not_use_server_ctx => {
1032
+ let _ctx = __do_not_use_server_ctx;
1033
+ // Client hydrates from window
1034
+ if (typeof document !== 'undefined') {
1035
+ _ctx = window.__TSR_DEHYDRATED__;
1036
+ }
1037
+ invariant(_ctx, 'Expected to find a __TSR_DEHYDRATED__ property on window... but we did not. Did you forget to render <DehydrateRouter /> in your app?');
1038
+ const ctx = _ctx;
1039
+ this.dehydratedData = ctx.payload;
1040
+ this.options.hydrate?.(ctx.payload);
1041
+ const dehydratedState = ctx.router.state;
1042
+ let matches = this.matchRoutes(this.state.location.pathname, this.state.location.search).map(match => {
1043
+ const dehydratedMatch = dehydratedState.dehydratedMatches.find(d => d.id === match.id);
1044
+ invariant(dehydratedMatch, `Could not find a client-side match for dehydrated match with id: ${match.id}!`);
1045
+ if (dehydratedMatch) {
1046
+ return {
1047
+ ...match,
1048
+ ...dehydratedMatch
1049
+ };
1050
+ }
1051
+ return match;
1052
+ });
1053
+ this.setState(s => {
1054
+ return {
1055
+ ...s,
1056
+ matches: matches
1057
+ };
1058
+ });
1059
+ };
1077
1060
 
1078
1061
  // resolveMatchPromise = (matchId: string, key: string, value: any) => {
1079
1062
  // state.matches
@@ -1094,8 +1077,23 @@ function lazyFn(fn, key) {
1094
1077
  function isCtrlEvent(e) {
1095
1078
  return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
1096
1079
  }
1080
+ class SearchParamError extends Error {}
1081
+ class PathParamError extends Error {}
1082
+ function getInitialRouterState(location) {
1083
+ return {
1084
+ status: 'idle',
1085
+ resolvedLocation: location,
1086
+ location,
1087
+ matches: [],
1088
+ pendingMatches: [],
1089
+ lastUpdated: Date.now()
1090
+ };
1091
+ }
1097
1092
 
1093
+ exports.PathParamError = PathParamError;
1098
1094
  exports.Router = Router;
1095
+ exports.SearchParamError = SearchParamError;
1099
1096
  exports.componentTypes = componentTypes;
1097
+ exports.getInitialRouterState = getInitialRouterState;
1100
1098
  exports.lazyFn = lazyFn;
1101
1099
  //# sourceMappingURL=router.js.map