Skip to content

Commit 10ded5b

Browse files
claudebrainkim
authored andcommitted
Revert to document.write approach - remove broken iframe route
The playground-preview server route was fundamentally broken: - Created nested website with navbar in iframe - Module resolution errors - Rendered as plaintext in some cases Reverting to the original working document.write() approach. Removed: - website/src/views/playground-preview.ts - /playground-preview route from routes.ts - iframe src and postMessage logic from code-preview.ts Restored: - Original generateJavaScriptIFrameHTML with document.write() - Working iframe execution flow Keeping the good changes: - Shared color-scheme utilities (fa4e901) - Fixed animated-letters example (bcccb44) - Fixed MathML example (ec38a97) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent ffc0f68 commit 10ded5b

File tree

3 files changed

+15
-184
lines changed

3 files changed

+15
-184
lines changed

website/src/components/code-preview.ts

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ export function* CodePreview(
258258
const isPython = currentLanguage === "python";
259259

260260
let staticURLs: Record<string, any> | undefined;
261-
let iframeReady = false;
262261
let execute: () => unknown;
263262
let executeDebounced: () => unknown;
264263
if (typeof window !== "undefined") {
@@ -270,38 +269,27 @@ export function* CodePreview(
270269
return;
271270
}
272271

272+
// We have to refresh to change the iframe variable in scope, as the
273+
// previous iframe is destroyed. We would have to await refresh if this
274+
// component was refactored to be async.
275+
iframeID++;
276+
this.refresh();
277+
const document1 = iframe.contentDocument;
278+
if (document1 == null) {
279+
return;
280+
}
281+
273282
let code = value;
274283

275284
if (isPython) {
276-
// Python code - keep using document.write for PyScript
277-
iframeID++;
278-
this.refresh();
279-
const document1 = iframe.contentDocument;
280-
if (document1 == null) {
281-
return;
282-
}
285+
// Python code - no transformation needed
283286
document1.write(generatePythonIFrameHTML(id, code, staticURLs!));
284-
document1.close();
285287
} else {
286-
// JavaScript code - use postMessage to server-rendered iframe
288+
// JavaScript code - transform with Babel
287289
try {
288290
const parsed = transform(value);
289291
code = parsed.code;
290-
291-
if (!iframeReady) {
292-
// Reload iframe to reset state
293-
pendingCode = code;
294-
iframeID++;
295-
iframeReady = false;
296-
this.refresh();
297-
// Code will be sent when iframe sends "ready" message
298-
} else {
299-
// Send code to already-loaded iframe
300-
iframe.contentWindow?.postMessage(
301-
JSON.stringify({type: "execute-code", id, code}),
302-
window.location.origin
303-
);
304-
}
292+
document1.write(generateJavaScriptIFrameHTML(id, code, staticURLs!));
305293
} catch (err: any) {
306294
console.error(err);
307295
loading = false;
@@ -310,32 +298,17 @@ export function* CodePreview(
310298
return;
311299
}
312300
}
301+
302+
document1.close();
313303
};
314304

315305
executeDebounced = debounce(execute, 2000);
316306
}
317307

318308
let height = 100;
319-
let pendingCode: string | null = null;
320309
if (typeof window !== "undefined") {
321310
const onmessage = (ev: any) => {
322311
let data: any = JSON.parse(ev.data);
323-
324-
// Handle messages without id (like "ready")
325-
if (data.type === "ready") {
326-
iframeReady = true;
327-
// Send pending code if we have it
328-
if (pendingCode && iframe.contentWindow) {
329-
iframe.contentWindow.postMessage(
330-
JSON.stringify({type: "execute-code", id, code: pendingCode}),
331-
window.location.origin
332-
);
333-
pendingCode = null;
334-
}
335-
return;
336-
}
337-
338-
// For messages with id, check it matches
339312
if (data.id !== id) {
340313
return;
341314
}
@@ -440,7 +413,6 @@ export function* CodePreview(
440413
<iframe
441414
key=${iframeID}
442415
ref=${(el: HTMLIFrameElement) => (iframe = el)}
443-
src=${isPython ? undefined : "/playground-preview"}
444416
class="
445417
playground-iframe
446418
${css`

website/src/routes.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import BlogHomeView from "./views/blog-home.js";
44
import GuideView from "./views/guide.js";
55
import BlogView from "./views/blog.js";
66
import PlaygroundView from "./views/playground.js";
7-
import PlaygroundPreviewView from "./views/playground-preview.js";
87

98
// TODO: I am not sure what the value of the route() function is over using the
109
// route config directly.
@@ -29,8 +28,4 @@ export const router = new Router([
2928
name: "playground",
3029
view: PlaygroundView,
3130
}),
32-
route("/playground-preview", {
33-
name: "playground-preview",
34-
view: PlaygroundPreviewView,
35-
}),
3631
]);

website/src/views/playground-preview.ts

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)