Skip to content

Commit 905cc7a

Browse files
authored
Fix checking if preview is standalone (codesandbox#3393)
* Fix checking if preview is standalone. * Drone: bump deploy-plugin version. * Drone: update signature. * Disable metrics in PR builds. * Disable analytics in development and PR builds.
1 parent 7753555 commit 905cc7a

File tree

7 files changed

+105
-101
lines changed

7 files changed

+105
-101
lines changed

.drone.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ steps:
2626
- yarn build
2727

2828
- name: deploy
29-
image: lbogdan/codesandbox-deploy-plugin:3
29+
image: lbogdan/codesandbox-deploy-plugin:4
3030
settings:
3131
domain: &domain build.csb.dev
3232
volumes:
@@ -124,7 +124,7 @@ steps:
124124
- yarn build
125125

126126
- name: deploy
127-
image: lbogdan/codesandbox-deploy-plugin:2
127+
image: lbogdan/codesandbox-deploy-plugin:4
128128
settings:
129129
domain: &domain build.csb.dev
130130
volumes:
@@ -177,6 +177,6 @@ trigger:
177177
branch: master
178178
---
179179
kind: signature
180-
hmac: e625eaf2e469b9b6763641d4cc1f089900ef8d8c41db32c6f6aacb27bee8c19c
180+
hmac: a229340365980b689bbe4c5f92d721cf3b6ce176670293a925f329501a054fe4
181181

182182
...

packages/app/src/sandbox/startup.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import BabelWorker from 'worker-loader?publicPath=/&name=babel-transpiler.[hash:
44
import hookConsole from 'sandbox-hooks/console';
55
import setupHistoryListeners from 'sandbox-hooks/url-listeners';
66
import { listenForPreviewSecret } from 'sandbox-hooks/preview-secret';
7+
import { isStandalone } from 'codesandbox-api';
78

89
window.babelworkers = [];
910
for (let i = 0; i < 3; i++) {
1011
window.babelworkers.push(new BabelWorker());
1112
}
1213

13-
if (!window.opener) {
14+
if (!isStandalone) {
1415
// Means we're in the editor
1516
setupHistoryListeners();
1617
hookConsole();

packages/app/src/sandbox/utils/metrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function persistMeasurements(data: {
4242
browser: string;
4343
version: string;
4444
}) {
45-
if (process.env.NODE_ENV === 'development') {
45+
if (process.env.NODE_ENV === 'development' || process.env.STAGING) {
4646
return Promise.resolve();
4747
}
4848

packages/common/src/utils/analytics/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ export const DO_NOT_TRACK_ENABLED =
7676
global.navigator.doNotTrack === '1' ||
7777
// @ts-ignore
7878
global.navigator.msDoNotTrack === '1' ||
79-
localStorage.getItem('DO_NOT_TRACK_ENABLED')
79+
localStorage.getItem('DO_NOT_TRACK_ENABLED') ||
80+
process.env.NODE_ENV === 'development' ||
81+
process.env.STAGING
8082
);
8183

8284
export const isAllowedEvent = (eventName, secondArg) => {
Lines changed: 87 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dispatch, isStandalone, listen } from 'codesandbox-api';
1+
import { dispatch, listen } from 'codesandbox-api';
22

33
const origHistoryProto = window.history.__proto__; // eslint-disable-line no-proto
44
const historyList = [];
@@ -37,107 +37,105 @@ export default function setupHistoryListeners() {
3737
}
3838
}
3939
}
40-
if (!isStandalone) {
41-
Object.assign(window.history, {
42-
go(delta) {
43-
const newPos = historyPosition + delta;
44-
if (newPos >= 0 && newPos <= historyList.length - 1) {
45-
historyPosition = newPos;
46-
const { url, state } = historyList[historyPosition];
47-
const oldURL = document.location.href;
48-
origHistoryProto.replaceState.call(window.history, state, '', url);
49-
const newURL = document.location.href;
50-
sendUrlChange(newURL);
51-
if (newURL.indexOf('#') === -1) {
52-
window.dispatchEvent(new PopStateEvent('popstate', { state }));
53-
} else {
54-
disableNextHashChange = true;
55-
window.dispatchEvent(
56-
new HashChangeEvent('hashchange', { oldURL, newURL })
57-
);
58-
}
40+
Object.assign(window.history, {
41+
go(delta) {
42+
const newPos = historyPosition + delta;
43+
if (newPos >= 0 && newPos <= historyList.length - 1) {
44+
historyPosition = newPos;
45+
const { url, state } = historyList[historyPosition];
46+
const oldURL = document.location.href;
47+
origHistoryProto.replaceState.call(window.history, state, '', url);
48+
const newURL = document.location.href;
49+
sendUrlChange(newURL);
50+
if (newURL.indexOf('#') === -1) {
51+
window.dispatchEvent(new PopStateEvent('popstate', { state }));
52+
} else {
53+
disableNextHashChange = true;
54+
window.dispatchEvent(
55+
new HashChangeEvent('hashchange', { oldURL, newURL })
56+
);
5957
}
60-
},
58+
}
59+
},
6160

62-
back() {
63-
window.history.go(-1);
64-
},
61+
back() {
62+
window.history.go(-1);
63+
},
6564

66-
forward() {
67-
window.history.go(1);
68-
},
65+
forward() {
66+
window.history.go(1);
67+
},
6968

70-
pushState(state, title, url) {
71-
origHistoryProto.replaceState.call(window.history, state, title, url);
72-
pushHistory(url, state);
73-
sendUrlChange(document.location.href);
74-
},
69+
pushState(state, title, url) {
70+
origHistoryProto.replaceState.call(window.history, state, title, url);
71+
pushHistory(url, state);
72+
sendUrlChange(document.location.href);
73+
},
7574

76-
replaceState(state, title, url) {
77-
origHistoryProto.replaceState.call(window.history, state, title, url);
78-
historyList[historyPosition] = { state, url };
79-
sendUrlChange(document.location.href);
80-
},
81-
});
75+
replaceState(state, title, url) {
76+
origHistoryProto.replaceState.call(window.history, state, title, url);
77+
historyList[historyPosition] = { state, url };
78+
sendUrlChange(document.location.href);
79+
},
80+
});
8281

83-
Object.defineProperties(window.history, {
84-
length: {
85-
get() {
86-
return historyList.length;
87-
},
82+
Object.defineProperties(window.history, {
83+
length: {
84+
get() {
85+
return historyList.length;
8886
},
87+
},
8988

90-
state: {
91-
get() {
92-
return historyList[historyPosition].state;
93-
},
89+
state: {
90+
get() {
91+
return historyList[historyPosition].state;
9492
},
95-
});
93+
},
94+
});
9695

97-
window.addEventListener('hashchange', () => {
98-
if (!disableNextHashChange) {
99-
const url = pathWithHash(document.location);
100-
pushHistory(url, null);
101-
sendUrlChange(document.location.href);
102-
} else {
103-
disableNextHashChange = false;
104-
}
105-
});
96+
window.addEventListener('hashchange', () => {
97+
if (!disableNextHashChange) {
98+
const url = pathWithHash(document.location);
99+
pushHistory(url, null);
100+
sendUrlChange(document.location.href);
101+
} else {
102+
disableNextHashChange = false;
103+
}
104+
});
106105

107-
document.addEventListener(
108-
'click',
109-
ev => {
110-
const el = ev.target;
111-
if (
112-
el.nodeName === 'A' &&
113-
!el.__vue__ && // workaround for vue-router <router-link>
114-
el.href.indexOf('#') !== -1 &&
115-
el.href.substr(-1) !== '#'
116-
) {
117-
const url = el.href;
118-
const oldURL = document.location.href;
119-
origHistoryProto.replaceState.call(window.history, null, '', url);
120-
const newURL = document.location.href;
121-
if (oldURL !== newURL) {
122-
disableNextHashChange = true;
123-
window.dispatchEvent(
124-
new HashChangeEvent('hashchange', { oldURL, newURL })
125-
);
126-
pushHistory(pathWithHash(document.location), null);
127-
sendUrlChange(document.location.href);
128-
}
129-
ev.preventDefault();
130-
ev.stopPropagation();
106+
document.addEventListener(
107+
'click',
108+
ev => {
109+
const el = ev.target;
110+
if (
111+
el.nodeName === 'A' &&
112+
!el.__vue__ && // workaround for vue-router <router-link>
113+
el.href.indexOf('#') !== -1 &&
114+
el.href.substr(-1) !== '#'
115+
) {
116+
const url = el.href;
117+
const oldURL = document.location.href;
118+
origHistoryProto.replaceState.call(window.history, null, '', url);
119+
const newURL = document.location.href;
120+
if (oldURL !== newURL) {
121+
disableNextHashChange = true;
122+
window.dispatchEvent(
123+
new HashChangeEvent('hashchange', { oldURL, newURL })
124+
);
125+
pushHistory(pathWithHash(document.location), null);
126+
sendUrlChange(document.location.href);
131127
}
132-
},
133-
true
134-
);
128+
ev.preventDefault();
129+
ev.stopPropagation();
130+
}
131+
},
132+
true
133+
);
135134

136-
pushHistory(pathWithHash(document.location), null);
135+
pushHistory(pathWithHash(document.location), null);
137136

138-
setTimeout(() => {
139-
sendUrlChange(document.location.href);
140-
});
141-
}
137+
setTimeout(() => {
138+
sendUrlChange(document.location.href);
139+
});
142140
return listen(handleMessage);
143141
}

packages/sse-hooks/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import hookConsole from 'sandbox-hooks/console';
22
import setupHistoryListeners from 'sandbox-hooks/url-listeners';
33
import { listenForPreviewSecret } from 'sandbox-hooks/preview-secret';
4-
import { dispatch } from 'codesandbox-api';
4+
import { dispatch, isStandalone } from 'codesandbox-api';
55

6-
hookConsole();
7-
setupHistoryListeners();
8-
listenForPreviewSecret();
9-
dispatch({ type: 'initialized' });
6+
if (!isStandalone) {
7+
hookConsole();
8+
setupHistoryListeners();
9+
listenForPreviewSecret();
10+
dispatch({ type: 'initialized' });
11+
}
1012

1113
setTimeout(() => {
1214
if (typeof window.__puppeteer__ === 'function') {

packages/sse-hooks/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"build:dev": "rollup -c rollup.config.js"
1414
},
1515
"dependencies": {
16-
"codesandbox-api": "0.0.24"
16+
"codesandbox-api": "0.0.24",
17+
"sandbox-hooks": "0.1.0"
1718
},
1819
"devDependencies": {
1920
"@babel/core": "^7.5.5",

0 commit comments

Comments
 (0)