geekskai Logogeekskai
ToolsBlogPricing
Sign in
Sign in
ToolsBlogSign in
Wednesday, October 30, 2024|5.135Mins Read

Choose a CMS for Next.js: App Router and Contentful Example

Authors
  • avatar
    Name
    Geeks Kai
    Twitter
    @KaiGeeks
Discuss on Twitter • View on GitHub

Tags

CMS-systemsNext.jsweb-developmentcontent-managementSEO

Previous Article

GeeksKai Blog - Developer's Nextjs Blog Template

Next Article

3d659.com blog: A Resourceful Hub for Information
← Back to the blog
geekskai Logo
geekskai

Public tools for developers and creators, plus optional Geekskai Audio Toolkit plans. Built with care.

Popular Tools

SoundCloud DownloaderSoundCloud to MP3SoundCloud to WAVSoundCloud PlaylistSoundCloud Artwork
View All Tools

Resources

BlogAbout MePricingTagsProjectsPrivacy PolicyTerms of ServiceRefund Policy

Connect With Us

mailMail
githubGitHub
twitterTwitter
linkedinLinkedin
© 2026 geekskai • All rights reserved
CMS delivery models for Next.js

A CMS supplies an editing workflow and content API; Next.js renders the website. Choose the editing and operating model before comparing brand names. Server rendering can expose content to crawlers, but it does not guarantee indexing or a ranking increase.

This example targets Next.js 16 App Router, with Cache Components disabled, and Contentful's Content Delivery API. It reads published content on the server with an explicit no-store policy. It is not a Pages Router example, a preview system, or a benchmark of CMS vendors.

Choose the delivery model

ModelSuitable starting pointCosts and responsibilities to check
Repository Markdown or MDXDevelopers edit a small documentation site or blogGit review, build time, media handling, and a writing interface if non-developers need one
Hosted headless CMSEditors need a separate interface and managed content APISeats, environments, API limits, localization, asset storage, export, and preview requirements
Self-hosted CMSThe team needs control over its content infrastructureDatabase, backups, upgrades, authentication, security updates, hosting, and the product's actual license

These are decision criteria, not a popularity ranking. A hosted service and its open-source starter are different products. Check each vendor's current plan and license against your requirements; no price comparison or universal winner is claimed here.

Prepare one actual Contentful model

In a Contentful space, choose an environment and enable the en-US locale used by this example. Create a content type whose API ID is post, with required fields title (short text), slug (short text), and body (long text, plain text). Make slugs unique through editorial validation. Rich Text produces a structured document and needs a renderer; it is not interchangeable with the plain-text body below.

Create an entry with title "Hello from the CMS", slug "hello", and body "This content is published." Publish the content type and entry. Create a Content Delivery token with access to that environment. In the Next.js project's server environment, configure CONTENTFUL_SPACE_ID, CONTENTFUL_ENVIRONMENT_ID, and CONTENTFUL_DELIVERY_TOKEN. Use the chosen environment's ID, not its display label. Do not prefix these variables with NEXT_PUBLIC or place a management token in the browser. The endpoint below is for Contentful's standard delivery region; EU data residency uses the documented regional endpoint.

Read published content on the server

Create lib/contentful.mjs. Keep the server-only guard: it prevents accidental use from a Client Component. A missing entry returns null; configuration, HTTP, and malformed-content errors remain visible failures rather than empty posts.

import "server-only"

function validatePost(collection) {
  if (!Array.isArray(collection.items) || !Number.isInteger(collection.total)) {
    throw new Error("Invalid Contentful collection")
  }
  if (collection.total === 0 && collection.items.length === 0) {
    return null
  }
  if (collection.total !== 1 || collection.items.length !== 1) {
    throw new Error("Expected exactly one published post per slug")
  }
  const fields = collection.items[0]?.fields
  if (!fields || [fields.title, fields.slug, fields.body].some(value => typeof value !== "string")) {
    throw new Error("Post requires title, slug, and plain-text body fields")
  }
  return { title: fields.title, slug: fields.slug, body: fields.body }
}

export async function loadPublishedPost({ slug }) {
  if (typeof slug !== "string" || slug.trim() === "") {
    throw new TypeError("slug must be a non-empty string")
  }
  const spaceId = process.env.CONTENTFUL_SPACE_ID
  const environmentId = process.env.CONTENTFUL_ENVIRONMENT_ID
  const deliveryToken = process.env.CONTENTFUL_DELIVERY_TOKEN
  if (!spaceId || !environmentId || !deliveryToken) {
    throw new Error("Configure the server-side Contentful environment variables")
  }
  const endpoint = new URL("https://cdn.contentful.com/spaces/" + encodeURIComponent(spaceId) + "/environments/" + encodeURIComponent(environmentId) + "/entries")
  endpoint.search = new URLSearchParams({ content_type: "post", "fields.slug": slug, locale: "en-US", limit: "2" }).toString()
  const response = await fetch(endpoint, {
    headers: { Authorization: "Bearer " + deliveryToken },
    cache: "no-store",
  })
  if (!response.ok) {
    throw new Error("Contentful delivery request failed: HTTP " + response.status)
  }
  const collection = await response.json()
  return validatePost(collection)
}

Create app/blog/[slug]/page.jsx. This example assumes the usual @/ alias maps to the project source root. The App Router awaits params and calls the server module directly; it does not repeat the request in a client-side Effect. React renders these strings as text. Do not add dangerouslySetInnerHTML to render untrusted CMS markup.

import { notFound } from "next/navigation"
import { loadPublishedPost } from "@/lib/contentful.mjs"

export default async function PostPage({ params }) {
  const { slug } = await params
  const post = await loadPublishedPost({ slug })
  if (!post) {
    notFound()
  }
  return (
    <article>
      <h1>{post.title}</h1>
      <p>{post.body}</p>
    </article>
  )
}

Complete the edit, publish, and read loop

  1. Open /blog/hello in the Next.js application and confirm the published title and body.
  2. Edit the same entry's body in Contentful and publish the new version. An unpublished draft is not delivered by this API.
  3. Make a new server request to /blog/hello and confirm the published change after the delivery API has propagated it. This code does not maintain a Next.js persistent fetch cache; an already-open browser view will not update by itself.
  4. Try a missing slug, a duplicate slug, missing environment configuration, an invalid token, and an entry with the wrong field type. Expect a missing page only for an empty valid result; other failures should reach the application's error handling.

A Contentful account, configured space, and published entry are required for this live loop. Offline response fixtures can verify the request and validation logic, but they do not establish account access, publication propagation, or production integration.

Pages Router and caching boundaries

getStaticProps remains a Pages Router API for files under pages. It does not belong in an App Router Server Component. Do not copy the earlier Pages Router example into app/blog/[slug]/page.jsx.

The example deliberately specifies no-store rather than relying on a version-dependent default. In Next.js 16 projects that enable Cache Components, use that mode's documented caching model instead of assuming this configuration applies unchanged. For a larger site, design a cache lifetime and authenticated publication webhook or another revalidation strategy. Draft previews need separate access control and preview-token handling. Metadata, asset optimization, pagination, locale routing, and production error views are separate integration work.

Official references

  • Next.js App Router data fetching
  • Next.js fetch caching options
  • Pages Router getStaticProps
  • Contentful Content Delivery API, authentication, and regional endpoints