generated from stabledata/surface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurface.router.ts
42 lines (38 loc) · 1.11 KB
/
surface.router.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import {
AnyRouter,
createRouter as tanStackCreateRouter,
} from "@tanstack/react-router";
import { routeTree } from './.routes.tree.js';
import { inflateState } from './state/registry.js';
import { rpcClient } from './surface.client.js';
export type RouterContext = {
rpc?: typeof rpcClient;
};
declare module "@tanstack/react-router" {
interface Register {
router: ReturnType<typeof createRouter>;
}
}
export function createRouter(injections?: RouterContext): AnyRouter {
const router = tanStackCreateRouter({
routeTree,
context: {
...injections,
},
dehydrate: (): RouterContext => {
// async state need to be dehydrated from method above
// easier to just handle it all there
return {
...injections,
};
},
hydrate: (context) => {
// inflate state from the state.registry.
// note that loaders and beforeLoaders also receive context,
// but you'll have to add it to context/state manually or else
// it may not be there, unless you use RPC round trip (self request)
inflateState(context);
},
});
return router;
}