frontend.blogwoman.de/src/app/sitemap.ts

85 lines
1.9 KiB
TypeScript

import type { MetadataRoute } from 'next'
import { getPage, getPages, getPosts } from '@/lib/api'
const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL || 'https://blogwoman.de'
function buildUrl(path: string) {
return `${SITE_URL}${path.startsWith('/') ? path : `/${path}`}`
}
async function getAllPages() {
const allPages = []
let page = 1
while (true) {
const data = await getPages({ limit: 100, page })
allPages.push(...data.docs)
if (!data.hasNextPage) break
page = data.nextPage || page + 1
}
return allPages
}
async function getAllPosts() {
const allPosts = []
let page = 1
while (true) {
const data = await getPosts({ limit: 100, page })
allPosts.push(...data.docs)
if (!data.hasNextPage) break
page = data.nextPage || page + 1
}
return allPosts
}
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const [home, pages, posts] = await Promise.all([
getPage('home'),
getAllPages(),
getAllPosts(),
])
const entries: MetadataRoute.Sitemap = []
if (home) {
entries.push({
url: buildUrl('/'),
lastModified: home.updatedAt || home.createdAt,
changeFrequency: 'daily',
priority: 1,
})
}
pages
.filter((page) => page.slug !== 'home')
.forEach((page) => {
entries.push({
url: buildUrl(`/${page.slug}`),
lastModified: page.updatedAt || page.createdAt,
changeFrequency: 'weekly',
priority: 0.8,
})
})
const postPrefix: Record<string, string> = {
blog: '/blog',
news: '/news',
press: '/presse',
announcement: '/aktuelles',
}
posts.forEach((post) => {
const prefix = postPrefix[post.type] || '/blog'
entries.push({
url: buildUrl(`${prefix}/${post.slug}`),
lastModified: post.updatedAt || post.createdAt,
changeFrequency: 'monthly',
priority: 0.6,
})
})
return entries
}