Next.js Discord

Discord Forum

Next app router sitemap generation & headless cms - not working

Unanswered
House Sparrow posted this in #help-forum
Open in Discord
House SparrowOP
Hello,

My sitemap.ts to generate a sitemap.xml is not working in my next js app. I am new to app directory, that sitemap will have some static routes but also dynamic route coming from a headless cms (here Prismic). Seems like it's not creating any sitemap:

type ChangeFrequency =
| 'always'
| 'hourly'
| 'daily'
| 'weekly'
| 'monthly'
| 'yearly'
| 'never'

interface Route {
url: string
lastModified: Date
changeFrequency: ChangeFrequency
priority: number
}

const siteUrl = process.env.SITE_URL || 'https://guadeloupeloc.fr/'

async function getRoutes(): Promise<Route[]> {
const client = createClient()

// Fetch dynamic routes from Prismic
const dynamicPagesData = await fetchAllTypesPages(client)

// Convert dynamic pages data to Route objects
const dynamicPages: Route[] = dynamicPagesData.map(({ url }) => ({
url: ${siteUrl}${url},
lastModified: new Date(), // Consider dynamically setting this based on your data
changeFrequency: 'monthly', // Consider dynamically setting this based on your content update frequency
priority: 0.6 // Consider dynamically setting this based on the importance of the page
}))

// Static routes
const staticPages: Route[] = [
// Add more static routes here
{
url: ${siteUrl}/plan-de-site,
lastModified: new Date(),
changeFrequency: 'monthly',
priority: 0.7
},
{
url: ${siteUrl}/signup,
lastModified: new Date(),
changeFrequency: 'monthly',
priority: 0.7
}
]

// Merge dynamic and static routes
const allPages = [...dynamicPages, ...staticPages]

return allPages
}

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const routes = await getRoutes()
return routes.map((route) => ({
url: route.url,
lastModified: route.lastModified.toISOString(), // Ensure lastModified is a string
changeFrequency: route.changeFrequency,
priority: route.priority
}))
}

0 Replies