Generate Sitemap

Generate a sitemap.xml for your blog using the slugs endpoint. This helps search engines discover and index all your published articles efficiently.

Overview

Generate a sitemap.xml for your blog to improve SEO. This uses the slugs endpoint to get all published article URLs and formats them for search engines.


Next.js App Router

Create app/sitemap.js to automatically generate your sitemap:

// app/sitemap.js
export default async function sitemap() {
  const response = await fetch(
    "https://beatrice.app/api/slugs?token=YOUR_TOKEN"
  );
  const slugs = await response.json();

  const articles = slugs.map((article) => ({
    url: `https://yoursite.com/blog/${article.slug}`,
    lastModified: article.updatedAt,
    changeFrequency: "weekly",
    priority: 0.8,
  }));

  return [
    {
      url: "https://yoursite.com",
      lastModified: new Date(),
      changeFrequency: "daily",
      priority: 1,
    },
    {
      url: "https://yoursite.com/blog",
      lastModified: new Date(),
      changeFrequency: "daily",
      priority: 0.9,
    },
    ...articles,
  ];
}

Your sitemap will be automatically available at https://yoursite.com/sitemap.xml.


Custom Sitemap Implementation

If you need more control, you can create a custom sitemap route:

// app/sitemap.xml/route.js
import { NextResponse } from "next/server";

export async function GET() {
  const response = await fetch(
    "https://beatrice.app/api/slugs?token=YOUR_TOKEN"
  );
  const slugs = await response.json();

  const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://yoursite.com</loc>
    <lastmod>${new Date().toISOString().split("T")[0]}</lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>https://yoursite.com/blog</loc>
    <lastmod>${new Date().toISOString().split("T")[0]}</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.9</priority>
  </url>
  ${slugs
    .map(
      (article) => `
  <url>
    <loc>https://yoursite.com/blog/${article.slug}</loc>
    <lastmod>${article.updatedAt.split("T")[0]}</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
  </url>
  `
    )
    .join("")}
</urlset>`;

  return new NextResponse(sitemap, {
    headers: {
      "Content-Type": "application/xml",
    },
  });
}

Sitemap Properties

Each URL in your sitemap can include these properties:

Property Type Description
url string Full URL of the page
lastModified Date When the page was last updated
changeFrequency string How often the page changes (always, hourly, daily, weekly, monthly, yearly, never)
priority number Relative importance (0.0 to 1.0)

Add to robots.txt

Add your sitemap to your robots.txt file:

# public/robots.txt
User-agent: *
Allow: /

Sitemap: https://yoursite.com/sitemap.xml

Notes

  • Only published articles (with publishedAt date) are included in the sitemap
  • The updatedAt field from the slugs endpoint is used for lastModified
  • Set appropriate changeFrequency and priority values for your content strategy
  • Submit your sitemap URL to Google Search Console and other search engines
  • Consider creating separate sitemaps for different content types if you have many articles