Skip to content

Commit b097cf2

Browse files
lbogdanCompuIves
authored andcommitted
[WIP] Added staging environment for deployment (codesandbox#282)
* Added staging environment for deployment. * Disable service worker for http.
1 parent 92ef126 commit b097cf2

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed

config/env.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ const getHost = () => {
1010
return 'http://localhost:3000';
1111
}
1212

13-
return process.env.NODE_ENV === 'development'
14-
? '*'
15-
: 'https://codesandbox.io';
13+
if (process.env.NODE_ENV === 'development') {
14+
return '*';
15+
}
16+
17+
if ('STAGING_BRANCH' in process.env) {
18+
return `http://${process.env.STAGING_BRANCH}.cs.lbogdan.tk`;
19+
}
20+
21+
return 'https://codesandbox.io';
1622
};
1723

1824
module.exports = Object.keys(process.env)
@@ -26,5 +32,6 @@ module.exports = Object.keys(process.env)
2632
'process.env.NODE_ENV': NODE_ENV,
2733
'process.env.CODESANDBOX_HOST': JSON.stringify(getHost()),
2834
'process.env.LOCAL_SERVER': !!LOCAL_SERVER,
35+
'process.env.STAGING': 'STAGING_BRANCH' in process.env,
2936
}
3037
);

src/app/utils/url-generator.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const buildEncodedUri = (strings: Array<string>, ...values: Array<string>) =>
99

1010
export const host = () => {
1111
if (process.env.NODE_ENV === 'production') {
12-
return 'codesandbox.io';
12+
return process.env.CODESANDBOX_HOST.split('//')[1];
1313
}
1414
if (process.env.LOCAL_SERVER) {
1515
return 'localhost:3000';
@@ -53,13 +53,26 @@ export const embedUrl = (sandbox: Sandbox) => {
5353
return `/embed/${sandbox.id}`;
5454
};
5555

56+
const stagingFrameUrl = (shortid: string, path: string) => {
57+
const stagingHost = process.env.CODESANDBOX_HOST.split('//')[1];
58+
const segments = stagingHost.split('.');
59+
const first = segments.shift();
60+
return `${location.protocol}//${first}-${shortid}.${segments.join(
61+
'.'
62+
)}/${path}`;
63+
};
64+
5665
export const frameUrl = (shortid: string, append: string = '') => {
5766
const path = append.indexOf('/') === 0 ? append.substr(1) : append;
5867

5968
if (process.env.LOCAL_SERVER) {
6069
return `http://localhost:3001/${path}`;
6170
}
6271

72+
if (process.env.STAGING) {
73+
return stagingFrameUrl(shortid, path);
74+
}
75+
6376
return `${location.protocol}//${shortid}.${host()}/${path}`;
6477
};
6578

src/common/registerServiceWorker.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const isLocalhost = Boolean(
1818
)
1919
);
2020

21+
const isHttp = Boolean(window.location.protocol === 'http:');
22+
2123
export default function register(swUrl, sendNotification) {
2224
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
2325
// The URL constructor is available in all browsers that support SW.
@@ -30,10 +32,10 @@ export default function register(swUrl, sendNotification) {
3032
}
3133

3234
window.addEventListener('load', () => {
33-
if (!isLocalhost) {
34-
// Is not local host. Just register service worker
35+
if (!isLocalhost && !isHttp) {
36+
// It's neither localhost nor http. Just register service worker
3537
registerValidSW(swUrl, sendNotification);
36-
} else {
38+
} else if (isLocalhost) {
3739
// This is running on localhost. Lets check if a service worker still exists or not.
3840
checkValidServiceWorker(swUrl);
3941
}

src/sandbox/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,25 @@ import registerServiceWorker from 'common/registerServiceWorker';
55
import requirePolyfills from 'common/load-dynamic-polyfills';
66
import { findMainModule } from 'app/store/entities/sandboxes/modules/selectors';
77

8-
import host from './utils/host';
9-
108
import setupHistoryListeners from './url-listeners';
119
import compile from './compile';
1210
import setupConsole from './console';
1311
import transformJSON from './console/transform-json';
1412

13+
const host = process.env.CODESANDBOX_HOST;
14+
1515
function getId() {
1616
if (process.env.LOCAL_SERVER) {
1717
return document.location.hash.replace('#', '');
1818
}
1919

20+
if (process.env.STAGING) {
21+
const segments = host.split('//')[1].split('.');
22+
const first = segments.shift();
23+
const re = RegExp(`${first}-(.*)\\.${segments.join('\\.')}`);
24+
return document.location.host.match(re)[1];
25+
}
26+
2027
return document.location.host.match(/(.*)\.codesandbox/)[1];
2128
}
2229

src/sandbox/utils/host.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)