Skip to content

Commit a3b21c0

Browse files
authored
Add separate sandbox host (codesandbox#2083)
* Fix getId() to also check the sandbox host. * Update frameUrl() to use the sandbox host. * Refactor: move get(sandbox)Id() to common. * frameUrl(): fix checking for sandbox host.
1 parent ae26f2c commit a3b21c0

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

packages/app/src/sandbox/index.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import registerServiceWorker from '@codesandbox/common/lib/registerServiceWorker
77
import requirePolyfills from '@codesandbox/common/lib/load-dynamic-polyfills';
88
import { getModulePath } from '@codesandbox/common/lib/sandbox/modules';
99
import { generateFileFromSandbox } from '@codesandbox/common/lib/templates/configuration/package-json';
10+
import { getSandboxId } from '@codesandbox/common/lib/utils/url-generator';
1011
import setupConsole from 'sandbox-hooks/console';
1112
import setupHistoryListeners from 'sandbox-hooks/url-listeners';
1213

@@ -20,23 +21,6 @@ export const SCRIPT_VERSION =
2021

2122
debug('Booting sandbox');
2223

23-
function getId() {
24-
if (process.env.LOCAL_SERVER) {
25-
return document.location.hash.replace('#', '');
26-
}
27-
28-
if (process.env.STAGING) {
29-
const segments = host.split('//')[1].split('.');
30-
const first = segments.shift();
31-
const re = RegExp(`${first}-(.*)\\.${segments.join('\\.')}`);
32-
return document.location.host.match(re)[1];
33-
}
34-
35-
const hostRegex = host.replace(/https?:\/\//, '').replace(/\./g, '\\.');
36-
const sandboxRegex = new RegExp(`(.*)\\.${hostRegex}`);
37-
return document.location.host.match(sandboxRegex)[1];
38-
}
39-
4024
requirePolyfills().then(() => {
4125
registerServiceWorker('/sandbox-service-worker.js', {});
4226

@@ -86,7 +70,7 @@ requirePolyfills().then(() => {
8670

8771
if (process.env.NODE_ENV === 'test' || isStandalone) {
8872
// We need to fetch the sandbox ourselves...
89-
const id = getId();
73+
const id = getSandboxId();
9074
window
9175
.fetch(host + `/api/v1/sandboxes/${id}`, {
9276
credentials: 'include',

packages/common/src/utils/url-generator.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ export const gitHubRepoPattern = /(https?:\/\/)?((www.)?)github.com(\/[\w-]+){2,
44
const gitHubPrefix = /(https?:\/\/)?((www.)?)github.com/;
55
const dotGit = /(\.git)$/;
66

7+
const sandboxHost = {
8+
'https://codesandbox.io': 'https://csb.dev',
9+
'https://codesandbox.stream': 'https://codesandbox.dev',
10+
};
11+
712
const buildEncodedUri = (
813
strings: TemplateStringsArray,
914
...values: Array<string>
@@ -99,7 +104,11 @@ export const frameUrl = (sandbox: Sandbox, append: string = '') => {
99104
return stagingFrameUrl(sandbox.id, path);
100105
}
101106

102-
return `${location.protocol}//${sandbox.id}.${host()}/${path}`;
107+
let sHost = host();
108+
if (`https://${sHost}` in sandboxHost) {
109+
sHost = sandboxHost[`https://${sHost}`].split('//')[1];
110+
}
111+
return `${location.protocol}//${sandbox.id}.${sHost}/${path}`;
103112
};
104113

105114
export const forkSandboxUrl = (sandbox: Sandbox) =>
@@ -153,3 +162,34 @@ export const patronUrl = () => `/patron`;
153162
export const curatorUrl = () => `/curator`;
154163
export const tosUrl = () => `/legal/terms`;
155164
export const privacyUrl = () => `/legal/privacy`;
165+
166+
export function getSandboxId() {
167+
const host = process.env.CODESANDBOX_HOST;
168+
169+
if (process.env.LOCAL_SERVER) {
170+
return document.location.hash.replace('#', '');
171+
}
172+
173+
if (process.env.STAGING) {
174+
const segments = host.split('//')[1].split('.');
175+
const first = segments.shift();
176+
const re = RegExp(`${first}-(.*)\\.${segments.join('\\.')}`);
177+
return document.location.host.match(re)[1];
178+
}
179+
180+
let result: string;
181+
[host, sandboxHost[host]].filter(Boolean).forEach(tryHost => {
182+
const hostRegex = tryHost.replace(/https?:\/\//, '').replace(/\./g, '\\.');
183+
const sandboxRegex = new RegExp(`(.*)\\.${hostRegex}`);
184+
const matches = document.location.host.match(sandboxRegex);
185+
if (matches) {
186+
result = matches[1];
187+
}
188+
});
189+
190+
if (!result) {
191+
throw new Error(`Can't detect sandbox ID from the current URL`);
192+
}
193+
194+
return result;
195+
}

0 commit comments

Comments
 (0)