Skip to content

Commit 808e7b1

Browse files
committed
Render all linked JS files with blobUrl
1 parent bbd8503 commit 808e7b1

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

client/modules/Preview/EmbedFrame.jsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import {
1818
} from '../../../server/utils/fileUtils';
1919
import { startTag, getAllScriptOffsets } from '../../utils/consoleUtils';
2020
import { registerFrame } from '../../utils/dispatcher';
21+
import { createBlobUrl } from './filesReducer';
22+
23+
let objectUrls = {};
24+
let objectPaths = {};
2125

2226
const Frame = styled.iframe`
2327
min-height: 100%;
@@ -124,9 +128,16 @@ function resolveScripts(sketchDoc, files) {
124128
if (resolvedFile.url) {
125129
script.setAttribute('src', resolvedFile.url);
126130
} else {
127-
script.setAttribute('data-tag', `${startTag}${resolvedFile.name}`);
128-
script.removeAttribute('src');
129-
script.innerHTML = resolvedFile.content; // eslint-disable-line
131+
// in the future, when using y.js, could remake the blob for only the file(s)
132+
// that changed
133+
const blobUrl = createBlobUrl(resolvedFile);
134+
script.setAttribute('src', blobUrl);
135+
const blobPath = blobUrl.split('/').pop();
136+
objectUrls[blobUrl] = resolvedFile.name;
137+
objectPaths[blobPath] = resolvedFile.name;
138+
// script.setAttribute('data-tag', `${startTag}${resolvedFile.name}`);
139+
// script.removeAttribute('src');
140+
// script.innerHTML = resolvedFile.content; // eslint-disable-line
130141
}
131142
}
132143
} else if (
@@ -194,12 +205,14 @@ function addLoopProtect(sketchDoc) {
194205

195206
function injectLocalFiles(files, htmlFile, basePath) {
196207
let scriptOffs = [];
208+
objectUrls = {};
209+
objectPaths = {};
197210
const resolvedFiles = resolveJSAndCSSLinks(files);
198211
const parser = new DOMParser();
199212
const sketchDoc = parser.parseFromString(htmlFile.content, 'text/html');
200213

201214
const base = sketchDoc.createElement('base');
202-
base.href = `${basePath}/`;
215+
base.href = `${basePath}`;
203216
sketchDoc.head.appendChild(base);
204217

205218
resolvePathsForElementsWithAttribute('src', sketchDoc, resolvedFiles);
@@ -218,6 +231,8 @@ function injectLocalFiles(files, htmlFile, basePath) {
218231
const consoleErrorsScript = sketchDoc.createElement('script');
219232
consoleErrorsScript.innerHTML = `
220233
window.offs = ${JSON.stringify(scriptOffs)};
234+
window.objectUrls = ${JSON.stringify(objectUrls)};
235+
window.objectPaths = ${JSON.stringify(objectPaths)};
221236
window.editorOrigin = '${getConfig('EDITOR_URL')}';
222237
`;
223238
addLoopProtect(sketchDoc);

client/utils/previewEntry.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,17 @@ function getScriptOff(line) {
7575
return [line - l, file];
7676
}
7777
// catch reference errors, via http://stackoverflow.com/a/12747364/2994108
78-
window.onerror = function onError(msg, url, lineNumber, columnNo, error) {
79-
// var string = msg.toLowerCase();
80-
// var substring = "script error";
81-
let data = {};
82-
let fileInfo;
83-
if (url.match(EXTERNAL_LINK_REGEX) !== null && error.stack) {
84-
const errorNum = error.stack.split('about:srcdoc:')[1].split(':')[0];
85-
fileInfo = getScriptOff(errorNum);
86-
data = `${msg} (${fileInfo[1]}: line ${fileInfo[0]})`;
87-
} else {
88-
fileInfo = getScriptOff(lineNumber);
89-
data = `${msg} (${fileInfo[1]}: line ${fileInfo[0]})`;
90-
}
78+
window.onerror = function onError(msg, source, lineNumber, columnNo, error) {
79+
const urls = Object.keys(window.objectUrls);
80+
let data = '';
81+
urls.forEach((url) => {
82+
if (error.stack.match(url)) {
83+
data = error.stack.replaceAll(url, window.objectUrls[url]);
84+
}
85+
});
9186
editor.postMessage(
9287
{
93-
source: fileInfo[1],
88+
source: 'sketch',
9489
messages: [
9590
{
9691
log: [
@@ -110,12 +105,16 @@ window.onerror = function onError(msg, url, lineNumber, columnNo, error) {
110105
// catch rejected promises
111106
window.onunhandledrejection = function onUnhandledRejection(event) {
112107
if (event.reason && event.reason.message && event.reason.stack) {
113-
const errorNum = event.reason.stack.split('about:srcdoc:')[1].split(':')[0];
114-
const fileInfo = getScriptOff(errorNum);
115-
const data = `${event.reason.message} (${fileInfo[1]}: line ${fileInfo[0]})`;
108+
const urls = Object.keys(window.objectUrls);
109+
let data = '';
110+
urls.forEach((url) => {
111+
if (event.reason.stack.match(url)) {
112+
data = event.reason.stack.replaceAll(url, window.objectUrls[url]);
113+
}
114+
});
116115
editor.postMessage(
117116
{
118-
source: fileInfo[1],
117+
source: 'sketch',
119118
messages: [
120119
{
121120
log: [
@@ -132,3 +131,18 @@ window.onunhandledrejection = function onUnhandledRejection(event) {
132131
);
133132
}
134133
};
134+
135+
// Monkeypatch p5._friendlyError
136+
// const friendlyError = window.p5._friendlyError;
137+
// window.p5._friendlyError = function (message, method, color) {
138+
// const urls = Object.keys(window.objectUrls);
139+
// const paths = Object.keys(window.objectPaths);
140+
// let newMessage = message;
141+
// urls.forEach((url) => {
142+
// newMessage = newMessage.replaceAll(url, window.objectUrls[url]);
143+
// });
144+
// paths.forEach((path) => {
145+
// newMessage = newMessage.replaceAll(path, window.objectPaths[path]);
146+
// });
147+
// friendlyError.apply(window.p5, [newMessage, method, color]);
148+
// };

0 commit comments

Comments
 (0)