Skip to content

Commit c906efc

Browse files
dceddiajarlef
authored andcommitted
Enable proxying of websockets (facebook#1090)
Added `ws: true` to the httpProxyMiddleware options, and also listen for the "upgrade" event so that websockets can be proxied immediately, rather than waiting for an initial HTTP request.
1 parent 2947c1e commit c906efc

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

packages/react-scripts/scripts/start.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,25 @@ function addMiddleware(devServer) {
181181
// - /sockjs-node/* (WebpackDevServer uses this for hot reloading)
182182
// Tip: use https://jex.im/regulex/ to visualize the regex
183183
var mayProxy = /^(?!\/(index\.html$|.*\.hot-update\.json$|sockjs-node\/)).*$/;
184-
devServer.use(mayProxy,
185-
// Pass the scope regex both to Express and to the middleware for proxying
186-
// of both HTTP and WebSockets to work without false positives.
187-
httpProxyMiddleware(pathname => mayProxy.test(pathname), {
188-
target: proxy,
189-
logLevel: 'silent',
190-
onError: onProxyError(proxy),
191-
secure: false,
192-
changeOrigin: true
193-
})
194-
);
184+
185+
// Pass the scope regex both to Express and to the middleware for proxying
186+
// of both HTTP and WebSockets to work without false positives.
187+
var hpm = httpProxyMiddleware(pathname => mayProxy.test(pathname), {
188+
target: proxy,
189+
logLevel: 'silent',
190+
onError: onProxyError(proxy),
191+
secure: false,
192+
changeOrigin: true,
193+
ws: true
194+
});
195+
devServer.use(mayProxy, hpm);
196+
197+
// Listen for the websocket 'upgrade' event and upgrade the connection.
198+
// If this is not done, httpProxyMiddleware will not try to upgrade until
199+
// an initial plain HTTP request is made.
200+
devServer.listeningApp.on('upgrade', hpm.upgrade);
195201
}
202+
196203
// Finally, by now we have certainly resolved the URL.
197204
// It may be /index.html, so let the dev server try serving it again.
198205
devServer.use(devServer.middleware);

0 commit comments

Comments
 (0)