-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.tsx
702 lines (649 loc) · 21.7 KB
/
server.tsx
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
import * as path from "std/path/mod.ts";
import {
Application,
Context,
ListenOptions,
RouteParams,
Router,
RouterMiddleware,
Status,
} from "x/oak/mod.ts";
import {
ComponentType,
Context as ReactContext,
ReactNode,
StrictMode,
} from "npm/react";
import { HelmetContext, HelmetProvider } from "npm/react-helmet-async";
import { renderToReadableStream as renderReactToReadableStream } from "npm/react-dom/server";
import {
createMemoryRouter,
RouteObject,
RouterProvider,
} from "npm/react-router-dom";
import serialize from "npm/serialize-javascript";
import {
AppErrorContext,
ErrorResponse,
HttpError,
isHttpError,
} from "./error.tsx";
import {
AppEnvironment,
createAppContext,
getEnv,
isBrowser,
isDevelopment,
isTest,
} from "./env.ts";
import { RouteFile } from "./mod.tsx";
if (isBrowser()) {
throw new Error("Cannot import server.tsx in the browser.");
}
const encoder = new TextEncoder();
/**
* An interface that defines the configuration for generating the HTML that wraps the server-side rendered react application.
*/
interface HTMLOptions<
AppContext extends Record<string, unknown> = Record<string, unknown>,
> {
helmet: HelmetContext.HelmetServerState;
env: AppEnvironment;
context: AppContext;
devPort?: number;
error?: HttpError<{ boundary?: string }>;
}
/** Generates the HTML that wraps the server-side rendered react application. */
function html<
AppContext extends Record<string, unknown> = Record<string, unknown>,
>(
{ helmet, env, context, devPort, error }: HTMLOptions<AppContext>,
) {
const errorJSON = HttpError.json(error);
if (isDevelopment()) {
if (error?.expose) errorJSON.expose = error.expose;
if (error instanceof Error) {
errorJSON.stack = error.stack;
}
}
const headTags = [
helmet.base.toString(),
helmet.title.toString(),
helmet.priority.toString(),
helmet.meta.toString(),
helmet.link.toString(),
helmet.style.toString(),
helmet.script.toString(),
`<script>
window.app = {
env: ${serialize(env, { isJSON: true })},
context: ${serialize(context, { isJSON: true })},
};
</script>`,
error &&
`<script>window.app.error = ${serialize(errorJSON)};</script>`,
isDevelopment() && devPort &&
`<script>window.app.devPort = ${
serialize(devPort, { isJSON: true })
};</script>`,
isDevelopment() && `<script src="/live-reload.js"></script>`,
helmet.noscript.toString(),
].filter((tag: string) => Boolean(tag));
return {
start: `\
<!DOCTYPE html>
<html ${helmet.htmlAttributes.toString()}>
<head>
${headTags.join("\n ")}
<script type="module" src="/${
isTest() ? "test-" : ""
}build/app.js" defer></script>
</head>
<body ${helmet.bodyAttributes.toString()}>`,
end: `
</body>
</html>
`,
};
}
/**
* Renders a React app to a readable stream that can be returned to the client.
* This is the default `renderToReadableStream` used for rendering React apps.
*/
export async function renderToReadableStream<
AppContext extends Record<string, unknown> = Record<string, unknown>,
AppState extends ServerState<AppContext> = ServerState<AppContext>,
>(
context: Context<AppState>,
) {
const { request, state } = context;
const { route, providerFactory, Context } = state._app;
const Provider = providerFactory(context);
const { env, context: appContext, error, devPort } = state.app;
const { pathname, search } = request.url;
const location = `${pathname}${search}`;
const helmetContext = {} as HelmetContext;
const router = createMemoryRouter([route], {
initialEntries: [location],
});
const stream = await renderReactToReadableStream(
<StrictMode>
<HelmetProvider context={helmetContext}>
<AppErrorContext.Provider value={{ error }}>
<Context.Provider value={appContext}>
<Provider>
<RouterProvider router={router} />
</Provider>
</Context.Provider>
</AppErrorContext.Provider>
</HelmetProvider>
</StrictMode>,
{
onError(error: unknown) {
console.error("renderToReadableStream error", error);
},
},
);
await stream.allReady;
const { start, end } = html({
helmet: helmetContext.helmet,
env,
context: appContext,
error,
devPort,
});
return stream
.pipeThrough(
new TransformStream({
start(controller) {
controller.enqueue(encoder.encode(start));
},
flush(controller) {
controller.enqueue(encoder.encode(end));
},
}),
);
}
/**
* A record of application state when handling a request.
*/
export interface ServerState<
AppContext extends Record<string, unknown> = Record<string, unknown>,
> {
/** Application state that is meant for internal use only. */
_app: {
route: RouteObject;
providerFactory: ProviderFactory<AppContext>;
Context: ReactContext<AppContext>;
};
/** A container for the application's data and functions. */
app: {
/**
* An object containing environment variables that will be shared with the browser.
*/
env: AppEnvironment;
/**
* A container for the application's data that will be serialized and sent to the browser.
* This data can be accessed via the `AppContext`.
*/
context: AppContext;
/** A function that renders the application to a readable stream and responds to the request with it. */
render: () => Promise<void>;
/** The port for the dev script's live reload server. */
devPort?: number;
/**
* If an error occurs when handling the request, this property will be set to that error.
* The error will be serialized and sent to the browser.
* The browser will recreate the error for an `AppErrorBoundary` to catch.
* If the server error is not getting caught, the boundary doesn't match the `AppErrorBoundary` you expect to catch it.
*/
error?: HttpError<{ boundary?: string }>;
};
}
/**
* The default function for creating a provider for the application.
*/
function defaultProviderFactory<
AppContext extends Record<string, unknown> = Record<string, unknown>,
AppState extends ServerState<AppContext> = ServerState<AppContext>,
>(
context: Context<AppState>,
): ComponentType<{ children: ReactNode }> {
return (({ children }) => <>{children}</>);
}
export type ProviderFactory<
AppContext extends Record<string, unknown> = Record<string, unknown>,
AppState extends ServerState<AppContext> = ServerState<AppContext>,
> = (
context: Context<AppState>,
) => ComponentType<{ children: ReactNode }>;
/** An interface that represents the options for creating an App Router. */
export interface AppRouterOptions<
AppContext extends Record<string, unknown> = Record<string, unknown>,
AppState extends ServerState<AppContext> = ServerState<AppContext>,
> {
/**
* A react router route object.
* The build script will automatically generate this for your application's routes.
* The route object is a default export from `_main.tsx` in your routes directory.
*/
route: RouteObject;
/**
* Default environment variables that you would like to share with the browser for all requests.
*/
env?: AppEnvironment;
/** Creates a provider that wraps the entire application. */
providerFactory?: ProviderFactory<AppContext, AppState>;
/** A context object for the App. State stored within the AppContext will be serialized and shared with the browser. */
Context?: ReactContext<AppContext>;
/**
* A function used to render the application to a readable stream.
* If you'd like to transform the stream before it is returned to the client,
* you can wrap the default `renderToReadableStream` to pipe it through a transform stream.
*
* For example, the following `renderToReadableStream` function uses the default function
* then pipe's it through a transform stream that adds a tailwindcss style sheet to the head of the HTML page using twind.
*
* ```ts
* async renderToReadableStream(context: Context<AppState>) {
* return (await renderToReadableStream(context))
* .pipeThrough(new TwindStream(tw));
* }
* ```
*/
renderToReadableStream?: typeof renderToReadableStream<AppContext, AppState>;
/**
* The Oak router for the application.
* The router object will be generated automatically for the application's routes.
* The object is the default export from the `_main.ts` file in the routes directory.
*/
router?: Router<AppState>;
/**
* The working directory of the application.
* Defaults to the current working directory that the application is running from.
*/
workingDirectory?: string;
/** The port for the dev script's live reload server. */
devPort?: number;
}
const TRAILING_SLASHES = /\/+$/;
/**
* Creates an Oak router for your application.
*/
export function createAppRouter<
AppContext extends Record<string, unknown> = Record<string, unknown>,
AppState extends ServerState<AppContext> = ServerState<AppContext>,
>(
{
route,
env,
providerFactory,
Context,
renderToReadableStream: renderAppToReadableStream,
router,
workingDirectory,
devPort,
}: AppRouterOptions<AppContext, AppState>,
) {
renderAppToReadableStream ??=
renderToReadableStream as typeof renderToReadableStream<
AppContext,
AppState
>;
router ??= new Router<AppState>();
workingDirectory ??= Deno.cwd();
providerFactory ??= defaultProviderFactory as ProviderFactory<
AppContext,
AppState
>;
Context ??= createAppContext<AppContext>();
const appRouter = new Router<AppState>()
.use(async (context, next) => {
const { request, response } = context;
const { pathname, search } = request.url;
if (pathname.length > 1 && pathname.at(-1) === "/") {
response.status = 301;
response.redirect(pathname.replace(TRAILING_SLASHES, "") + search);
} else {
await next();
}
})
.use(async (context: Context<AppState>, next) => {
const { request, response, state } = context;
try {
if (!state.app) {
state._app = {
route,
providerFactory: providerFactory! as ProviderFactory<AppContext>,
Context: Context!,
};
state.app = {
env: {
APP_ENV: getEnv("APP_ENV"),
...env,
},
context: {} as AppContext,
render: async () => {
response.type = "html";
response.body = await renderAppToReadableStream!(context);
},
};
if (isDevelopment() && devPort) {
state.app.devPort = devPort;
}
}
await next();
} catch (cause) {
const error = HttpError.from(cause);
console.error("App error", error);
response.status = error.status;
state.app.error = error;
await state.app.render();
}
})
.use(router.routes(), router.allowedMethods());
if (isDevelopment()) {
let liveReloadScript = "";
appRouter.use(async (context, next) => {
const { request, response } = context;
if (request.url.pathname === "/live-reload.js") {
if (!liveReloadScript) {
liveReloadScript = await (await fetch(
new URL("./live-reload.js", import.meta.url),
)).text();
}
response.headers.set("Content-Type", "text/javascript");
response.body = liveReloadScript;
} else {
await next();
}
});
}
appRouter.get("/(.*)", async (context) => {
try {
await context.send({ root: `${workingDirectory}/public` });
} catch (cause) {
if (isHttpError(cause) && cause.status === Status.NotFound) {
throw new HttpError(404, "Not found", { cause });
} else {
throw cause;
}
}
});
return appRouter;
}
/** Creates a Udibo React App. */
export function createApp<
AppContext extends Record<string, unknown> = Record<string, unknown>,
AppState extends ServerState<AppContext> = ServerState<AppContext>,
>(options: AppRouterOptions<AppContext, AppState>) {
const app = new Application();
const appRouter = createAppRouter(options);
app.use(appRouter.routes(), appRouter.allowedMethods());
return app;
}
/**
* This function tells the dev server when the app server is listening.
* If you are not using serve, you must add an event listener to your app that will call this function once it's listening.
* If this function is not called, the browser will not automatically refresh when the app server is restarted.
* If called before the app server is listening, the browser will refresh before the app server is ready to handle the request.
* This function will not do anything if the app is not running in development mode.
*/
export async function listeningDev(
{ hostname, secure, devPort }: {
hostname: string;
secure: boolean;
devPort?: number;
},
) {
if (isDevelopment()) {
try {
const origin = `${secure ? "https://" : "http://"}${hostname}`;
await fetch(`${origin}:${devPort || 9002}/listening`);
} catch {
// Ignore errors
}
}
}
export interface ServeOptions<
AppContext extends Record<string, unknown> = Record<string, unknown>,
AppState extends ServerState<AppContext> = ServerState<AppContext>,
> extends AppRouterOptions<AppContext, AppState> {
/** The port your application will listen on. */
port?: number;
}
/** Creates and serves a Udibo React App. */
export async function serve<
AppContext extends Record<string, unknown> = Record<string, unknown>,
>({ port, ...options }: ServeOptions<AppContext>) {
const app = createApp(options);
app.addEventListener("error", ({ error }) => {
console.error("Uncaught app error", error);
});
app.addEventListener("listen", ({ hostname, port, secure }) => {
const origin = `${secure ? "https://" : "http://"}${hostname}`;
console.log(`Listening on: ${origin}:${port}`);
queueMicrotask(() =>
listeningDev({ hostname, secure, devPort: options.devPort })
);
});
const listenOptions = {} as ListenOptions;
if (typeof port === "number") listenOptions.port = port;
await app.listen(listenOptions);
}
/**
* This middleware ensures all errors in the route are HttpErrors.
* If an error isn't an HttpError, a new HttpError is created with it as the cause.
* If a boundary is specified, it will add the boundary to the HttpError.
* If an AppErrorBoundary exists with a matching boundary, it will be used to handle the error.
* If a boundary is not specified, the first AppErrorBoundary without a boundary specified will handle the error.
* If a boundary is specified, but no AppErrorBoundary exists with a matching boundary, the error will go unhandled.
*
* By default, any route that has an ErrorFallback will have an errorBoundary automatically added to it.
* The automatic error boundaries name will match the route by default.
* If a route exports a boundary string, that will be used as the errorBoundary's boundary.
* You can add your own error boundaries anywhere.
*
* To ensure an error boundary catches the error, you need to either export a boundary string from your route
* or your router needs to use the error boundary middleware.
*
* ```ts
* export const boundary = "MyComponentErrorBoundary"
* ```
*
* ```ts
* const router = new Router()
* .use(errorBoundary("MyComponentErrorBoundary"))
* ```
*
* Then the related react component for the route needs to either use `withAppErrorBoundary` or `AppErrorBoundary` to be able to catch the error during rendering.
* The boundary identifier must match the one on the server.
*
* ```tsx
* const MyComponentSafe = withAppErrorBoundary(MyComponent, {
* FallbackComponent: DefaultErrorFallback,
* boundary: "MyComponentErrorBoundary"
* })
* ```
*
* ```tsx
* <AppErrorBoundary FallbackComponent={DefaultErrorFallback} boundary="MyComponentErrorBoundary">
* <MyComponent />
* </AppErrorBoundary>
* ```
*/
export function errorBoundary<
Params extends RouteParams<string> = RouteParams<string>,
AppContext extends Record<string, unknown> = Record<string, unknown>,
AppState extends ServerState<AppContext> = ServerState<AppContext>,
>(boundary?: string): RouterMiddleware<string, Params, AppState> {
return async (context, next) => {
const { request, response, state } = context;
const { app } = state;
try {
await next();
} catch (cause) {
const error = HttpError.from<{ boundary?: string }>(cause);
if (isDevelopment()) error.expose = true;
app.error = error;
if (boundary) error.data.boundary = boundary;
response.status = error.status;
const extname = path.extname(request.url.pathname);
if (error.status !== 404 || extname === "" || extname === ".html") {
await state.app.render();
}
}
};
}
/**
* An Oak router that is used to render the application for get requests.
* It is used for all route components that do not have their own route middleware.
* The defaultRouter is not meant to be used directly by the user and is for internal use only.
*/
const defaultRouter = new Router<ServerState>()
.get("/", async (context: Context<ServerState>) => {
await context.state.app.render();
});
/**
* A representation of the routers for a routes directory that is used to generate an Oak router.
* This interface is meant for internal use only.
*/
export interface RouterDefinition<
AppContext extends Record<string, unknown> = Record<string, unknown>,
AppState extends ServerState<AppContext> = ServerState<AppContext>,
> {
name: string;
parent?: RouterDefinition<AppContext, AppState>;
react?: boolean;
file?: {
react?: RouteFile;
oak?: Router<AppState>;
};
main?: {
react?: RouteFile;
oak?: Router<AppState>;
};
index?: {
react?: RouteFile;
oak?: Router<AppState>;
};
children?: Record<string, RouterDefinition<AppContext, AppState>>;
}
export const ROUTE_PARAM = /^\[(.+)]$/;
export const ROUTE_WILDCARD = /^\[\.\.\.\]$/;
export function routePathFromName(name: string, forServer = false) {
if (!name) return "";
return name
.replace(ROUTE_WILDCARD, forServer ? "(.*)" : "*")
.replace(ROUTE_PARAM, ":$1");
}
export function routerPathFromName(name: string) {
return routePathFromName(name, true);
}
/**
* Generates an Oak router for a routes directory.
* The router returned by this function is the default export from the `_main.ts` file in the routes directory.
* This function is meant for internal use only.
*/
export function generateRouter<
AppContext extends Record<string, unknown> = Record<string, unknown>,
AppState extends ServerState<AppContext> = ServerState<AppContext>,
>(
options: RouterDefinition<AppContext, AppState>,
relativePath?: string,
parentBoundary?: string,
): Router<AppState> {
const { name, react, file, main, index, children, parent } = options;
const router = new Router<AppState>();
if (parent?.react && !react) {
router.use(async ({ request, response }, next) => {
try {
await next();
} catch (cause) {
const error = HttpError.from(cause);
console.error("API error", error);
response.status = error.status;
const extname = path.extname(request.url.pathname);
if (error.status !== 404 || extname === "") {
response.body = new ErrorResponse(error);
}
}
});
}
const currentPath = `${
relativePath && relativePath !== "/" ? relativePath : ""
}/${name}`;
let boundary = parentBoundary;
if (file) {
if (file.react && (file.react.ErrorFallback || file.react.boundary)) {
boundary = file.react.boundary ?? currentPath;
}
const boundaryMiddleware = react && errorBoundary(boundary);
if (file.oak) {
if (boundaryMiddleware) router.use(boundaryMiddleware);
router.use(file.oak.routes(), file.oak.allowedMethods());
} else if (file.react) {
if (boundaryMiddleware) router.use(boundaryMiddleware);
router.use(defaultRouter.routes(), defaultRouter.allowedMethods());
}
} else {
if (main?.react && (main.react.ErrorFallback || main.react.boundary)) {
boundary = main.react.boundary ?? currentPath;
}
const boundaryMiddleware = react && errorBoundary(boundary);
if (boundaryMiddleware) router.use(boundaryMiddleware);
const mainRouter = main?.oak ?? router;
if (index) {
if (index.react && (index.react.ErrorFallback || index.react.boundary)) {
mainRouter.use(
errorBoundary(
index.react.boundary ?? `${currentPath}/index`,
),
);
} else if (main?.oak && boundaryMiddleware) {
mainRouter.use(boundaryMiddleware);
}
if (index.oak) {
mainRouter.use("/", index.oak.routes(), index.oak.allowedMethods());
} else if (react) {
mainRouter.use(
"/",
defaultRouter.routes(),
defaultRouter.allowedMethods(),
);
}
if (boundaryMiddleware) mainRouter.use(boundaryMiddleware);
}
if (children) {
let notFoundRouter: Router<AppState> | undefined = undefined;
for (const [name, child] of Object.entries(children)) {
child.parent = options;
const childRouter = generateRouter(
child,
currentPath,
boundary,
);
if (name === "[...]") {
notFoundRouter = childRouter;
} else {
mainRouter.use(
`/${routerPathFromName(name)}`,
childRouter.routes(),
childRouter.allowedMethods(),
);
}
}
if (notFoundRouter) {
mainRouter.use(
`/${routerPathFromName("[...]")}`,
notFoundRouter.routes(),
notFoundRouter.allowedMethods(),
);
}
}
if (main?.oak) {
router.use(mainRouter.routes(), mainRouter.allowedMethods());
}
}
return router;
}