· 3 min read
Live demos, and a sandbox for the CMS preview
Code blocks that run. svelte run makes the block a module the page imports; markdown/Pre.svelte draws its component above its code, in a region named "Live…
What we did
- Code blocks that run.
```svelte runmakes the block a module the page imports;markdown/Pre.sveltedraws its component above its code, in a region named "Live demo". A```ts runblock's default export is called and its result shown;run=clientwaits for a "Run the demo" button.:::demo[Title]frames a demo in a window. - The demo and its code block share a frame outside the
<figure>: Svelte's accessibility check said a<figcaption>must stay first or last. Plain code blocks keep exactly their old markup. - In the CMS preview, demos run in a sandbox. Each demo's module is
SandboxDemo.svelte, which compiles the demo and posts the JavaScript to<iframe sandbox="allow-scripts">loadingdemo.html; the page inside mounts it with its own copy of Svelte and sends back its height and any error. Script blocks don't run there; a line says so. cms/blob-modules.tsholds the import rewriting the preview and the frame share. The runner page is a second input of the CMS build;scripts/serve.tssendsAccess-Control-Allow-Origin: *like GitHub Pages.- Tested: a hostile demo in the preview got
SecurityErrorreadinglocalStorage,parent.documentanddocument.cookie, and navigatingtop; the counter demo worked in its frame; the built runner worked from an opaque origin too. A draft post tells the story, in both languages.
What I learned
sandbox="allow-scripts"withoutallow-same-origingives the frame an opaque origin: no storage, no cookies, no access to the page around it, and no navigating the tab (Chrome logs "Unsafe attempt to initiate navigation").- An opaque origin sends
Origin: null, and module scripts are fetched with CORS, so the server has to allow it. GitHub Pages already sends*; Vite's dev server needed'null'inserver.cors. postMessagefrom a frame's code goes to whichever window you call it on, andevent.sourceis the window whose code sent it, not the frame that holds the element.
Problems & fixes
- The first sandbox ran nothing: Sveltia draws its preview in a frame of its own, so the CMS's code (in the top window) sent the demo, not the frame's
parent, and the runner ignored it; its answers went toparent, where nobody listened. It now accepts its parent or the top window and answers whichever asked. - An
{#if}on its own line added a whitespace node to every code block; the demo now lives outside the figure, so nothing changed for plain blocks.
Links
Next
- Page-data, then an RSS feed. (Braces were allowed the same day instead of a post about them: entry
2026-11-27-braces-in-posts.)