refactor(blog) update sitemap for blog pages

This commit is contained in:
javayhu 2025-06-17 18:17:05 +08:00
parent 568ef9bc3a
commit da0176ffc5

View File

@ -1,6 +1,7 @@
import { websiteConfig } from '@/config/website';
import { getLocalePathname } from '@/i18n/navigation';
import { routing } from '@/i18n/routing';
import { source } from '@/lib/docs/source';
import { blogSource, categorySource, source } from '@/lib/docs/source';
import type { MetadataRoute } from 'next';
import type { Locale } from 'next-intl';
import { getBaseUrl } from '../lib/urls/urls';
@ -48,86 +49,87 @@ export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
);
// add categories
// sitemapList.push(
// ...allCategories.flatMap((category: { slug: string }) =>
// routing.locales.map((locale) => ({
// url: getUrl(`/blog/category/${category.slug}`, locale),
// lastModified: new Date(),
// priority: 0.8,
// changeFrequency: 'weekly' as const,
// }))
// )
// );
sitemapList.push(
...categorySource.getPages().flatMap((category) =>
routing.locales.map((locale) => ({
url: getUrl(`/blog/category/${category.slugs[0]}`, locale),
lastModified: new Date(),
priority: 0.8,
changeFrequency: 'weekly' as const,
}))
)
);
// add paginated blog list pages
// routing.locales.forEach((locale) => {
// const posts = allPosts.filter(
// (post) => post.locale === locale && post.published
// );
// const totalPages = Math.max(
// 1,
// Math.ceil(posts.length / websiteConfig.blog.paginationSize)
// );
// // /blog/page/[page] (from 2)
// for (let page = 2; page <= totalPages; page++) {
// sitemapList.push({
// url: getUrl(`/blog/page/${page}`, locale),
// lastModified: new Date(),
// priority: 0.8,
// changeFrequency: 'weekly' as const,
// });
// }
// });
routing.locales.forEach((locale) => {
const posts = blogSource
.getPages(locale)
.filter((post) => post.data.published);
const totalPages = Math.max(
1,
Math.ceil(posts.length / websiteConfig.blog.paginationSize)
);
// /blog/page/[page] (from 2)
for (let page = 2; page <= totalPages; page++) {
sitemapList.push({
url: getUrl(`/blog/page/${page}`, locale),
lastModified: new Date(),
priority: 0.8,
changeFrequency: 'weekly' as const,
});
}
});
// add paginated category pages
// routing.locales.forEach((locale) => {
// const localeCategories = allCategories.filter(
// (category) => category.locale === locale
// );
// localeCategories.forEach((category) => {
// // posts in this category and locale
// const postsInCategory = allPosts.filter(
// (post) =>
// post.locale === locale &&
// post.published &&
// post.categories.some((cat) => cat && cat.slug === category.slug)
// );
// const totalPages = Math.max(
// 1,
// Math.ceil(postsInCategory.length / websiteConfig.blog.paginationSize)
// );
// // /blog/category/[slug] (first page)
// sitemapList.push({
// url: getUrl(`/blog/category/${category.slug}`, locale),
// lastModified: new Date(),
// priority: 0.8,
// changeFrequency: 'weekly' as const,
// });
// // /blog/category/[slug]/page/[page] (from 2)
// for (let page = 2; page <= totalPages; page++) {
// sitemapList.push({
// url: getUrl(`/blog/category/${category.slug}/page/${page}`, locale),
// lastModified: new Date(),
// priority: 0.8,
// changeFrequency: 'weekly' as const,
// });
// }
// });
// });
routing.locales.forEach((locale) => {
const localeCategories = categorySource.getPages(locale);
localeCategories.forEach((category) => {
// posts in this category and locale
const postsInCategory = blogSource
.getPages(locale)
.filter((post) => post.data.published)
.filter((post) =>
post.data.categories.some((cat) => cat === category.slugs[0])
);
const totalPages = Math.max(
1,
Math.ceil(postsInCategory.length / websiteConfig.blog.paginationSize)
);
// /blog/category/[slug] (first page)
sitemapList.push({
url: getUrl(`/blog/category/${category.slugs[0]}`, locale),
lastModified: new Date(),
priority: 0.8,
changeFrequency: 'weekly' as const,
});
// /blog/category/[slug]/page/[page] (from 2)
for (let page = 2; page <= totalPages; page++) {
sitemapList.push({
url: getUrl(
`/blog/category/${category.slugs[0]}/page/${page}`,
locale
),
lastModified: new Date(),
priority: 0.8,
changeFrequency: 'weekly' as const,
});
}
});
});
// // add posts (single post pages)
// sitemapList.push(
// ...allPosts.flatMap((post: { slugAsParams: string; locale: string }) =>
// routing.locales
// .filter((locale) => post.locale === locale)
// .map((locale) => ({
// url: getUrl(`/blog/${post.slugAsParams}`, locale),
// lastModified: new Date(),
// priority: 0.8,
// changeFrequency: 'weekly' as const,
// }))
// )
// );
// add posts (single post pages)
sitemapList.push(
...blogSource.getPages().flatMap((post) =>
routing.locales
.filter((locale) => post.locale === locale)
.map((locale) => ({
url: getUrl(`/blog/${post.slugs.join('/')}`, locale),
lastModified: new Date(),
priority: 0.8,
changeFrequency: 'weekly' as const,
}))
)
);
// add docs
const docsParams = source.generateParams();