Files
website/src/app/sitemap.ts
T

104 lines
2.7 KiB
TypeScript
Raw Normal View History

2026-04-23 01:08:18 +08:00
import type { MetadataRoute } from 'next'
import { getPayloadClient } from '@/lib/payload'
import { buildAbsoluteUrl } from '@/lib/seo'
import type { Locale } from '@/i18n/config'
export const dynamic = 'force-dynamic'
const LOCALES: Locale[] = ['en', 'zh']
const STATIC_PATHS = ['', '/products', '/support', '/support/faq', '/support/warranty', '/support/contact', '/downloads', '/about', '/dealers', '/news']
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const payload = await getPayloadClient()
const [productsEn, productsZh, newsEn, newsZh] = await Promise.all([
payload.find({
collection: 'products',
depth: 0,
limit: 100,
locale: 'en',
where: { docLocale: { equals: 'en' } },
}),
payload.find({
collection: 'products',
depth: 0,
limit: 100,
locale: 'zh',
where: { docLocale: { equals: 'zh' } },
}),
payload.find({
collection: 'news',
depth: 0,
limit: 200,
locale: 'en',
where: { docLocale: { equals: 'en' } },
}),
payload.find({
collection: 'news',
depth: 0,
limit: 200,
locale: 'zh',
where: { docLocale: { equals: 'zh' } },
}),
])
const entries: MetadataRoute.Sitemap = []
for (const locale of LOCALES) {
for (const path of STATIC_PATHS) {
entries.push({
changeFrequency: path === '' ? 'weekly' : 'monthly',
lastModified: new Date(),
priority: path === '' ? 1 : 0.7,
url: buildAbsoluteUrl(`/${locale}${path}`),
})
}
}
for (const product of productsEn.docs) {
if (product.slug) {
entries.push({
changeFrequency: 'weekly',
lastModified: product.updatedAt || new Date(),
priority: 0.8,
url: buildAbsoluteUrl(`/en/products/${product.slug}`),
})
}
}
for (const product of productsZh.docs) {
if (product.slug) {
entries.push({
changeFrequency: 'weekly',
lastModified: product.updatedAt || new Date(),
priority: 0.8,
url: buildAbsoluteUrl(`/zh/products/${product.slug}`),
})
}
}
for (const article of newsEn.docs) {
if (article.slug) {
entries.push({
changeFrequency: 'monthly',
lastModified: article.updatedAt || article.publishedAt || new Date(),
priority: 0.65,
url: buildAbsoluteUrl(`/en/news/${article.slug}`),
})
}
}
for (const article of newsZh.docs) {
if (article.slug) {
entries.push({
changeFrequency: 'monthly',
lastModified: article.updatedAt || article.publishedAt || new Date(),
priority: 0.65,
url: buildAbsoluteUrl(`/zh/news/${article.slug}`),
})
}
}
return entries
}