-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
server.ts
461 lines (409 loc) · 13.7 KB
/
server.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import type { StaticHandler, StaticHandlerContext } from "@remix-run/router";
import {
isRouteErrorResponse,
unstable_createStaticHandler,
} from "@remix-run/router";
import type { AppLoadContext } from "./data";
import type { AppState } from "./errors";
import type { ServerBuild, HandleDocumentRequestFunction } from "./build";
import type { EntryContext } from "./entry";
import { createEntryMatches, createEntryRouteModules } from "./entry";
import { serializeError } from "./errors";
import { getDocumentHeadersRR } from "./headers";
import invariant from "./invariant";
import { ServerMode, isServerMode } from "./mode";
import type { RouteMatch } from "./routeMatching";
import { matchServerRoutes } from "./routeMatching";
import type { ServerRoute, ServerRouteManifest } from "./routes";
import { createStaticHandlerDataRoutes, createRoutes } from "./routes";
import { json, isRedirectResponse, isResponse } from "./responses";
import { createServerHandoffString } from "./serverHandoff";
export type RequestHandler = (
request: Request,
loadContext?: AppLoadContext
) => Promise<Response>;
export type CreateRequestHandlerFunction = (
build: ServerBuild,
mode?: string
) => RequestHandler;
export const createRequestHandler: CreateRequestHandlerFunction = (
build,
mode
) => {
let routes = createRoutes(build.routes);
let dataRoutes = createStaticHandlerDataRoutes(build.routes);
let serverMode = isServerMode(mode) ? mode : ServerMode.Production;
let staticHandler = unstable_createStaticHandler(dataRoutes);
return async function requestHandler(request, loadContext = {}) {
let url = new URL(request.url);
let matches = matchServerRoutes(routes, url.pathname);
let response: Response;
if (url.searchParams.has("_data")) {
let routeId = url.searchParams.get("_data")!;
response = await handleDataRequestRR(
serverMode,
staticHandler,
routeId,
request,
loadContext
);
if (build.entry.module.handleDataRequest) {
let match = matches!.find((match) => match.route.id == routeId)!;
response = await build.entry.module.handleDataRequest(response, {
context: loadContext,
params: match.params,
request,
});
}
} else if (
matches &&
matches[matches.length - 1].route.module.default == null
) {
response = await handleResourceRequestRR(
serverMode,
staticHandler,
matches.slice(-1)[0].route.id,
request,
loadContext
);
} else {
response = await handleDocumentRequestRR(
serverMode,
build,
staticHandler,
request,
loadContext
);
}
if (request.method === "HEAD") {
return new Response(null, {
headers: response.headers,
status: response.status,
statusText: response.statusText,
});
}
return response;
};
};
async function handleDataRequestRR(
serverMode: ServerMode,
staticHandler: StaticHandler,
routeId: string,
request: Request,
loadContext: AppLoadContext
) {
try {
let response = await staticHandler.queryRoute(request, {
routeId,
requestContext: loadContext,
});
if (isRedirectResponse(response)) {
// We don't have any way to prevent a fetch request from following
// redirects. So we use the `X-Remix-Redirect` header to indicate the
// next URL, and then "follow" the redirect manually on the client.
let headers = new Headers(response.headers);
headers.set("X-Remix-Redirect", headers.get("Location")!);
headers.delete("Location");
if (response.headers.get("Set-Cookie") !== null) {
headers.set("X-Remix-Revalidate", "yes");
}
return new Response(null, {
status: 204,
headers,
});
}
return response;
} catch (error) {
if (isResponse(error)) {
error.headers.set("X-Remix-Catch", "yes");
return error;
}
let status = 500;
let errorInstance = error;
if (isRouteErrorResponse(error)) {
status = error.status;
errorInstance = error.error || errorInstance;
}
if (serverMode !== ServerMode.Test && !request.signal.aborted) {
console.error(errorInstance);
}
if (
serverMode === ServerMode.Development &&
errorInstance instanceof Error
) {
return errorBoundaryError(errorInstance, status);
}
return errorBoundaryError(new Error("Unexpected Server Error"), status);
}
}
function findParentBoundary(
routes: ServerRouteManifest,
routeId: string,
error: any
): string {
// Fall back to the root route if we don't match any routes, since Remix
// has default error/catch boundary handling. This handles the case where
// react-router doesn't have a matching "root" route to assign the error to
// so it returns context.errors = { __shim-error-route__: ErrorResponse }
let route = routes[routeId] || routes["root"];
// Router-thrown ErrorResponses will have the error instance. User-thrown
// Responses will not have an error. The one exception here is internal 404s
// which we handle the same as user-thrown 404s
let isCatch =
isRouteErrorResponse(error) && (!error.error || error.status === 404);
if (
(isCatch && route.module.CatchBoundary) ||
(!isCatch && route.module.ErrorBoundary) ||
!route.parentId
) {
return route.id;
}
return findParentBoundary(routes, route.parentId, error);
}
// Re-generate a remix-friendly context.errors structure. The Router only
// handles generic errors and does not distinguish error versus catch. We
// may have a thrown response tagged to a route that only exports an
// ErrorBoundary or vice versa. So we adjust here and ensure that
// data-loading errors are properly associated with routes that have the right
// type of boundaries.
export function differentiateCatchVersusErrorBoundaries(
build: ServerBuild,
context: StaticHandlerContext
) {
if (!context.errors) {
return;
}
let errors: Record<string, any> = {};
for (let routeId of Object.keys(context.errors)) {
let error = context.errors[routeId];
let handlingRouteId = findParentBoundary(build.routes, routeId, error);
errors[handlingRouteId] = error;
}
context.errors = errors;
}
async function handleDocumentRequestRR(
serverMode: ServerMode,
build: ServerBuild,
staticHandler: StaticHandler,
request: Request,
loadContext: AppLoadContext
) {
let context;
try {
context = await staticHandler.query(request, {
requestContext: loadContext,
});
} catch (error) {
if (!request.signal.aborted && serverMode !== ServerMode.Test) {
console.error(error);
}
return new Response(null, { status: 500 });
}
if (isResponse(context)) {
return context;
}
// Restructure context.errors to the right Catch/Error Boundary
differentiateCatchVersusErrorBoundaries(build, context);
let appState: AppState = {
trackBoundaries: true,
trackCatchBoundaries: true,
catchBoundaryRouteId: null,
renderBoundaryRouteId: null,
loaderBoundaryRouteId: null,
};
// Copy staticContext.errors to catch/error boundaries in appState
// Note: Only assign the boundary id if this module has a boundary. This
// should be true in almost all cases, except for cases in which no
// boundaries exist and the router "assigns" the catch/error to the root
// route for lack of a better place to put it. If the root doesn't have a
// catch/error boundary, then we leave the boundaryId null to bubble to the
// default boundary at render time
for (let match of context.matches) {
let route = match.route as ServerRoute;
let id = route.id;
let error = context.errors?.[id];
let hasCatchBoundary = build.routes[id]?.module.CatchBoundary != null;
let hasErrorBoundary = build.routes[id]?.module.ErrorBoundary != null;
if (!error) {
continue;
} else if (isRouteErrorResponse(error)) {
// Internal Router errors will throw an ErrorResponse with the actual
// error instance, while user-thrown ErrorResponses will not have the
// instance. We also exclude 404s so we can handle them as CatchBoundary
// errors so the user has a singular location for 404 UI
if (error.internal && error.error && error.status !== 404) {
if (hasErrorBoundary) {
appState.loaderBoundaryRouteId = id;
}
appState.trackBoundaries = false;
appState.error = await serializeError(error.error);
if (
error.status === 405 &&
error.error.message.includes("Invalid request method")
) {
// Note: Emptying this out ensures our response matches the Remix-flow
// exactly, but functionally both end up using the root error boundary
// if it exists. Might be able to clean this up eventually.
context.matches = [];
}
break;
}
// ErrorResponse's without an error were thrown by the user action/loader
if (hasCatchBoundary) {
appState.catchBoundaryRouteId = id;
}
appState.trackCatchBoundaries = false;
appState.catch = {
// Order is important for response matching assertions!
data:
error.error && error.status === 404
? error.error.message
: error.data,
status: error.status,
statusText: error.statusText,
};
break;
} else {
if (hasErrorBoundary) {
appState.loaderBoundaryRouteId = id;
}
appState.trackBoundaries = false;
appState.error = await serializeError(error);
break;
}
}
let renderableMatches = getRenderableMatches(
context.matches as unknown as RouteMatch<ServerRoute>[],
appState
);
if (!renderableMatches) {
renderableMatches = [];
let root = staticHandler.dataRoutes[0] as ServerRoute;
if (root?.module?.CatchBoundary) {
appState.catchBoundaryRouteId = "root";
renderableMatches.push({
params: {},
pathname: "",
route: staticHandler.dataRoutes[0] as ServerRoute,
});
}
}
let headers = getDocumentHeadersRR(build, context, renderableMatches);
let serverHandoff: Pick<
EntryContext,
"actionData" | "appState" | "matches" | "routeData" | "future"
> = {
actionData: context.actionData || undefined,
appState,
matches: createEntryMatches(renderableMatches, build.assets.routes),
routeData: context.loaderData || {},
future: build.future,
};
let entryContext: EntryContext = {
...serverHandoff,
manifest: build.assets,
routeModules: createEntryRouteModules(build.routes),
serverHandoffString: createServerHandoffString(serverHandoff),
};
let handleDocumentRequestParameters: Parameters<HandleDocumentRequestFunction> =
[request, context.statusCode, headers, entryContext];
let handleDocumentRequestFunction = build.entry.module.default;
try {
return await handleDocumentRequestFunction(
...handleDocumentRequestParameters
);
} catch (error) {
handleDocumentRequestParameters[1] = 500;
appState.trackBoundaries = false;
appState.error = await serializeError(error as Error);
entryContext.serverHandoffString = createServerHandoffString(serverHandoff);
try {
return await handleDocumentRequestFunction(
...handleDocumentRequestParameters
);
} catch (error: any) {
return returnLastResortErrorResponse(error, serverMode);
}
}
}
async function handleResourceRequestRR(
serverMode: ServerMode,
staticHandler: StaticHandler,
routeId: string,
request: Request,
loadContext: AppLoadContext
) {
try {
// Note we keep the routeId here to align with the Remix handling of
// resource routes which doesn't take ?index into account and just takes
// the leaf match
let response = await staticHandler.queryRoute(request, {
routeId,
requestContext: loadContext,
});
// callRouteLoader/callRouteAction always return responses
invariant(
isResponse(response),
"Expected a Response to be returned from queryRoute"
);
return response;
} catch (error) {
if (isResponse(error)) {
// Note: Not functionally required but ensures that our response headers
// match identically to what Remix returns
error.headers.set("X-Remix-Catch", "yes");
return error;
}
return returnLastResortErrorResponse(error, serverMode);
}
}
async function errorBoundaryError(error: Error, status: number) {
return json(await serializeError(error), {
status,
headers: {
"X-Remix-Error": "yes",
},
});
}
// This prevents `<Outlet/>` from rendering anything below where the error threw
// TODO: maybe do this in <RemixErrorBoundary + context>
function getRenderableMatches(
matches: RouteMatch<ServerRoute>[] | null,
appState: AppState
) {
if (!matches) {
return null;
}
// no error, no worries
if (!appState.catch && !appState.error) {
return matches;
}
let lastRenderableIndex: number = -1;
matches.forEach((match, index) => {
let id = match.route.id;
if (
appState.renderBoundaryRouteId === id ||
appState.loaderBoundaryRouteId === id ||
appState.catchBoundaryRouteId === id
) {
lastRenderableIndex = index;
}
});
return matches.slice(0, lastRenderableIndex + 1);
}
function returnLastResortErrorResponse(error: any, serverMode?: ServerMode) {
if (serverMode !== ServerMode.Test) {
console.error(error);
}
let message = "Unexpected Server Error";
if (serverMode !== ServerMode.Production) {
message += `\n\n${String(error)}`;
}
// Good grief folks, get your act together 😂!
return new Response(message, {
status: 500,
headers: {
"Content-Type": "text/plain",
},
});
}