22/** @jsx h */
33import io from 'socket.io-client' ;
44import { Terminal } from 'xterm' ;
5- import { render , h } from 'preact' ;
5+ import { render , h } from 'preact' ; // eslint-disable-line import/extensions
66import * as fit from 'xterm/lib/addons/fit/fit' ;
77import * as WebfontLoader from 'xterm-webfont' ;
88import 'xterm/dist/xterm.css' ;
@@ -12,7 +12,7 @@ import { Power3 } from 'gsap/EasePack';
1212import TweenLite from 'gsap/TweenLite' ;
1313import TimelineLite from 'gsap/TimelineLite' ;
1414
15- import getTemplate from '../../../packages/ common/templates' ;
15+ import getTemplate from '@codesandbox/ common/lib /templates' ;
1616
1717import Cube from './Cube' ;
1818
@@ -31,36 +31,15 @@ if (process.env.NODE_ENV === 'development') {
3131const rootDomain = `codesandbox.${ hostParts [ hostParts . length - 1 ] } ` ;
3232const domain = `sse.${ rootDomain } ` ;
3333const 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
6645function 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