Next.js Discord

Discord Forum

Dynamic Sitemap

Unanswered
Tomistoma posted this in #help-forum
Open in Discord
Avatar
TomistomaOP
Hi Everyone,
I've deployed my Blog with vercel and everything seems to run pretty smoothly. The only thing I've problem with is the dynamic sitemap. My file is located in app/sitemap.js. and this is my code.

import { getPosts } from "@/config/contentful/client"

export default async function sitemap() {
  const posts = (await getPosts()).items;
  const postsEntries = posts.map(post => (
    {
      url: `${process.env.NEXT_PUBLIC_SITE_URL}/posts/${post.fields.postSlug}`,
      lastModified: new Date(post.sys.updatedAt)
    }
  ))
  return [
    {
      url: `${process.env.NEXT_PUBLIC_SITE_URL}`,
      changeFrequency: 'yearly',
    },
    {
      url:`${process.env.NEXT_PUBLIC_SITE_URL}/blog`,
      changeFrequency: 'monthly',
    },
    {
      url: `${process.env.NEXT_PUBLIC_SITE_URL}/about`,
      changeFrequency: 'yearly',
    },
    ...postsEntries
  ]
}


At the moment if I don't push to production the entire app the sitemap doesn't get updated and so misses the entire purpose of the function to generate a new sitemap dynamically on every request. Did anyone face the same problem? How did you fix it? Thanks for help me in advance 😉

0 Replies