-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
routes.ts
106 lines (97 loc) · 2.93 KB
/
routes.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import type {
AgnosticDataRouteObject,
ActionFunctionArgs,
LoaderFunctionArgs,
} from "@remix-run/router";
import { callRouteActionRR, callRouteLoaderRR } from "./data";
import type { ServerRouteModule } from "./routeModules";
export interface RouteManifest<Route> {
[routeId: string]: Route;
}
export type ServerRouteManifest = RouteManifest<Omit<ServerRoute, "children">>;
// NOTE: make sure to change the Route in remix-react if you change this
export interface Route {
index?: boolean;
caseSensitive?: boolean;
id: string;
parentId?: string;
path?: string;
}
// NOTE: make sure to change the EntryRoute in remix-react if you change this
export interface EntryRoute extends Route {
hasAction: boolean;
hasLoader: boolean;
hasCatchBoundary: boolean;
hasErrorBoundary: boolean;
imports?: string[];
module: string;
parentId?: string;
}
export interface ServerRoute extends Route {
children: ServerRoute[];
module: ServerRouteModule;
}
export function createRoutes(
manifest: ServerRouteManifest,
parentId?: string
): ServerRoute[] {
return Object.entries(manifest)
.filter(([, route]) => route.parentId === parentId)
.map(([id, route]) => ({
...route,
children: createRoutes(manifest, id),
}));
}
// Convert the Remix ServerManifest into DataRouteObject's for use with
// createStaticHandler
export function createStaticHandlerDataRoutes(
manifest: ServerRouteManifest,
parentId?: string
): AgnosticDataRouteObject[] {
return Object.values(manifest)
.filter((route) => route.parentId === parentId)
.map((route) => {
let commonRoute = {
// Always include root due to default boundaries
hasErrorBoundary:
route.id === "root" ||
route.module.CatchBoundary != null ||
route.module.ErrorBoundary != null,
id: route.id,
path: route.path,
loader: route.module.loader
? (args: LoaderFunctionArgs) =>
callRouteLoaderRR({
request: args.request,
params: args.params,
loadContext: args.context,
loader: route.module.loader!,
routeId: route.id,
})
: undefined,
action: route.module.action
? (args: ActionFunctionArgs) =>
callRouteActionRR({
request: args.request,
params: args.params,
loadContext: args.context,
action: route.module.action!,
routeId: route.id,
})
: undefined,
handle: route.module.handle,
// TODO: RRR - Implement!
shouldRevalidate: () => true,
};
return route.index
? {
index: true,
...commonRoute,
}
: {
caseSensitive: route.caseSensitive,
children: createStaticHandlerDataRoutes(manifest, route.id),
...commonRoute,
};
});
}