-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
routeModules.ts
214 lines (193 loc) · 6.06 KB
/
routeModules.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import type { ComponentType, ReactElement } from "react";
import type {
ActionFunction as RRActionFunction,
ActionFunctionArgs as RRActionFunctionArgs,
LoaderFunction as RRLoaderFunction,
LoaderFunctionArgs as RRLoaderFunctionArgs,
DataRouteMatch,
Params,
Location,
ShouldRevalidateFunction,
} from "react-router-dom";
import type { LoaderFunction, SerializeFrom } from "@remix-run/server-runtime";
import type { AppData } from "./data";
import type { LinkDescriptor } from "./links";
import type { EntryRoute } from "./routes";
export interface RouteModules {
[routeId: string]: RouteModule | undefined;
}
export interface RouteModule {
clientAction?: ClientActionFunction;
clientLoader?: ClientLoaderFunction;
ErrorBoundary?: ErrorBoundaryComponent;
HydrateFallback?: HydrateFallbackComponent;
Layout?: LayoutComponent;
default: RouteComponent;
handle?: RouteHandle;
links?: LinksFunction;
meta?: MetaFunction;
shouldRevalidate?: ShouldRevalidateFunction;
}
/**
* A function that handles data mutations for a route on the client
*/
export type ClientActionFunction = (
args: ClientActionFunctionArgs
) => ReturnType<RRActionFunction>;
/**
* Arguments passed to a route `clientAction` function
*/
export type ClientActionFunctionArgs = RRActionFunctionArgs<undefined> & {
serverAction: <T = AppData>() => Promise<SerializeFrom<T>>;
};
/**
* A function that loads data for a route on the client
*/
export type ClientLoaderFunction = ((
args: ClientLoaderFunctionArgs
) => ReturnType<RRLoaderFunction>) & {
hydrate?: boolean;
};
/**
* Arguments passed to a route `clientLoader` function
*/
export type ClientLoaderFunctionArgs = RRLoaderFunctionArgs<undefined> & {
serverLoader: <T = AppData>() => Promise<SerializeFrom<T>>;
};
/**
* ErrorBoundary to display for this route
*/
export type ErrorBoundaryComponent = ComponentType;
/**
* `<Route HydrateFallback>` component to render on initial loads
* when client loaders are present
*/
export type HydrateFallbackComponent = ComponentType;
/**
* Optional, root-only `<Route Layout>` component to wrap the root content in.
* Useful for defining the <html>/<head>/<body> document shell shared by the
* Component, HydrateFallback, and ErrorBoundary
*/
export type LayoutComponent = ComponentType<{
children: ReactElement<
unknown,
ErrorBoundaryComponent | HydrateFallbackComponent | RouteComponent
>;
}>;
/**
* A function that defines `<link>` tags to be inserted into the `<head>` of
* the document on route transitions.
*
* @see https://remix.run/route/meta
*/
export interface LinksFunction {
(): LinkDescriptor[];
}
export interface MetaMatch<
RouteId extends string = string,
Loader extends LoaderFunction | unknown = unknown
> {
id: RouteId;
pathname: DataRouteMatch["pathname"];
data: Loader extends LoaderFunction ? SerializeFrom<Loader> : unknown;
handle?: RouteHandle;
params: DataRouteMatch["params"];
meta: MetaDescriptor[];
error?: unknown;
}
export type MetaMatches<
MatchLoaders extends Record<string, LoaderFunction | unknown> = Record<
string,
unknown
>
> = Array<
{
[K in keyof MatchLoaders]: MetaMatch<
Exclude<K, number | symbol>,
MatchLoaders[K]
>;
}[keyof MatchLoaders]
>;
export interface MetaArgs<
Loader extends LoaderFunction | unknown = unknown,
MatchLoaders extends Record<string, LoaderFunction | unknown> = Record<
string,
unknown
>
> {
data:
| (Loader extends LoaderFunction ? SerializeFrom<Loader> : AppData)
| undefined;
params: Params;
location: Location;
matches: MetaMatches<MatchLoaders>;
error?: unknown;
}
export interface MetaFunction<
Loader extends LoaderFunction | unknown = unknown,
MatchLoaders extends Record<string, LoaderFunction | unknown> = Record<
string,
unknown
>
> {
(args: MetaArgs<Loader, MatchLoaders>): MetaDescriptor[] | undefined;
}
export type MetaDescriptor =
| { charSet: "utf-8" }
| { title: string }
| { name: string; content: string }
| { property: string; content: string }
| { httpEquiv: string; content: string }
| { "script:ld+json": LdJsonObject }
| { tagName: "meta" | "link"; [name: string]: string }
| { [name: string]: unknown };
type LdJsonObject = { [Key in string]: LdJsonValue } & {
[Key in string]?: LdJsonValue | undefined;
};
type LdJsonArray = LdJsonValue[] | readonly LdJsonValue[];
type LdJsonPrimitive = string | number | boolean | null;
type LdJsonValue = LdJsonPrimitive | LdJsonObject | LdJsonArray;
/**
* A React component that is rendered for a route.
*/
export type RouteComponent = ComponentType<{}>;
/**
* An arbitrary object that is associated with a route.
*
* @see https://remix.run/route/handle
*/
export type RouteHandle = unknown;
export async function loadRouteModule(
route: EntryRoute,
routeModulesCache: RouteModules
): Promise<RouteModule> {
if (route.id in routeModulesCache) {
return routeModulesCache[route.id] as RouteModule;
}
try {
let routeModule = await import(/* webpackIgnore: true */ route.module);
routeModulesCache[route.id] = routeModule;
return routeModule;
} catch (error: unknown) {
// User got caught in the middle of a deploy and the CDN no longer has the
// asset we're trying to import! Reload from the server and the user
// (should) get the new manifest--unless the developer purged the static
// assets, the manifest path, but not the documents 😬
if (
window.__remixContext.isSpaMode &&
// @ts-expect-error
typeof import.meta.hot !== "undefined"
) {
// In SPA Mode (which implies vite) we don't want to perform a hard reload
// on dev-time errors since it's a vite compilation error and a reload is
// just going to fail with the same issue. Let the UI bubble to the error
// boundary and let them see the error in the overlay or the dev server log
console.error(`Error loading route module \`${route.module}\`:`, error);
throw error;
}
window.location.reload();
return new Promise(() => {
// check out of this hook cause the DJs never gonna re[s]olve this
});
}
}