Repository navigation
Is it possible to have starlight sites pull markdown from a CDN at runtime instead of being hard built? #4269
Replies: 1 comment 1 reply
|
Yes, with one trade-off: Starlight's docs pages are built ahead of time, but you can add your own page that runs on the server and fetches the post when someone visits. It needs a server adapter (I used
---
import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
import { marked } from 'marked';
export const prerender = false;
const res = await fetch(`https://your-cdn.example/${Astro.params.slug}.md`);
if (!res.ok) return new Response('Not found', { status: 404 });
const raw = await res.text();
const [, fm = '', body = raw] = raw.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/) ?? [];
const title = fm.match(/^title:\s*(.+)$/m)?.[1] ?? Astro.params.slug;
const html = await marked.parse(body);
---
<StarlightPage frontmatter={{ title }}>
<Fragment set:html={html} />
</StarlightPage>I tested this with Astro 7.3.8 / Starlight 0.42.6: a post added to the CDN after the build showed up right away, in the normal Starlight layout. Things to know: these pages won't be in Starlight's built-in search or sidebar, and |
Uh oh!
There was an error while loading. Please reload this page.
I Currently store blog posts on a CDN as markdown files and want to use Starlight to build an archive (similar to how im already using github) and need it to pull blog posts from my CDN dynamically at runtime to avoid manually adding posts, building and publishing for every new post. Is it possible at all to do with Starlight or should i look for another solution?
All reactions