sitemap.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { MetadataRoute } from "next";
  2. import { notionBlogConfig, siteConfig } from "@/config/site";
  3. import { getAllBlogPosts } from "@/lib/notion";
  4. type ChangeFrequency =
  5. | "always"
  6. | "hourly"
  7. | "daily"
  8. | "weekly"
  9. | "monthly"
  10. | "yearly"
  11. | "never";
  12. export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
  13. const blogPosts = await getAllBlogPosts(notionBlogConfig.blogParentId);
  14. const changeFrequency: ChangeFrequency = "weekly";
  15. const blogs = blogPosts.map(({ id, lastEditedAt }) => ({
  16. url: `${siteConfig.url}/blog/${id}`,
  17. lastModified: lastEditedAt.toISOString(),
  18. changeFrequency,
  19. priority: 0.7,
  20. }));
  21. // Use the most recent blog post's edit time for the blog list page
  22. const latestBlogEdit =
  23. blogPosts.length > 0
  24. ? blogPosts
  25. .reduce((latest, post) =>
  26. post.lastEditedAt > latest.lastEditedAt ? post : latest,
  27. )
  28. .lastEditedAt.toISOString()
  29. : new Date().toISOString();
  30. const routes: MetadataRoute.Sitemap = [
  31. {
  32. url: siteConfig.url,
  33. lastModified: latestBlogEdit,
  34. changeFrequency: "weekly" as ChangeFrequency,
  35. priority: 1.0,
  36. },
  37. {
  38. url: `${siteConfig.url}/blog`,
  39. lastModified: latestBlogEdit,
  40. changeFrequency: "weekly" as ChangeFrequency,
  41. priority: 0.8,
  42. },
  43. ];
  44. return [...routes, ...blogs];
  45. }