Skip to content

Commit af33518

Browse files
committed
feat: added esm module import and iframe sandboxing
- feat: added `type='module'` on preview script - feat: added `sandbox='allow-scripts'` on iframe to prevent user scripts from accessing the parent window - refactor: use Blob and [URL.createObjectURL](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL_static) for managing performant previews
1 parent 6123370 commit af33518

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

source/components/AppPreview.vue

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ const store = useAppStore()
77
const iframe = ref()
88
99
const content = computed(() => {
10-
return `<html><head><style type="text/css">${store.code.css}</style></head><body>${store.code.html}<script type="text/javascript">(function() { try { ${store.code.js} } catch(err) { console.error(err); } })();</` + `script></body></html>`
10+
return `<html><head><style type="text/css">${store.code.css}</style></head><body>${store.code.html}<script type="module">${store.code.js}</` + `script></body></html>`
1111
})
1212
13-
let tid;
13+
let tid, objectURL;
1414
function refreshPreview(x) {
1515
if (!iframe.value) return
1616
if (tid) clearTimeout(tid);
1717
tid = setTimeout(() => {
18-
const doc = iframe.value.contentWindow.document;
19-
doc.open();
20-
doc.writeln(content.value);
21-
doc.close();
22-
}, 200);
18+
if (objectURL) URL.revokeObjectURL(objectURL)
19+
const blob = new Blob([content.value], { type: 'text/html' })
20+
objectURL = URL.createObjectURL(blob)
21+
iframe.value.src = objectURL
22+
}, 300);
2323
}
2424
2525
watchEffect(() => {
@@ -28,5 +28,5 @@ watchEffect(() => {
2828
</script>
2929

3030
<template>
31-
<iframe ref="iframe" src="about:blank" frameborder="0" title="preview" class="h-full w-full"></iframe>
31+
<iframe ref="iframe" src="about:blank" frameborder="0" title="preview" sandbox="allow-scripts" class="h-full w-full"></iframe>
3232
</template>

0 commit comments

Comments
 (0)