@@ -20,11 +20,11 @@ import type {
20
20
21
21
function wrapExpressRequestHandler (
22
22
origRequestHandler : ExpressRequestHandler ,
23
- build : ServerBuild | Promise < ServerBuild > ,
23
+ build : ServerBuild | Promise < ServerBuild > | ( ( ) => Promise < ServerBuild > | ServerBuild ) ,
24
24
) : ExpressRequestHandler {
25
25
let routes : ServerRoute [ ] ;
26
26
27
- if ( 'routes' in build ) {
27
+ if ( build && 'routes' in build ) {
28
28
routes = createRoutes ( build . routes ) ;
29
29
}
30
30
@@ -53,8 +53,20 @@ function wrapExpressRequestHandler(
53
53
54
54
// This is only meant to be used on development servers, so we don't need to worry about performance here
55
55
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
+ }
58
70
}
59
71
60
72
const [ name , source ] = getTransactionName ( routes , url ) ;
@@ -84,12 +96,24 @@ function wrapGetLoadContext(origGetLoadContext: () => AppLoadContext): GetLoadCo
84
96
}
85
97
86
98
// 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 > {
89
104
if ( build instanceof Promise ) {
90
105
return build . then ( resolved => instrumentBuild ( resolved , true ) ) ;
91
106
}
92
107
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
+
93
117
return instrumentBuild ( build , true ) ;
94
118
}
95
119
0 commit comments