Skip to content

astracms/nextjs-example

Repository files navigation

AstraCMS Next.js Example

A complete example of using AstraCMS with Next.js App Router.

Features

  • ✅ Server Components with @astracms/next
  • ✅ Static Site Generation (SSG) with generateStaticParams
  • ✅ Blog listing with filtering options
  • ✅ Individual post pages with cover images
  • ✅ Tailwind CSS with Typography plugin
  • ✅ Dark mode support

Quick Start

1. Clone and Install

git clone https://github.com/astracms/astracms-nextjs-example.git
cd astracms-nextjs-example
pnpm install

2. Configure Environment

Copy the example environment file and add your API key:

cp .env.example .env.local

Edit .env.local:

ASTRACMS_API_KEY=astra_pk_your_api_key_here

Get your API key from AstraCMS Dashboard → Settings → API Keys.

3. Run Development Server

pnpm dev

Open http://localhost:3000 to see the result.

Project Structure

app/
├── page.tsx              # Home page
├── layout.tsx            # Root layout with fonts
├── globals.css           # Global styles with Tailwind
└── blog/
    ├── page.tsx          # Blog listing page
    └── [slug]/
        └── page.tsx      # Individual blog post page
lib/
└── astracms.ts           # AstraCMS client configuration

Usage Examples

Fetching Posts

import { astracms } from '@/lib/astracms'

// Get all posts
const posts = await astracms.getPosts()

// With filtering options
const posts = await astracms.getPosts({
  categories: ['blog'],
  excludeCategories: ['internal'],
  excludeTags: ['draft'],
  format: 'html',
  limit: 10,
  page: 1,
})

Fetching a Single Post

import { astracms } from '@/lib/astracms'

const post = await astracms.getPost('my-post-slug')

Static Generation

export async function generateStaticParams() {
  const posts = await astracms.getPosts()
  return posts.map((post) => ({ slug: post.slug }))
}

Configuration

Next.js Config

For remote images from AstraCMS CDN, add to next.config.ts:

const nextConfig: NextConfig = {
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'cdn.astracms.dev',
      },
    ],
  },
}

AstraCMS Client

Configure the client in lib/astracms.ts:

import { createAstraCMSClient } from '@astracms/next'

export const astracms = createAstraCMSClient({
  apiKey: process.env.ASTRACMS_API_KEY!,
})

Learn More

Deploy

Deploy with Vercel

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors