Skip to content

Commit 5b2d13b

Browse files
committed
prefer blobs
1 parent 79f68e9 commit 5b2d13b

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/components/preview.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, createEffect, createSignal, splitProps, JSX, onMount } from 'solid-js';
1+
import { Component, createEffect, createSignal, splitProps, JSX, onMount, onCleanup } from 'solid-js';
22
import { useZoom } from '../hooks/useZoom';
33

44
export const Preview: Component<Props> = (props) => {
@@ -21,7 +21,14 @@ export const Preview: Component<Props> = (props) => {
2121
if (isNotDom || isEmpty || !isIframeReady()) return;
2222

2323
latestCode = internal.code.replace('render(', 'window.dispose = render(');
24-
iframe.contentWindow!.postMessage({ event: CODE_UPDATE, code: latestCode }, '*');
24+
25+
const blob = new Blob([latestCode], {
26+
type: 'text/javascript',
27+
});
28+
const src = URL.createObjectURL(blob);
29+
onCleanup(() => URL.revokeObjectURL(src));
30+
31+
iframe.contentWindow!.postMessage({ event: CODE_UPDATE, code: src }, '*');
2532
});
2633

2734
createEffect(() => {
@@ -49,7 +56,7 @@ export const Preview: Component<Props> = (props) => {
4956
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
5057
5158
<!-- Ressource hints -->
52-
<link rel="dns-prefetch" href="//unpkg.com">
59+
<link rel="dns-prefetch" href="https://unpkg.com">
5360
<link href="https://unpkg.com" rel="preconnect" crossorigin>
5461
<link rel="preload" href="https://unpkg.com/[email protected]/modern-normalize.css" as="style">
5562
<link href="https://unpkg.com/[email protected]/modern-normalize.css" rel="stylesheet">
@@ -115,7 +122,7 @@ export const Preview: Component<Props> = (props) => {
115122
eruda.position({ x: window.innerWidth - 30, y: window.innerHeight - 30 });
116123
const style = Object.assign(document.createElement('link'), {
117124
rel: 'stylesheet',
118-
href: '/eruda.css'
125+
href: '${location.origin}/eruda.css'
119126
});
120127
eruda._shadowRoot.appendChild(style);
121128
if (${internal.devtools}) eruda.show();
@@ -147,9 +154,7 @@ export const Preview: Component<Props> = (props) => {
147154
148155
console.clear();
149156
150-
const encodedCode = encodeURIComponent(code);
151-
const dataUri = 'data:text/javascript;charset=utf-8,' + encodedCode;
152-
await import(dataUri);
157+
await import(code);
153158
154159
const load = document.getElementById('load');
155160
if (code && load) load.remove();
@@ -168,14 +173,19 @@ export const Preview: Component<Props> = (props) => {
168173
</body>
169174
</html>
170175
`;
176+
const blob = new Blob([html], {
177+
type: 'text/html',
178+
});
179+
const src = URL.createObjectURL(blob);
180+
onCleanup(() => URL.revokeObjectURL(src));
171181

172182
createEffect(() => {
173183
// Bail early on first mount or we are already reloading
174184
if (!internal.reloadSignal) return;
175185

176186
// Otherwise, reload everytime we clicked the reload button
177187
setIframeReady(false);
178-
iframe.srcdoc = html;
188+
iframe.src = src;
179189
});
180190

181191
const styleScale = () => {
@@ -187,7 +197,6 @@ export const Preview: Component<Props> = (props) => {
187197
};
188198

189199
onMount(() => {
190-
iframe.srcdoc = html;
191200
iframe.addEventListener('load', () => {
192201
setIframeReady(true);
193202

@@ -205,6 +214,7 @@ export const Preview: Component<Props> = (props) => {
205214
title="Solid REPL"
206215
class="overflow-auto p-0 w-full h-full dark:bg-other block"
207216
ref={iframe}
217+
src={src}
208218
// @ts-ignore
209219
sandbox="allow-popups-to-escape-sandbox allow-scripts allow-popups allow-forms allow-pointer-lock allow-top-navigation allow-modals allow-same-origin"
210220
></iframe>

0 commit comments

Comments
 (0)