sitemap.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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() ?? new Date().toISOString(),
  18. changeFrequency,
  19. priority: 0.7,
  20. }));
  21. const latestBlogEdit =
  22. blogPosts.length > 0
  23. ? blogPosts
  24. .reduce((latest, post) =>
  25. (post.lastEditedAt?.getTime() ?? 0) >
  26. (latest.lastEditedAt?.getTime() ?? 0)
  27. ? post
  28. : latest,
  29. )
  30. .lastEditedAt?.toISOString() ?? new Date().toISOString()
  31. : new Date().toISOString();
  32. const routes: MetadataRoute.Sitemap = [
  33. {
  34. url: siteConfig.url,
  35. lastModified: latestBlogEdit,
  36. changeFrequency: "weekly" as ChangeFrequency,
  37. priority: 1.0,
  38. },
  39. {
  40. url: `${siteConfig.url}/blog`,
  41. lastModified: latestBlogEdit,
  42. changeFrequency: "weekly" as ChangeFrequency,
  43. priority: 0.8,
  44. },
  45. ];
  46. return [...routes, ...blogs];
  47. }