Skip to content
Sign inJoin waitlist

Using a headless CMS — Denzel Next.js

Connecting a headless CMS works the same for every Next.js template sold on Framebrick, so this page lists only what is specific to Denzel.

The approach, rich text, images, keeping pages fresh, and step-by-step guides for Sanity, Payload, Strapi and Directus are in Use a headless CMS with a Next.js template.

Pages read them through src/lib/content.ts: getPosts / getPost / getPostsBySlug / getPostsByCategory / getPostsByAuthor / getRelatedPosts / getMoreByAuthor, getCategories / getCategory and getAuthors / getAuthor. Mirror src/content/types.ts in the CMS:

Collection In the demo Fields
Post (posts.ts) 26 slug, title, excerpt, category (a category’s slug), author (an author’s slug), date, image, body (rich text)
Category (categories.ts) 5 slug, title, description, image (or null)
Author (authors.ts) 6 slug, name, bio, avatar (or null), social (facebook, twitter, instagram: an address or null)

An image is an object, { src, alt }: the path or address and its description. Dates are ISO dates ("2023-09-14").

  • References. A post’s category and author are slugs. Model them as references to the categories and the authors, mapped to their slugs. A slug that matches nothing still builds, but the card shows the slug and links to a missing page. The demo’s last category (“Uncategory”) and last author (“Admin”) are defaults without posts; their pages say so.
  • Order. getPosts() sorts the posts by date, newest first, so every list (Blog, Latest, a category, an author, Related Posts, More Articles by) is newest first whatever order the CMS returns; posts of the same date keep the CMS’s order. Categories and authors keep the order they arrive in. Latest shows the first eight (latest.count in src/content/pages/home.ts), Related Posts and More Articles by three each (post.related.count, post.more.count in blog.ts).
  • Home’s picks are slugs in the page copy. The slider (slider.slugs), Editor’s Pick (picks) and Trending (trending.columns) in home.ts, the sidebar’s Recent (sidebar.recent.posts) and Explore Topics (sidebar.topics.categories) and the footer’s two posts (footer.posts.slugs) in src/content/site.ts name posts and categories by slug. Keep those slugs in the CMS, or change them in the files; to let editors choose them, add a “featured” field or a settings document to the CMS and fill these lists from it. A slug in Editor’s Pick or Trending that no longer exists stops the build; elsewhere it is skipped.
  • The sidebar and the search read everything. getSidebarData() (src/lib/sidebar.ts) counts each category’s posts for Explore Topics, and the root layout (src/app/layout.tsx) reads every post, category and author for the header’s search and the footer, so every page depends on those queries.
  • Every item has a page. /blog/<slug>, /category/<slug> and /author/<slug> are built from the collections, and the sitemap reads the same functions. The menu’s category and author links are written in navigation (src/content/site.ts), not read from the collection.

A post’s body is blocks, not HTML:

{ type: "paragraph", text: "…" } // **bold** between double asterisks
{ type: "heading", level: 3, text: "…" } // levels 2–3 share the larger style, 4–6 the smaller one
{ type: "list", ordered: false, items: ["…", "…"] } // an item is a string …
{ type: "list", ordered: true, items: [{ text: "…", ordered: false, items: ["…", "…"] }] } // … or a string with a nested list

The demo’s posts use paragraphs, headings of levels 3 and 4, bulleted and numbered lists, bullets nested under numbered items and bold. There are no italics, links or images inside the text, and lists nest one level deep. Convert your CMS’s rich text to these blocks in your mapping function, bold as **…** (how).

Every photo sits in a frame and is cropped to fill it, so a photo of other proportions works; upload images at least as large as the layout uses:

Image Size
A post’s image (734 : 406 on its page, up to about 820 px wide; 477 : 430, 357 : 267 and 25 : 16 on the cards; square and round on the small ones) 1600 × 1067 or larger
A category’s image (336 : 234 beside the title; 52 px round in Explore Topics) 800 × 533
An author’s avatar (100 px round on the author page; 42 and 32 px beside the name) 120 × 120

Photos go through next/image, which needs the CMS’s host in images.remotePatterns of next.config.ts (Images). Every photo fills a frame of its own shape, so the layout needs no width and height from the CMS.