src/lib/rendered.ts (1279B)
1 /**
2 * Fail the build when a content entry comes back without a body.
3 *
4 * Astro's glob loader catches any exception a Markdown render throws, logs one line and
5 * stores the entry with `rendered` undefined (astro/dist/content/loaders/glob.js). `render()`
6 * then returns a Content component that emits nothing, while everything around it — title,
7 * byline, description, feed item, sitemap entry — still builds, and `astro build` exits 0.
8 * The post ships as a title over an empty column. A throw from page code does fail the
9 * build, so every page that renders an entry calls this first.
10 *
11 * The loader only re-renders a file whose contents changed, so a failure cached by an
12 * earlier build comes back with no log line at all. The message says how to get it back.
13 */
14 export function assertRendered(entry: { id: string; filePath?: string; rendered?: { html: string } }): void {
15 const file = entry.filePath ?? entry.id;
16 if (!entry.rendered) {
17 throw new Error(
18 `${file} did not render. The cause is the "Error rendering" line logged above; if there ` +
19 `is none, an earlier build cached the failure. Rerun with \`npm run build -- --force\`.`,
20 );
21 }
22 if (!entry.rendered.html.trim()) throw new Error(`${file} rendered to an empty body.`);
23 }