Fetch published articles for a homepage or paginated blog, with optional search, tag, and date filters.
Use the setup guide’s API helper and styles. This example fetches three published articles in a Server Component. Results are ordered by article date, newest first, with the slug breaking ties. If the API returns no articles, the page shows an empty state.
import Link from "next/link";
import { getArticles } from "../lib/content";
import styles from "./blog.module.css";
export default async function Welcome() {
const { items } = await getArticles({ pageSize: 3 });
return (
<>
<div className={styles.eyebrow}>DEMO JOURNAL</div>
<h1>
Latest
<br />
articles
</h1>
<p className={styles.intro}>
A sample website that renders published articles from the Postparticle
API.
</p>
<h2>Recent articles</h2>
<div className={styles.cards}>
{items.map((a) => (
<article key={a.slug}>
{a.coverUrl && <img src={a.coverUrl} alt={a.title} />}
<time dateTime={a.articleDate}>{a.articleDate}</time>
<h3>
<Link href={`/blog/${a.slug}`}>{a.title}</Link>
</h3>
<p>{a.excerpt}</p>
<Link href={`/blog/${a.slug}`}>Read article</Link>
</article>
))}
</div>
{!items.length && <p>No articles have been published yet.</p>}
<Link href="/blog">All articles</Link>
</>
);
}
The page reads and validates query parameters on the server before calling the API. Search and tag filters can be combined. Changing filters resets pagination, and previous/next links preserve the current filters and date order. Unset filters are omitted from the request. Invalid values show correction guidance and do not trigger an API request.
Add the validation helper and filter component before the listing page. The filter component resets when URL values change. Its Clear link also resets unsent edits when the page is already unfiltered.
export type BlogSearchParams = Record<string, string | string[] | undefined>;
export function readBlogFilters(params: BlogSearchParams) {
const errors: string[] = [];
function single(key: string) {
const value = params[key];
if (Array.isArray(value)) {
errors.push(`Use only one ${key} parameter.`);
return value[0] || "";
}
return value || "";
}
const q = single("q");
const tag = single("tag");
const order = single("order");
const page = single("page");
if (q.length > 200) errors.push("Search must be 200 characters or fewer.");
if (tag.length > 60) errors.push("Tag must be 60 characters or fewer.");
if (order && order !== "asc" && order !== "desc")
errors.push("Choose ascending or descending date order.");
const pageNumber = page ? Number(page) : 1;
if (
!Number.isSafeInteger(pageNumber) ||
pageNumber < 1 ||
pageNumber > 100000
)
errors.push(
"Page must be a whole number from 1 to 100000. Apply filters to return to page 1.",
);
return {
q,
tag,
dateOrder: order === "asc" ? ("asc" as const) : ("desc" as const),
pageNumber,
errors,
};
}
"use client";
import Link from "next/link";
import { useRef } from "react";
import styles from "../app/blog.module.css";
export default function BlogFilters({
q,
tag,
dateOrder,
}: {
q: string;
tag: string;
dateOrder: "asc" | "desc";
}) {
const form = useRef<HTMLFormElement>(null);
return (
<form ref={form} action="/blog" className={styles.form}>
<label>
Search articles
<input
name="q"
defaultValue={q}
maxLength={200}
placeholder="Enter a search term"
/>
</label>
<label>
Find a tag
<input
name="tag"
defaultValue={tag}
maxLength={60}
placeholder="ideas"
/>
</label>
<label>
Date order
<select name="order" defaultValue={dateOrder}>
<option value="desc">Newest first</option>
<option value="asc">Oldest first</option>
</select>
</label>
<button>Filter</button>
<Link href="/blog" onClick={() => form.current?.reset()}>
Clear
</Link>
</form>
);
}
import Link from "next/link";
import BlogFilters from "../../components/blog-filters";
import { getArticles } from "../../lib/content";
import { type BlogSearchParams, readBlogFilters } from "../../lib/filters";
import styles from "../blog.module.css";
export default async function Blog({
searchParams,
}: {
searchParams: Promise<BlogSearchParams>;
}) {
const { q, tag, dateOrder, pageNumber, errors } = readBlogFilters(
await searchParams,
);
const data = errors.length
? null
: await getArticles({ q, tag, order: dateOrder, page: pageNumber });
function pageHref(page: number) {
return `/blog?${new URLSearchParams({ page: String(page), order: dateOrder, ...(q ? { q } : {}), ...(tag ? { tag } : {}) })}`;
}
return (
<>
<h1>Articles</h1>
<BlogFilters
key={JSON.stringify([q, tag, dateOrder, pageNumber, errors])}
q={q}
tag={tag}
dateOrder={dateOrder}
/>
{errors.length > 0 && (
<div className={styles.error} role="alert">
<strong>Check your filters</strong>
<ul>
{errors.map((error) => (
<li key={error}>{error}</li>
))}
</ul>
<p>
Correct the fields and apply filters, or use Clear to start again.
</p>
</div>
)}
<div className={styles.cards}>
{data?.items.map((a) => (
<article key={a.slug}>
<time dateTime={a.articleDate}>{a.articleDate}</time>
<h2>
<Link href={`/blog/${a.slug}`}>{a.title}</Link>
</h2>
<p>{a.excerpt}</p>
<div>
{a.tags.map((t) => (
<Link
className={styles.tag}
key={t}
href={`/blog?tag=${encodeURIComponent(t)}`}
>
{t}
</Link>
))}
</div>
</article>
))}
</div>
{data && !data.items.length && <p>No articles match these filters.</p>}
<nav className={styles.form} aria-label="Blog pagination">
{data && pageNumber > 1 && (
<Link href={pageHref(pageNumber - 1)}>← Previous</Link>
)}
{data && pageNumber * data.pageSize < data.total && (
<Link href={pageHref(pageNumber + 1)}>Next →</Link>
)}
</nav>
</>
);
}
GET /api/v1/projects/demo/articles?organizationId={organizationId}&q=journey&tag=journal&order=desc&page=1&pageSize=12q: case-insensitive search across title, excerpt, and Markdown body; maximum 200 characters.tag: exact, case-insensitive tag matching; maximum 60 characters.order: asc or desc, using the editable article date.page: 1–100000. pageSize: 1–100; default 12.The response contains items, total, page, and pageSize. Only published articles appear. An empty result means there are no matching articles. A storage failure is an error and should reach the website’s error boundary.