Next.js Discord

Discord Forum

Sitemap only updating on build(app router)

Unanswered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
I am using this code to generate sitemaps dynamically:

// app/sitemap.js

import { getAllPosts, getAllCategories } from "@/lib/sanity/client";

const rootUrl = "https://example.com";

const STATIC_PAGES = [
  {
    url: "/",
    lastModified: new Date().toISOString()
  },
  {
    url: "/post",
    lastModified: new Date().toISOString()
  },
  {
    url: "/category",
    lastModified: new Date().toISOString()
  },
  {
    url: "/about",
    lastModified: new Date().toISOString()
  },
  {
    url: "/contact",
    lastModified: new Date().toISOString()
  },
  {
    url: "/privacy",
    lastModified: new Date().toISOString()
  },
  {
    url: "/terms",
    lastModified: new Date().toISOString()
  },
  {
    url: "/cookie-policy",
    lastModified: new Date().toISOString()
  },
  {
    url: "/disclosure",
    lastModified: new Date().toISOString()
  },
  {
    url: "/sitemap.xml",
    lastModified: new Date().toISOString()
  }
];

export default async function sitemap() {
  const allPosts = await getAllPosts();
  const posts = allPosts.map(({ slug, publishedAt }) => ({
    url: `${rootUrl}/${slug.current}`,
    lastModified: publishedAt
  }));
  const allCategories = await getAllCategories();

  const categories = allCategories.map(({ category }) => ({
    url: `${rootUrl}/category/${category}`,
    lastModified: new Date().toISOString()
  }));

  return [...STATIC_PAGES, ...categories, ...posts];
}


But the sitemap is updating only during build. Can someone help me how to make this dynamic?

0 Replies