Skip to content
Sign inJoin waitlist

Pages and sections — Denzel Astro

A page is a small file that picks sections and hands them content. src/pages/blog.astro is the whole Blog page:

---
import BlogGrid from "../components/sections/BlogGrid.astro";
import pages from "../content/pages/blog.json";
import Layout from "../layouts/Layout.astro";
import { getAuthors, getCategories, getPosts } from "../lib/content";
import { toPostCard } from "../lib/to-ui";
const { blog } = pages;
const [posts, categories, authors] = await Promise.all([getPosts(), getCategories(), getAuthors()]);
---
<Layout title={blog.meta.title} description={blog.meta.description}>
<BlogGrid title={blog.title} posts={posts.map((post) => toPostCard(post, categories, authors))} />
</Layout>
  • Layout (src/layouts/Layout.astro) gives the page its browser title, description, canonical link, share image, icons and fonts, the header (with the search) and the footer.
  • BlogGrid is every post as a card, read from the collections through src/lib/content.ts (getPosts(), newest first), each turned into what a card shows (its link, category, author, date and photo) by toPostCard() in src/lib/to-ui.ts.

6 kinds of page and 39 addresses in the demo (plus the 404 page and the style guide, which are not in the sitemap). Every address is in src/content/routes.ts.

Page Address Page file Copy (src/content/pages/) Items it shows
Home / src/pages/index.astro home.json the posts picked by slug (slider, Editor’s Pick, Trending), the eight newest posts, the sidebar
Blog /blog src/pages/blog.astro blog.json (blog) every post, newest first
Category (5) /category/<slug> src/pages/category/[slug].astro blog.json (category) one category and its posts, the sidebar
Author (6) /author/<slug> src/pages/author/[slug].astro blog.json (author) one author and their posts, the sidebar
Post (26) /blog/<slug> src/pages/blog/[slug].astro blog.json (post) one post, three related posts, three more by its author, the sidebar
404 any unknown address src/pages/404.astro not-found.json —

All 39 addresses of the demo: /, /blog, five categories under /category/ (business, technology, economy, fashion, uncategory), six authors under /author/ (anderson, annabella, ceridwen, dostoevsky, melvile, admin) and 26 posts under /blog/. The style guide is /style-guide.

Pages are built as files (blog.html, blog/cryptocurrency-experiences-volatility-as-regulatory-concerns-persist.html), which hosts serve at /blog and /blog/cryptocurrency-experiences-volatility-as-regulatory-concerns-persist (Going live). To change an address (/category to /topics, say), rename its folder in src/pages/ and its entry in routes.ts, and search the content for the old address: the menu’s links in site.json are written as text and aren’t checked (removing a page).

In src/components/sections/, one file per section. Each takes its copy from a page file (or site.json) and its items from a collection.

Section File Used on
HomeSlider HomeSlider.astro Home: the row of featured posts under the header
HomeContent HomeContent.astro Home: Editor’s Pick, Trending and Latest, with the sidebar
BlogGrid BlogGrid.astro Blog
CategoryHead CategoryHead.astro A category: breadcrumb, name, description and photo
AuthorHead AuthorHead.astro An author: photo, breadcrumb, name, bio and social icons
PostList PostList.astro A category and an author: their posts as rows, with the sidebar
PostContent PostContent.astro A post: the article, Related Posts and More Articles by, with the sidebar
NotFoundBody NotFoundBody.astro 404
Sidebar Sidebar.astro Inside HomeContent, PostList and PostContent

The header and the footer are in src/components/site/ and come with Layout.

Sidebar.astro draws three boxes, 30 px apart: About (the logo over a dotted map, a line of text and the social icons), Recent (four posts with a round photo, “View All”) and Explore Topics (categories in two columns, each with its round photo and its number of posts). It sits beside the main column from desktops up (30 % of the width) and below it on phones and tablets.

Its words come from sidebar in src/content/site.json. Its two lists come from getSidebarData() in src/lib/sidebar.ts, which each page with a sidebar calls once and passes in: it looks up the posts named in sidebar.recent.posts and the categories named in sidebar.topics.categories (both by slug, in that order, unknown slugs skipped) and counts each category’s posts (Editing the content). To leave the sidebar off a page, remove <Sidebar slot="sidebar" … /> from the section; the column then takes the full width.

Several blocks come from one file, so changing them there changes every page that shows them:

Block Field Shown on
The menu, the social icons and the search navigation, socials, search in site.json the header and the phone menu, every page
The sidebar sidebar in site.json Home, a category, an author, a post
The footer, with its two posts and the newsletter box footer in site.json every page
“View All” labels.viewAll in site.json the section titles of Home and a post
The breadcrumbs “Denzel / Blog” category.crumbs, post.crumbs, author.home in blog.json a category, a post, an author
“Related Posts”, “More Articles by” and how many they show post.related, post.more in blog.json a post

The social links of an author (social in authors.json) are the author’s own and show only on their page.

The menu holds Home, All Post (the Blog), the four categories and Author (Anderson’s page); the logo leads Home. The other pages are reached like this:

Page Linked from
A post Its card’s title and photo (Home, the Blog, a category, an author, Related Posts, More Articles by, Recent, the footer), the search
A category The orange label on the cards, the last crumb of a post’s breadcrumb, Explore Topics, the menu, the search
An author The name on the cards and under a post’s title, “View All” of More Articles by, the search
The Blog “View All” (Editor’s Pick, Trending, Related Posts, Recent), “View More” under Latest, the crumb “Blog”

Every page type with each section outlined and labelled: the component that renders it, the file to edit, and where its copy (src/content/pages/*.json, src/content/site.json) and items (src/content/collections/*.json) come from. Header and footer take their links and details from src/content/site.json. Photos are the placeholders of the download.

Two sections: the slider (six featured posts in a row under the header, sliding one card every 5 seconds), then the main column with “Editor’s Pick” (a large post and two small ones), “Trending” (two columns, each a large post over two small ones) and “Latest” (the eight newest posts as rows and “View More”), beside the sidebar. Their words and picks are in src/content/pages/home.json, the sidebar’s in site.json.

Home: sections and the files behind them

One section: every post as a card, three across on desktops (two on tablets, one on phones), newest first, all on one page. The page’s <h1> (“All Post”) is for screen readers only. The words are in blog of src/content/pages/blog.json.

Blog: sections and the files behind them

Two sections: the head (the breadcrumb “Denzel / Blog / ”, the name, the description and the category’s photo); then its posts as rows, newest first, beside the sidebar. A category with no posts shows category.empty instead. The category is in categories.json, the labels in category of blog.json.

Category: sections and the files behind them

Two sections: the head (the round photo, the breadcrumb “Denzel / Articles by: ”, the name, the bio and the social icons); then their posts as rows, newest first, beside the sidebar. An author with no posts shows author.empty. The author is in authors.json, the labels in author of blog.json.

Author: sections and the files behind them

One section beside the sidebar: the breadcrumb (“Denzel / Blog / ”), the title, the author with their photo and the date, the cover and the text; then “Related Posts” (the three newest other posts of its category) and “More Articles by ” (the three newest other posts of the author), each left out when it has no post. Everything but the labels is the post in posts.json; the labels are post in blog.json.

Post: sections and the files behind them

One section, a screen high: the illustration, “Something’s wrong here…”, a line of text and “Return to Homepage”, centred. They are in src/content/pages/not-found.json.

Page not found: sections and the files behind them

Move the lines in the page file, or delete one (and its import). To take the slider off Home, delete <HomeSlider … /> from src/pages/index.astro (it holds Home’s <h1> for screen readers: keep one on the page). Editor’s Pick, Trending and Latest are one section, HomeContent: to remove one of them, delete its block (and the divider above it) in src/components/sections/HomeContent.astro, then its prop there and in src/pages/index.astro.

The header is white and the same at every scroll position, so no section needs to mark itself for it, and every page starts below the header’s spacer in <main>.

  1. Add the URL to src/content/routes.ts:

    about: "/about",
  2. Add its copy to src/content/pages/about.json, with a meta (title, description):

    {
    "meta": {
    "title": "About",
    "description": "Who writes Denzel, what we cover and how to reach the editors with a story, a correction or a pitch."
    },
    "title": "About Denzel",
    "text": "Unveiling stories that intrigue and inspire."
    }
  3. Create src/pages/about.astro:

    ---
    import about from "../content/pages/about.json";
    import Section from "../design-system/components/layout/Section.astro";
    import { text } from "../design-system/tokens/text";
    import { cn } from "../design-system/utils/cn";
    import Layout from "../layouts/Layout.astro";
    ---
    <Layout title={about.meta.title} description={about.meta.description}>
    <Section padding="py-10 md:py-[50px] lg:py-[60px]" innerClass="gap-2.5">
    <h1 class={cn(text.title, "text-ink")}>{about.title}</h1>
    <p class={cn(text.paragraph, "text-body")}>{about.text}</p>
    </Section>
    </Layout>

    Every page has one <h1>. For a page with the sidebar, look at src/components/sections/PostList.astro: it puts the column and <Sidebar> in WithSidebar, with the lists from getSidebarData().

  4. Link to it: add { "label": "About", "href": "/about" } to navigation in src/content/site.json. The desktop menu sits in one row with the logo, the socials and the search, so keep the labels short and the menu to about seven links.

  5. Add routes.about to the paths list in src/pages/sitemap.xml.ts, next to routes.home and routes.blog.

Delete its file (or folder) under src/pages/, its entry in navigation (src/content/site.json), its line in src/pages/sitemap.xml.ts, and the route in routes.ts. npm run check then tells you about anything still using the route through routes. Links written as text in the content are not checked: the /blog of “View All” and “View More” (picks.viewAll, trending.viewAll, latest.more in home.json, sidebar.recent.viewAll in site.json) and the crumbs of blog.json, for example. Search src/content/ for the path.

src/pages/blog/[slug].astro builds one page per post:

export async function getStaticPaths() {
return (await getPosts()).map((post) => ({ params: { slug: post.slug }, props: { post } }));
}

Add a post to src/content/collections/posts.json, and its page, its card and its sitemap entry appear on the next build; any other address under /blog/ is a 404. The same pattern builds the categories (src/pages/category/[slug].astro) and the authors (src/pages/author/[slug].astro): every category and author gets a page, even with no posts.

src/pages/404.astro, with its copy in src/content/pages/not-found.json. It is built as 404.html, which hosts serve for any unknown address, with a 404 status; it is noindex, like the style guide.

  • Fixed and white. The header stays at the top of every page, 92 px tall on phones and tablets and 96 px on desktops, and does not change while you scroll.
  • Desktop: the logo, the seven links, the social icons and the “Search All” pill (it fills with Ink on hover). The link of the page you are on is grey and struck through; the others turn orange and underline on hover. Tablet: the logo, the social icons in the middle, a round search button and the menu button. Phone: the same without the social icons.
  • The phone menu. A press on the four squares opens the menu under the bar with Framer’s spring: the links in a column and the social icons; the button then reads “Close”. It closes when a link or the logo is pressed, or the button again. src/components/site/header.ts runs it.
  • The menu is navigation in src/content/site.json. A link is marked as current only on its own address (a post doesn’t mark “All Post”), for screen readers as well.

The “Search All” pill (desktops) and the round search button (phones and tablets) open a search box over a dark scrim: centred, about a fifth down the window, from tablets up; a bar across the top on phones. As the visitor types, it lists the matches, each with its title and its address; every word typed must appear. The first result is highlighted, ↑ ↓ move the highlight, Enter or a click opens it, Escape or the scrim closes the box, and “No results found” shows when nothing matches.

It searches what toSearchItems() in src/lib/to-ui.ts gives it: every post (by its title, excerpt, category and author name), every category with at least one post (by its name and description) and every author with at least one post (by their name and bio). The list is written into every page when the site builds, so the search needs no server and no service. The box is src/design-system/components/navigation/SearchDialog.astro, its script src/design-system/scripts/search.ts, its words search in site.json.

  • Sitemap: src/pages/sitemap.xml.ts builds sitemap.xml from routes.ts and the collections: Home and the Blog, then every category, author and post: 39 addresses in the demo. The 404 page and the style guide are left out.
  • Robots: src/pages/robots.txt.ts builds robots.txt, which allows everything and points at the sitemap. Both use site.url, so set your domain before publishing.
  • Per page: every indexable page gets a title, a description, a canonical link, an og:url and a share image from src/layouts/Layout.astro (with src/lib/meta.ts). Home and the Blog write theirs in their meta; Home’s title is shown as it is. A category takes its name and category.metaDescription; an author their name, author.metaDescription and the start of their bio; a post its title, and its title followed by its first paragraph (cut at 160 characters) as the description, is marked as an article with its date, and shares its cover. A title that would be longer than 60 characters with | Denzel appended is shown without the suffix. Every other page shares public/opengraph-image.png.
  • Not indexed: the style guide and the 404 page (noindex).