@@ -20,6 +20,7 @@ const { staticAssets } = require('../config/build');
2020
2121// Tools like Cloud9 rely on this.
2222var DEFAULT_PORT = process . env . PORT || 3000 ;
23+ const PROXY_DOMAIN = 'https://codesandbox.io' ;
2324var compiler ;
2425var handleCompile ;
2526var compileStart ;
@@ -214,6 +215,7 @@ function addMiddleware(devServer, index) {
214215 rewrites : [ { from : / \/ e m b e d / , to : '/embed.html' } ] ,
215216 } )
216217 ) ;
218+ let wsProxy ;
217219 if ( process . env . LOCAL_SERVER ) {
218220 devServer . use (
219221 cors ( {
@@ -226,10 +228,21 @@ function addMiddleware(devServer, index) {
226228 credentials : true ,
227229 } )
228230 ) ;
231+ wsProxy = proxy ( {
232+ target : PROXY_DOMAIN . replace ( 'https' , 'wss' ) ,
233+ changeOrigin : true ,
234+ ws : true ,
235+ autoRewrite : true ,
236+ protocolRewrite : true ,
237+ onProxyReqWs ( proxyReq , req , socket , options , head ) {
238+ proxyReq . setHeader ( 'Origin' , PROXY_DOMAIN ) ;
239+ } ,
240+ } ) ;
241+ devServer . use ( '/socket' , wsProxy ) ;
229242 devServer . use (
230243 '/api' ,
231244 proxy ( {
232- target : 'https://codesandbox.io' ,
245+ target : PROXY_DOMAIN ,
233246 changeOrigin : true ,
234247 } )
235248 ) ;
@@ -246,6 +259,8 @@ function addMiddleware(devServer, index) {
246259 // Finally, by now we have certainly resolved the URL.
247260 // It may be /index.html, so let the dev server try serving it again.
248261 devServer . use ( devServer . middleware ) ;
262+
263+ return { wsProxy } ;
249264}
250265
251266function runDevServer ( port , protocol , index ) {
@@ -273,7 +288,7 @@ function runDevServer(port, protocol, index) {
273288 } ) ;
274289
275290 // Our custom middleware proxies requests to /index.html or a remote API.
276- addMiddleware ( devServer , index ) ;
291+ const { wsProxy } = addMiddleware ( devServer , index ) ;
277292
278293 // Launch WebpackDevServer.
279294 devServer . listen ( port , err => {
@@ -285,6 +300,10 @@ function runDevServer(port, protocol, index) {
285300 console . log ( chalk . cyan ( 'Starting the development server...' ) ) ;
286301 openBrowser ( port , protocol ) ;
287302 } ) ;
303+
304+ if ( wsProxy ) {
305+ devServer . listeningApp . on ( 'upgrade' , wsProxy . upgrade ) ;
306+ }
288307}
289308
290309function run ( port ) {
0 commit comments