Search code examples
javascripttypescriptnext.jssitemap.xml

sitemap.xml not working after build in Next.js


I'm working on a Next.js project where I need to generate a sitemap using sitemap.xml. The sitemap works perfectly in development, but after building the project for production, the /sitemap.xml URL returns no data.

I created sitemap.ts in /app

/app/sitemap.ts

import { MetadataRoute } from 'next'
import { getAllNews } from '../app/libs/api/news' 

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {

  let news = await getAllNews();

  console.log(news)
  return [ ...news]
}

Are there any specific configurations or common issues related to static generation and sitemap generation in Next.js that I might be missing?


Solution

  • You can try to use the map function on the list of news feeds items.

    const newsItems= news.map(item => ({
      url: `${process.env.NEXT_PUBLIC_BASE_URL}/news/${item.slug}`,
      lastModified: item.updatedAt,
    }));
    
    return newsItems;
    

    Make Sure that you have set the Environment variable fore the NEXT_PUBLIC_BASE_URL depending upon your Operating System.

    Please do share more detailed log. If problem presists.