Skip to content

Commit 240049a

Browse files
committed
Fix WS proxy
1 parent c05030a commit 240049a

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

packages/app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
"overmind": "^20.1.0-1572182984474",
161161
"overmind-devtools": "^19.0.0",
162162
"overmind-react": "^21.1.0-1572182984474",
163-
"phoenix": "^1.3.0",
163+
"phoenix": "^1.4.11",
164164
"postcss": "^6.0.9",
165165
"postcss-selector-parser": "^2.2.3",
166166
"posthtml": "^0.11.3",

packages/app/scripts/start.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const { staticAssets } = require('../config/build');
2020

2121
// Tools like Cloud9 rely on this.
2222
var DEFAULT_PORT = process.env.PORT || 3000;
23+
const PROXY_DOMAIN = 'https://codesandbox.io';
2324
var compiler;
2425
var handleCompile;
2526
var compileStart;
@@ -214,6 +215,7 @@ function addMiddleware(devServer, index) {
214215
rewrites: [{ from: /\/embed/, 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

251266
function 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

290309
function run(port) {

packages/app/src/app/overmind/effects/live/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ export default {
7474
},
7575
connect() {
7676
if (!_socket) {
77-
_socket = new Socket(`wss://${location.host}/socket`, {
77+
const protocol = process.env.LOCAL_SERVER ? 'ws' : 'wss';
78+
_socket = new Socket(`${protocol}://${location.host}/socket`, {
7879
params: {
7980
guardian_token: provideJwtToken(),
8081
},

yarn.lock

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19651,9 +19651,10 @@ phin@^2.9.1:
1965119651
resolved "https://registry.yarnpkg.com/phin/-/phin-2.9.3.tgz#f9b6ac10a035636fb65dfc576aaaa17b8743125c"
1965219652
integrity sha512-CzFr90qM24ju5f88quFC/6qohjC144rehe5n6DH900lgXmUe86+xCKc10ev56gRKC4/BkHUoG4uSiQgBiIXwDA==
1965319653

19654-
phoenix@^1.3.0:
19655-
version "1.3.0"
19656-
resolved "https://registry.yarnpkg.com/phoenix/-/phoenix-1.3.0.tgz#1df2c27f986ee295e37c9983ec28ebac1d7f4a3e"
19654+
phoenix@^1.4.11:
19655+
version "1.4.11"
19656+
resolved "https://registry.yarnpkg.com/phoenix/-/phoenix-1.4.11.tgz#3e70cd2461600ef3b0d4308e8fef7d2005fe000a"
19657+
integrity sha512-UkuqKB/+Uy9LNt15v1PpPeoLizcYwF4cFTi1wiMZ2TVX5+2Pj7MbRDFmoUFPc+Z1jsOE/TdQ6kzZohErWMiYlQ==
1965719658

1965819659
physical-cpu-count@^2.0.0:
1965919660
version "2.0.0"

0 commit comments

Comments
 (0)