Skip to content

Commit 336e556

Browse files
committed
Add support for functions.
1 parent 8e10689 commit 336e556

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

packages/remix/src/utils/serverAdapters/express.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import type {
2020

2121
function wrapExpressRequestHandler(
2222
origRequestHandler: ExpressRequestHandler,
23-
build: ServerBuild | Promise<ServerBuild>,
23+
build: ServerBuild | Promise<ServerBuild> | (() => Promise<ServerBuild> | ServerBuild),
2424
): ExpressRequestHandler {
2525
let routes: ServerRoute[];
2626

27-
if ('routes' in build) {
27+
if (build && 'routes' in build) {
2828
routes = createRoutes(build.routes);
2929
}
3030

@@ -53,8 +53,20 @@ function wrapExpressRequestHandler(
5353

5454
// This is only meant to be used on development servers, so we don't need to worry about performance here
5555
if (!routes) {
56-
const resolvedBuild = await build;
57-
routes = createRoutes(resolvedBuild.routes);
56+
if (build instanceof Promise) {
57+
const resolvedBuild = await build;
58+
routes = createRoutes(resolvedBuild.routes);
59+
}
60+
61+
if (typeof build === 'function') {
62+
const resolvedBuild = build();
63+
if (resolvedBuild instanceof Promise) {
64+
const resolved = await resolvedBuild;
65+
routes = createRoutes(resolved.routes);
66+
} else {
67+
routes = createRoutes(resolvedBuild.routes);
68+
}
69+
}
5870
}
5971

6072
const [name, source] = getTransactionName(routes, url);
@@ -84,12 +96,24 @@ function wrapGetLoadContext(origGetLoadContext: () => AppLoadContext): GetLoadCo
8496
}
8597

8698
// A wrapper around build if it's a Promise or a function that returns a Promise that calls instrumentServer on the resolved value
87-
// This is currently only required for development mode with HMR
88-
function wrapBuild(build: ServerBuild | Promise<ServerBuild>): ServerBuild | Promise<ServerBuild> {
99+
// This is currently only required for development mode with HM
100+
101+
function wrapBuild(
102+
build: ServerBuild | Promise<ServerBuild> | (() => Promise<ServerBuild> | ServerBuild),
103+
): ServerBuild | Promise<ServerBuild> {
89104
if (build instanceof Promise) {
90105
return build.then(resolved => instrumentBuild(resolved, true));
91106
}
92107

108+
if (typeof build === 'function') {
109+
const resolved = build();
110+
if (resolved instanceof Promise) {
111+
return resolved.then(resolved => instrumentBuild(resolved, true));
112+
}
113+
114+
return instrumentBuild(resolved, true);
115+
}
116+
93117
return instrumentBuild(build, true);
94118
}
95119

0 commit comments

Comments
 (0)