Skip to content

Commit 1083314

Browse files
lbogdanCompuIves
authored andcommitted
Container loading screen improvements (codesandbox#1237)
* SSE loading screen: force reload the current URL after sandbox starts. * Disabled source maps. * Added unrecoverable error handling. * Added reload loop prevention. * Remove (now) unneeded .babelrc. * Fix templates import path. * Use @codesandbox/common package. * Try websocket transport first, and fall back to polling if not supported.
1 parent 7d27f0f commit 1083314

File tree

3 files changed

+65
-42
lines changed

3 files changed

+65
-42
lines changed

standalone-packages/sse-loading-screen/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"main": "index.js",
55
"license": "MIT",
66
"dependencies": {
7+
"@codesandbox/common": "^1.0.4",
78
"babel-plugin-emotion": "^9.2.10",
89
"emotion": "^9.2.10",
910
"gsap": "^2.0.2",
@@ -24,7 +25,7 @@
2425
},
2526
"scripts": {
2627
"start": "parcel src/index.html",
27-
"build": "rimraf dist && parcel build src/index.html",
28+
"build": "rimraf dist && parcel build src/index.html --no-source-maps",
2829
"install-dependencies": "yarn install"
2930
}
3031
}

standalone-packages/sse-loading-screen/src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</div>
3737
</div>
3838

39-
<!-- <script src="./index.js" type="application/javascript"></script> -->
39+
<script src="./index.js" type="application/javascript"></script>
4040
</body>
4141

4242
</html>

standalone-packages/sse-loading-screen/src/index.js

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/** @jsx h */
33
import io from 'socket.io-client';
44
import { Terminal } from 'xterm';
5-
import { render, h } from 'preact';
5+
import { render, h } from 'preact'; // eslint-disable-line import/extensions
66
import * as fit from 'xterm/lib/addons/fit/fit';
77
import * as WebfontLoader from 'xterm-webfont';
88
import 'xterm/dist/xterm.css';
@@ -12,7 +12,7 @@ import { Power3 } from 'gsap/EasePack';
1212
import TweenLite from 'gsap/TweenLite';
1313
import TimelineLite from 'gsap/TimelineLite';
1414

15-
import getTemplate from '../../../packages/common/templates';
15+
import getTemplate from '@codesandbox/common/lib/templates';
1616

1717
import Cube from './Cube';
1818

@@ -31,36 +31,15 @@ if (process.env.NODE_ENV === 'development') {
3131
const rootDomain = `codesandbox.${hostParts[hostParts.length - 1]}`;
3232
const domain = `sse.${rootDomain}`;
3333
const sandbox = hostParts[0];
34-
35-
if (process.env.NODE_ENV === 'production') {
36-
fetch(`https://${rootDomain}/api/v1/sandboxes/${sandbox}/slim`)
37-
.then(res => {
38-
if (res.status === 404) {
39-
window.location.replace(`https://${rootDomain}/s/${sandbox}`);
40-
return {};
41-
}
42-
43-
return res.json();
44-
})
45-
.then(json => {
46-
if (Object.keys(json) > 0 && !json.is_sse) {
47-
window.location.replace(`https://${sandbox}.${rootDomain}/`);
48-
}
49-
if (json.template) {
50-
const templateDef = getTemplate(json.template);
51-
52-
color =
53-
json.template === 'next'
54-
? templateDef.color.darken(0.3)
55-
: templateDef.color;
56-
}
57-
});
58-
} else {
59-
setTimeout(() => {
60-
const templateDef = getTemplate('gatsby');
61-
62-
color = templateDef.color;
63-
}, 1200);
34+
const lastLoadedAt = parseInt(localStorage.getItem('last_loaded_at'), 10);
35+
const now = Date.now();
36+
let isLoop = false;
37+
38+
if (lastLoadedAt) {
39+
const timeDiff = now - lastLoadedAt;
40+
if (timeDiff <= 5000) {
41+
isLoop = true;
42+
}
6443
}
6544

6645
function createCube(element, id, noAnimation = false, styles = {}) {
@@ -173,9 +152,9 @@ const createMainCube = () => {
173152
'-=.1'
174153
);
175154

176-
//topfront 0 -16
177-
//topleft -52 -42
178-
//topright 53 -41
155+
// topfront 0 -16
156+
// topleft -52 -42
157+
// topright 53 -41
179158

180159
// createCube(cubeEl, 'second', true);
181160
// TweenLite.to('#second', 0.5, { x: 53, y: 26 });
@@ -225,17 +204,25 @@ async function start() {
225204

226205
const socket = io(`https://${domain}`, {
227206
autoConnect: false,
207+
transports: ['websocket', 'polling'],
228208
});
229209

230210
socket.on('connect', () => {
231211
socket.emit('sandbox', { id: sandbox });
232212
socket.emit('sandbox:start');
233213
});
234214

235-
socket.on('sandbox:log', ({ chan, data }) => {
215+
socket.on('sandbox:log', ({ data }) => {
236216
term.write(data);
237217
});
238218

219+
socket.on('sandbox:error', ({ message, unrecoverable }) => {
220+
if (unrecoverable) {
221+
document.getElementById('loading-text').textContent = message;
222+
socket.close();
223+
}
224+
});
225+
239226
window.s = socket;
240227

241228
const maximizeTerminal = () => {
@@ -278,9 +265,7 @@ async function start() {
278265
updateStatus(4, 3, 'started');
279266

280267
setTimeout(() => {
281-
// if (process.env.NODE_ENV === 'production') {
282-
window.location.replace(`https://${sandbox}.${domain}/`);
283-
// }
268+
window.location.reload(true);
284269
}, 100);
285270
});
286271

@@ -289,4 +274,41 @@ async function start() {
289274
socket.connect();
290275
}
291276

292-
start();
277+
if (isLoop) {
278+
document.getElementById('loading-text').textContent = 'Error: Reloading too fast.';
279+
} else {
280+
localStorage.setItem('last_loaded_at', now);
281+
282+
if (process.env.NODE_ENV === 'production') {
283+
fetch(`https://${rootDomain}/api/v1/sandboxes/${sandbox}/slim`)
284+
.then(res => {
285+
if (res.status === 404) {
286+
window.location.replace(`https://${rootDomain}/s/${sandbox}`);
287+
return {};
288+
}
289+
290+
return res.json();
291+
})
292+
.then(json => {
293+
if (Object.keys(json) > 0 && !json.is_sse) {
294+
window.location.replace(`https://${sandbox}.${rootDomain}/`);
295+
}
296+
if (json.template) {
297+
const templateDef = getTemplate(json.template);
298+
299+
color =
300+
json.template === 'next'
301+
? templateDef.color.darken(0.3)
302+
: templateDef.color;
303+
}
304+
});
305+
} else {
306+
setTimeout(() => {
307+
const templateDef = getTemplate('gatsby');
308+
309+
color = templateDef.color;
310+
}, 1200);
311+
}
312+
313+
start();
314+
}

0 commit comments

Comments
 (0)