Back to blog

How to Set Per-Post Metadata in a Next.js App Router Blog

Use each published post’s data to set its title, description, and canonical URL in Next.js, then check the tags on the live article.

Per-post title, description, and canonical URL for a Next.js blog article

In app/blog/[slug]/page.tsx, export generateMetadata, load the published post identified by the route slug, and return its title, description, and absolute canonical URL. Use the same post source that renders the article; otherwise, the page and its metadata can disagree.

Start with the published post and its URL

Identify the server-side loader your article page already uses. For the example below, assume getPublishedPostBySlug(slug) returns a post with slug, title, and description, or null when that slug is not published. This is an illustrative interface, not a Next.js or Ranks function. Adapt the import, field names, and published-post rule to your site. Keep drafts and scheduled posts out of this loader if their public article routes should not be available.

Also identify your production origin and article path. The example assumes posts live at https://example.com/blog/{slug}; example.com is fictional. Replace it with your real canonical origin, including your chosen www or non-www hostname, and change /blog/ if your routes use another path. A canonical URL should identify the intended public article, not a draft or preview address.

Return metadata from the post route

Next.js shows generateMetadata in a dynamic App Router post route: it awaits params, obtains the slug, and uses post data to set the title and description. The export belongs in a Server Component page or layout. Put this implementation in app/blog/[slug]/page.tsx, alongside your existing article page:

`import type { Metadata } from 'next'; import { notFound } from 'next/navigation'; import { getPublishedPostBySlug } from '@/lib/posts'; type Props = { params: Promise<{ slug: string }> }; const siteOrigin = 'https://example.com'; export async function generateMetadata({ params }: Props): Promise<Metadata> { const { slug } = await params; const post = await getPublishedPostBySlug(slug); if (!post) notFound(); return { title: { absolute: post.title }, description: post.description, alternates: { canonical: new URL('/blog/' + encodeURIComponent(post.slug), siteOrigin).toString(), }, }; }`

Replace the illustrative import and origin before using this code. title.absolute keeps the post title exact even if a parent layout has a title template; alternates.canonical receives an absolute URL assembled from the post’s slug. Your page’s default export should call the same published-post loader, call notFound() for a missing post, and render the returned article with your existing content renderer. Do not maintain a separate metadata-only list of articles.

For a fictional published record with slug onboarding-checklist, title “A SaaS Onboarding Checklist,” and description “Plan the first steps for new SaaS users,” this function returns that exact title and description plus the canonical https://example.com/blog/onboarding-checklist. A different published slug gets values from its own record rather than inheriting those of the first post.

Ranks says its articles live on the customer’s own blog and that automatic blog setup and publishing support Next.js App Router sites using TypeScript. Whether you review articles before publishing or use its supported automatic mode, this route mapping remains your website’s implementation: the article data supplies the values, while your Next.js page returns the metadata.

Check the deployed article, not just the function

Before release, check that the post record has the intended title and description and that the constructed canonical matches the URL you plan to publish. Then publish the article and deploy the route change as your site requires. Only after the destination is public should you inspect its live metadata.

Open the public post URL and inspect its rendered HTML or browser developer tools. Next.js generates the relevant page metadata tags, though dynamically rendered pages may stream metadata separately as generateMetadata resolves. Check the resulting <title>, <meta name="description">, and <link rel="canonical"> against the published record and public URL—not merely against what you expected the code to produce.

For the fictional example, the live title should read “A SaaS Onboarding Checklist,” the description should read “Plan the first steps for new SaaS users,” and the canonical should be https://example.com/blog/onboarding-checklist after you substitute your real domain. Open a second published post as well: if both pages show the same values, check whether the route is loading the requested slug and whether a shared layout or hard-coded metadata is overriding the post-specific return value.

This check covers page metadata, not every kind of article markup or preview. Use the Article structured-data guide for JSON-LD and the social-preview image guide for image tags. If the live tags are correct but a search result displays different wording, follow the separate title-link or description-snippet guide.

Sources

  1. Next.js: Metadata and OG imagesnextjs.org
  2. Ranksranks.so

Grow your SaaS traffic on autopilot

One vetted backlink submission and one targeted blog article per day, on autopilot. Track your growth, all in one dashboard.

Try Ranks