RankFloRankFlo
5 min read

How to Generate Sitemaps Dynamically with a Headless CMS

Static sitemaps get outdated. Learn how to generate dynamic XML sitemaps from your CMS API with Next.js, Astro, and Remix examples.

R

ruben

Why Dynamic Sitemaps Matter

A sitemap tells Google which pages exist on your site and when they were last updated. A static sitemap gets outdated the moment you publish a new post. Dynamic sitemaps generate from your CMS data, ensuring Google always has the latest picture of your content.

Next.js Implementation

// app/sitemap.ts\nimport { MetadataRoute } from 'next';\n\nexport default async function sitemap(): Promise<MetadataRoute.Sitemap> {\n  const res = await fetch('https://app.rankflo.io/api/v1/content?project_key=blg_xxx');\n  const { data: posts } = await res.json();\n  \n  return posts.map(post => ({\n    url: `https://yoursite.com/blog/${post.slug}`,\n    lastModified: post.updatedAt,\n    changeFrequency: 'weekly',\n    priority: 0.8,\n  }));\n}

Astro Implementation

// src/pages/sitemap.xml.ts\nexport async function GET() {\n  const posts = await getPosts();\n  const xml = posts.map(p =>\n    `<url><loc>https://yoursite.com/blog/${p.slug}</loc></url>`\n  ).join(');\n  return new Response(`<?xml version="1.0"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${xml}</urlset>`);\n}

Best Practices

  • Include lastmod dates for every URL
  • Submit your sitemap to Google Search Console
  • Reference your sitemap in robots.txt
  • Keep sitemap under 50,000 URLs (use sitemap index for larger sites)