Astro + Contentful · Lesson 9 of 15 · 15 min
Build the course and lesson pages
List courses with their lessons in order, then generate one page per lesson with its author.
The data is in collections. Now it’s regular Astro work.
1. A layout
src/layouts/BaseLayout.astro
---
interface Props {
title: string;
}
const { title } = Astro.props;
---
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{title} · Learn Hub</title>
</head>
<body>
<header>
<a href="/">Learn Hub</a>
</header>
<main>
<slot />
</main>
</body>
</html>
<style is:global>
body {
font-family: system-ui, sans-serif;
line-height: 1.6;
max-width: 720px;
margin: 0 auto;
padding: 1.5rem;
color: #1a1a1a;
}
header a {
font-weight: 700;
text-decoration: none;
color: inherit;
}
img {
max-width: 100%;
height: auto;
border-radius: 8px;
}
pre {
background: #f4f4f4;
padding: 1rem;
border-radius: 8px;
overflow-x: auto;
}
</style>
It’s plain on purpose. Style it your way after the course.
2. The course list
Replace the homepage:
src/pages/index.astro
---
import { getCollection, getEntries } from 'astro:content';
import BaseLayout from '../layouts/BaseLayout.astro';
const allCourses = await getCollection('courses');
const courses = await Promise.all(
allCourses.map(async (course) => ({
course,
lessons: await getEntries(course.data.lessons),
})),
);
---
<BaseLayout title="Courses">
<h1>Courses</h1>
{courses.map(({ course, lessons }) => (
<section>
<h2>{course.data.title}</h2>
<p>{course.data.level} · {lessons.length} lessons</p>
<p>{course.data.summary}</p>
<ol>
{lessons.map((lesson) => (
<li>
<a href={`/lessons/${lesson.id}/`}>{lesson.data.title}</a> ({lesson.data.minutes} min)
</li>
))}
</ol>
</section>
))}
</BaseLayout>
getEntries(course.data.lessons) takes the list of references and returns the real lessons, in the order the editor dragged them into in Contentful. That’s the payoff of reference() in the schema.
3. One page per lesson
src/pages/lessons/[id].astro
---
import { getCollection, getEntry, render } from 'astro:content';
import BaseLayout from '../../layouts/BaseLayout.astro';
export async function getStaticPaths() {
const lessons = await getCollection('lessons');
return lessons.map((lesson) => ({
params: { id: lesson.id },
props: { lesson },
}));
}
const { lesson } = Astro.props;
const { Content } = await render(lesson);
const author = lesson.data.author ? await getEntry(lesson.data.author) : undefined;
---
<BaseLayout title={lesson.data.title}>
<h1>{lesson.data.title}</h1>
<p>{lesson.data.summary}</p>
{author && (
<p>
{author.data.photo && <img src={author.data.photo} alt="" width="40" height="40" />}
By {author.data.name}{author.data.role && `, ${author.data.role}`} · {lesson.data.minutes} min
</p>
)}
<Content />
</BaseLayout>
getStaticPaths() returns one path per lesson, the same pattern as blog posts.
getEntry(lesson.data.author) follows the reference to the author.
<Content /> is the Rich text body, already converted to HTML by the loader.
alt="" on the author photo is on purpose. The name is right next to it, so the photo is decoration.
4. Look at it
Visit localhost:4321. You see your course with two lessons. Click one. Title, author, and body all come from Contentful.
The draft lesson isn’t listed, and its URL is a 404. That’s withoutUnresolvableLinks and the Delivery API doing their jobs.