Postream

Loading the index…

Docs: SEO kit

SEO kit

Metadata, hreflang, JSON-LD, sitemaps, robots.txt, llms.txt and RSS ready for the blog, built from Postream posts.

The @postream/seo package ships the public API client and the SEO kit for Next.js blogs: metadata with canonical and hreflang (x-default in the default language), JSON-LD in a single @graph per page, per-language sitemaps with an index, a robots.txt that lets search and AI crawlers in, llms.txt, a table of contents with anchors, FAQ in <details>, related posts and RSS. Pure functions, with no runtime dependency; the return values fit Next's Metadata and MetadataRoute types.

pnpm add @postream/seo

Set up

Server only, without NEXT_PUBLIC_, with the secret key:

.env
POSTREAM_API_URL=https://api.postream.blog/public/v1
POSTREAM_SITE_ID=7c0e...
POSTREAM_API_KEY=sk_...

With siteId, the client uses the per-site route (edge cache, purged when the post changes) and sends the list queries in the canonical form. postPath says where each version lives on the blog; the package assumes no URL structure.

lib/postream.ts
import { createPostreamClient, organizationFromSite, type GraphConfig } from "@postream/seo";
 
export const postream = createPostreamClient({
  apiUrl: process.env.POSTREAM_API_URL!,
  siteId: process.env.POSTREAM_SITE_ID!,
  apiKey: process.env.POSTREAM_API_KEY!,
  // Revalidate every 5 minutes (ISR) or right away, by tag, when a webhook arrives.
  init: { next: { revalidate: 300, tags: ["postream"] } },
});
 
export const site = await postream.getSite();
 
export const seo: GraphConfig = {
  siteUrl: site.url,
  defaultLocale: site.xDefaultLocale,
  postPath: ({ locale, slug }) => `/${locale}/blog/${slug}`,
  // Name, description and profiles come from the panel; the logo size is yours.
  organization: organizationFromSite(site, { width: 512, height: 512 }),
  // For posts without an image of known size.
  defaultImage: { url: "/og.png", width: 1200, height: 630 },
};

Post page

app/[locale]/blog/[slug]/page.tsx
import type { Metadata } from "next";
import { notFound, permanentRedirect } from "next/navigation";
import { postJsonLd, postMetadata, postParts, postSummary, postUrl, serializeJsonLd } from "@postream/seo";
import { postream, seo } from "@/lib/postream";
 
type Props = { params: Promise<{ locale: string; slug: string }> };
 
export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const { locale, slug } = await params;
  const post = await postream.getPost(slug, { locale });
  return post ? postMetadata(post, seo) : {};
}
 
export default async function BlogPost({ params }: Props) {
  const { locale, slug } = await params;
  const post = await postream.getPost(slug, { locale });
  if (!post) notFound();
  // The API followed the 301 of an old slug: send the visitor to the new URL.
  if (post.slug !== slug) permanentRedirect(postUrl(seo, post));
  const { bodyHtml, faq } = postParts(post);
  return (
    <article>
      <script type="application/ld+json" dangerouslySetInnerHTML={{ __html: serializeJsonLd(postJsonLd(post, seo)) }} />
      <h1>{post.title}</h1>
      <p>{postSummary(post)}</p>
      <div dangerouslySetInnerHTML={{ __html: bodyHtml }} />
      {faq.map((q) => (
        <details key={q.id} id={q.id}>
          <summary>{q.question}</summary>
          <p>{q.answer}</p>
        </details>
      ))}
    </article>
  );
}

The graph carries Organization, WebSite, WebPage, BreadcrumbList, Person, BlogPosting and, when the post has them, FAQPage and HowTo. serializeJsonLd escapes <, > and &: post text cannot close the <script> tag. In the blog's tests, validateJsonLdGraph(postJsonLd(post, seo)) returns [] when the graph is sound.

Sitemaps, robots.txt and llms.txt

// app/sitemap.ts
import { postsSitemap } from "@postream/seo";
import { postream, seo } from "@/lib/postream";
 
export default async function sitemap() {
  return postsSitemap(await postream.listSitemap(), seo);
}
 
// app/robots.ts
import { robots } from "@postream/seo";
import { seo } from "@/lib/postream";
 
export default function robotsTxt() {
  return robots(seo, { disallow: ["/api/"] });
}

For one sitemap per language with an index, use localeSitemaps, sitemapIndexXml and sitemapXml. The llms.txt comes from llmsTxt(await postream.listAllPosts(), seo, { categories: site.categories }). lastmod and dateModified come from the API's updatedAt, which only changes with the content.

Other helpers

postsRss (RSS 2.0), relatedPosts (pillar ↔ satellites), tableOfContents, authorProfile, siteJsonLd (home and lists), postAlternates, languageAlternates and absoluteUrl. The client also has listPosts, listAllPosts, listAuthors, getAuthor and listCategories. API errors come out as PostreamApiError with status and code; getPost and getAuthor return null on a 404.

To measure reading and engagement on the same pages, see Analytics.