Skip to content

Commit cf8f0ed

Browse files
committed
Fix loading assets at sketch runtime
1 parent 5141186 commit cf8f0ed

File tree

6 files changed

+19
-12
lines changed

6 files changed

+19
-12
lines changed

client/modules/IDE/actions/ide.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,11 @@ export function startSketch() {
266266
dispatch(startVisualSketch());
267267
const state = getState();
268268
dispatchMessage({
269-
type: MessageTypes.FILES,
270-
payload: state.files
269+
type: MessageTypes.SKETCH,
270+
payload: {
271+
files: state.files,
272+
basePath: window.location.pathname
273+
}
271274
});
272275
dispatchMessage({
273276
type: MessageTypes.START

client/modules/Preview/EmbedFrame.jsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,14 @@ function addLoopProtect(sketchDoc) {
192192
});
193193
}
194194

195-
function injectLocalFiles(files, htmlFile) {
195+
function injectLocalFiles(files, htmlFile, basePath) {
196196
let scriptOffs = [];
197197
const resolvedFiles = resolveJSAndCSSLinks(files);
198198
const parser = new DOMParser();
199199
const sketchDoc = parser.parseFromString(htmlFile.content, 'text/html');
200200

201201
const base = sketchDoc.createElement('base');
202-
base.href = `${window.location.href}/`;
202+
base.href = `${basePath}/`;
203203
sketchDoc.head.appendChild(base);
204204

205205
resolvePathsForElementsWithAttribute('src', sketchDoc, resolvedFiles);
@@ -230,7 +230,7 @@ function getHtmlFile(files) {
230230
return files.filter((file) => file.name.match(/.*\.html$/i))[0];
231231
}
232232

233-
function EmbedFrame({ files, isPlaying }) {
233+
function EmbedFrame({ files, isPlaying, basePath }) {
234234
const iframe = useRef();
235235
const htmlFile = useMemo(() => getHtmlFile(files), [files]);
236236

@@ -247,7 +247,7 @@ function EmbedFrame({ files, isPlaying }) {
247247
function renderSketch() {
248248
const doc = iframe.current;
249249
if (isPlaying) {
250-
const htmlDoc = injectLocalFiles(files, htmlFile);
250+
const htmlDoc = injectLocalFiles(files, htmlFile, basePath);
251251
srcDoc.set(doc, htmlDoc);
252252
} else {
253253
doc.srcdoc = '';
@@ -275,7 +275,8 @@ EmbedFrame.propTypes = {
275275
name: PropTypes.string.isRequired,
276276
content: PropTypes.string.isRequired
277277
}).isRequired,
278-
isPlaying: PropTypes.bool.isRequired
278+
isPlaying: PropTypes.bool.isRequired,
279+
basePath: PropTypes.string.isRequired
279280
};
280281

281282
export default EmbedFrame;

client/modules/Preview/previewIndex.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ const GlobalStyle = createGlobalStyle`
2121
const App = () => {
2222
const [state, dispatch] = useReducer(filesReducer, [], initialState);
2323
const [isPlaying, setIsPlaying] = useState(false);
24+
const [basePath, setBasePath] = useState('');
2425
registerFrame(window.parent, getConfig('EDITOR_URL'));
2526

2627
function handleMessageEvent(message) {
2728
const { type, payload } = message;
2829
switch (type) {
29-
case MessageTypes.FILES:
30-
dispatch(setFiles(payload));
30+
case MessageTypes.SKETCH:
31+
dispatch(setFiles(payload.files));
32+
setBasePath(payload.basePath);
3133
break;
3234
case MessageTypes.START:
3335
setIsPlaying(true);
@@ -55,7 +57,7 @@ const App = () => {
5557
return (
5658
<React.Fragment>
5759
<GlobalStyle />
58-
<EmbedFrame files={state} isPlaying={isPlaying} />
60+
<EmbedFrame files={state} isPlaying={isPlaying} basePath={basePath} />
5961
</React.Fragment>
6062
);
6163
};

client/utils/dispatcher.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const MessageTypes = {
99
START: 'START',
1010
STOP: 'STOP',
1111
FILES: 'FILES',
12+
SKETCH: 'SKETCH',
1213
REGISTER: 'REGISTER',
1314
EXECUTE: 'EXECUTE'
1415
};

server/previewServer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import webpackDevMiddleware from 'webpack-dev-middleware';
55
import webpackHotMiddleware from 'webpack-hot-middleware';
66
import config from '../webpack/config.dev';
77
import embedRoutes from './routes/embed.routes';
8+
import assetRoutes from './routes/asset.routes';
89
import renderPreviewIndex from './views/previewIndex';
910

1011
const app = new Express();
@@ -33,6 +34,7 @@ app.get('/', (req, res) => {
3334
});
3435

3536
app.use('/', embedRoutes);
37+
app.use('/', assetRoutes);
3638

3739
app.listen(process.env.PREVIEW_PORT, (error) => {
3840
if (!error) {

server/server.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ app.use('/editor', requestsOfTypeJSON(), collections);
141141
// isomorphic rendering
142142
app.use('/', serverRoutes);
143143

144-
app.use(assetRoutes);
145-
146144
app.use('/', redirectEmbedRoutes);
147145
app.use('/', passportRoutes);
148146

0 commit comments

Comments
 (0)