@@ -33,39 +33,21 @@ module.exports = function connect() {
33
33
routes
34
34
) ;
35
35
36
- const dev_middleware = dev ? require ( 'webpack-dev-middleware' ) ( client , {
37
- noInfo : true ,
38
- logLevel : 'silent' ,
39
- publicPath : '/client/'
40
- } ) : null ;
41
-
42
- const hot_middleware = dev ? require ( 'webpack-hot-middleware' ) ( client , {
43
- reload : true ,
44
- path : '/__webpack_hmr' ,
45
- heartbeat : 10 * 1000
46
- } ) : null ;
47
-
48
- async function handle_webpack_generated_files ( url , req , res , next ) {
49
- if ( dev ) {
50
- dev_middleware ( req , res , ( ) => {
51
- hot_middleware ( req , res , next ) ;
36
+ async function handle_webpack_generated_files ( req , res , next ) {
37
+ if ( req . pathname . startsWith ( '/client/' ) ) {
38
+ await compiler . ready ;
39
+ res . set ( {
40
+ 'Content-Type' : 'application/javascript' ,
41
+ 'Cache-Control' : 'max-age=31536000'
52
42
} ) ;
43
+ res . end ( compiler . asset_cache [ req . pathname ] ) ;
53
44
} else {
54
- if ( url . startsWith ( '/client/' ) ) {
55
- await compiler . ready ;
56
- res . set ( {
57
- 'Content-Type' : 'application/javascript' ,
58
- 'Cache-Control' : 'max-age=31536000'
59
- } ) ;
60
- res . end ( compiler . asset_cache [ url ] ) ;
61
- } else {
62
- next ( ) ;
63
- }
45
+ next ( ) ;
64
46
}
65
47
}
66
48
67
- async function handle_index ( url , req , res , next ) {
68
- if ( url === '/index.html' ) {
49
+ async function handle_index ( req , res , next ) {
50
+ if ( req . pathname === '/index.html' ) {
69
51
await compiler . ready ;
70
52
res . set ( {
71
53
'Content-Type' : 'text/html' ,
@@ -77,8 +59,8 @@ module.exports = function connect() {
77
59
}
78
60
}
79
61
80
- async function handle_service_worker ( url , req , res , next ) {
81
- if ( url === '/service-worker.js' ) {
62
+ async function handle_service_worker ( req , res , next ) {
63
+ if ( req . pathname === '/service-worker.js' ) {
82
64
await compiler . ready ;
83
65
res . set ( {
84
66
'Content-Type' : 'application/javascript' ,
@@ -90,7 +72,9 @@ module.exports = function connect() {
90
72
}
91
73
}
92
74
93
- async function handle_route ( url , req , res , next ) {
75
+ async function handle_route ( req , res , next ) {
76
+ const url = req . pathname ;
77
+
94
78
// whatever happens, we're going to serve some HTML
95
79
res . set ( {
96
80
'Content-Type' : 'text/html'
@@ -147,15 +131,41 @@ module.exports = function connect() {
147
131
}
148
132
}
149
133
150
- return async function ( req , res , next ) {
151
- const url = req . url . replace ( / \? .+ / , '' ) ;
152
-
153
- handle_index ( url , req , res , ( ) => {
154
- handle_service_worker ( url , req , res , ( ) => {
155
- handle_webpack_generated_files ( url , req , res , ( ) => {
156
- handle_route ( url , req , res , next ) ;
157
- } ) ;
158
- } ) ;
159
- } ) ;
134
+ const handler = compose_handlers ( [
135
+ dev && require ( 'webpack-hot-middleware' ) ( client , {
136
+ reload : true ,
137
+ path : '/__webpack_hmr' ,
138
+ heartbeat : 10 * 1000
139
+ } ) ,
140
+
141
+ handle_index ,
142
+ handle_service_worker ,
143
+ handle_webpack_generated_files ,
144
+ handle_route
145
+ ] . filter ( Boolean ) ) ;
146
+
147
+ return function ( req , res , next ) {
148
+ req . pathname = req . url . replace ( / \? .+ / , '' ) ;
149
+ handler ( req , res , next ) ;
160
150
} ;
161
- } ;
151
+ } ;
152
+
153
+ function compose_handlers ( handlers ) {
154
+ return ( req , res , next ) => {
155
+ let i = 0 ;
156
+ function go ( ) {
157
+ const handler = handlers [ i ] ;
158
+
159
+ if ( handler ) {
160
+ handler ( req , res , ( ) => {
161
+ i += 1 ;
162
+ go ( ) ;
163
+ } ) ;
164
+ } else {
165
+ next ( ) ;
166
+ }
167
+ }
168
+
169
+ go ( ) ;
170
+ }
171
+ }
0 commit comments