· 3 min read
A leaner CMS
After svelte-markdown replaced mdsvex, I went through the CMS and asked of each piece whether it still earned its place. It went from 977 to 694 lines of code.
What we did
- After svelte-markdown replaced mdsvex, I went through the CMS and asked of each piece whether it still earned its place. It went from 977 to 694 lines of code.
- The CMS moved from
cms/tosrc/lib/cms/, next to the code it shares with the site. Nothing needscms/in Vite's allowed folders,tsconfigor the test config any more. - Sveltia is imported from npm.
main.tssetsCMS_MANUAL_INIT, imports@sveltia/cms, registers our pieces and callsinit({ config })withconfig.ymlread by our own code. The Vite plugin went from 92 lines to 46: it only answers/adminwith a page that has one script. - The "Real page" tab is gone: the hidden
.preview.mdfiles, the endpoint that wrote them, and the?previewbranches in the[slug]pages. The preview draws with the page's own components now, so it had nothing left to show. - The editor components show a one-line summary (the block's name and title). The preview pane next to them draws the real block, so
blocks.tslost its second copy of each block's markup as HTML strings. - Covers come from a Vite glob instead of a dev-only route;
content.tsreads frontmatter with the samereadEntry()as the CMS; the preview's reading time is the site'sreadingTime(), in its own small file. - Kept: the post's header in the preview, drawn in React. Mounting
ArticleHeader.sveltethere would need a language prop, and its scoped CSS can't reach Sveltia's frame.
What I learned
- The CMS page isn't a SvelteKit route on purpose: every route sits inside the root layout and its global CSS, and it would be part of the build.
- A React effect must return a function or nothing. Whatever it returns, React calls as its cleanup before the effect runs again.
- Sveltia's editorial workflow (a branch and a pull request per entry, publish = merge) needs the GitHub backend: it's for when the CMS is hosted, not for the local workflow.
Problems & fixes
- Awaiting
import('@sveltia/cms')at the top ofmain.tshung with no error. The page'sDOMContentLoadedwaits for that module to finish, and Sveltia waits forDOMContentLoaded.import(…).then(setUp)instead. - Vite's dependency optimizer broke Sveltia's npm build, which loads fonts and wasm from a folder next to its code. It's in
optimizeDeps.exclude. - Sveltia's build uses
import.meta.hot, so Vite's client is on the CMS page anyway. I checked that changing content doesn't reload it, so the CMS's components no longer need to be compiled without hot reload. - Reading
config.ymlourselves neededmerge: truein theyamlpackage: the config uses<<:merge keys. - The live preview drew the first version and then stopped, with
l is not a functionin react-dom on every keystroke. The React wrapper's effect was an arrow that returned a value (() => update(props)). A block body fixed it, andmarkdown.test.tschecks it under a stand-in React that keeps React's rule.
Links
- Sveltia CMS: manual initialization
- Sveltia CMS: editorial workflow
- React: useEffect and its cleanup
- Vite: dependency optimization options
- MDN: DOMContentLoaded
Next
- Check the whole editing flow by hand (Sveltia's folder picker can't be automated).
- Images (keep enhanced-img, cache it in CI) and how drafts get published: open questions in
design/svelte-markdown-spike.md.