· 2 min read · updated
A CMS preview that compiles Svelte
The CMS preview compiles each post in the browser the way the build does: mdsvex/compile with the build's options (src/lib/markdown/options.ts), then Svelte's…
What we did
- The CMS preview compiles each post in the browser the way the build does:
mdsvex/compilewith the build's options (src/lib/markdown/options.ts), then Svelte's compiler, thenmount()(src/lib/cms/mdsvex.svelte.ts). - The compiled module imports Svelte's runtime and our components by name; each name becomes a
blob:module that hands out the CMS's own copy, and only the import statements at the top are rewritten. - Images and block files come from Sveltia's
getAsset(), links get the file's language, both through the same contexts the site's pages set. - Checked with Sveltia's test backend: all 72 published posts preview identical to the built pages, code colours included. A warm compile takes about 6 ms.
- Braces in posts now fail the build and the preview, like HTML: the preview runs what it compiles, in the page that holds the GitHub token. Update (2026-11-27): a plain
{expression}is allowed now; the preview shows it as code instead of running it (entry2026-11-27-braces-in-posts).
What I learned
- Svelte forbids importing
svelte/internal/*from.svelte.tsfiles (it's private); a plain.tsfile can, and the compiler and runtime from one package always match. - One of Svelte's runtime exports is called
await, soexport const { await }is a syntax error: the blob modules export through local variables. - A post with plugin data imports
setContextfrom'svelte', so that name needs a module too.
Problems & fixes
- Playwright can't drive Sveltia's folder picker, so the tests used Sveltia's in-browser test backend, switched on in
config.ymlonly during the test.
Links
Next
- Try the built CMS at
/adminonce the branch is pushed.