data.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { cache } from "react";
  2. import { ref, getDownloadURL, listAll } from "firebase/storage";
  3. import { storage } from "@/firebase/firebase";
  4. import { notionBlogConfig } from "@/config/site";
  5. import { getAllBlogPosts } from "@/lib/notion";
  6. // Use React cache to dedupe requests within a single render pass
  7. export const getHomeData = cache(async () => {
  8. const paths = {
  9. photos: "photos",
  10. avatar: "avatar/eric.jpg",
  11. dog: "avatar/dog.jpg",
  12. webagent: "projects/webagent.jpg",
  13. chatbot: "projects/chatbot.jpg",
  14. paper: "projects/paper.jpg",
  15. };
  16. const allPaths = [
  17. paths.avatar,
  18. paths.dog,
  19. paths.webagent,
  20. paths.chatbot,
  21. paths.paper,
  22. ];
  23. // Parallel data fetching - initiate all requests at once
  24. const [photosResult, ...urlResults] = await Promise.all([
  25. listAll(ref(storage, paths.photos)).then((res) =>
  26. Promise.all(res.items.map((item) => getDownloadURL(item))),
  27. ),
  28. ...allPaths.map((p) => getDownloadURL(ref(storage, p))),
  29. ]);
  30. return {
  31. photos: photosResult,
  32. avatarUrl: urlResults[0],
  33. dogUrl: urlResults[1],
  34. webagentUrl: urlResults[2],
  35. chatbotUrl: urlResults[3],
  36. paperUrl: urlResults[4],
  37. };
  38. });
  39. /** Latest posts for the home page; the page still renders if Notion is down. */
  40. export const getLatestPosts = cache(async () => {
  41. try {
  42. const posts = await getAllBlogPosts(notionBlogConfig.blogParentId);
  43. return posts.map((p) => ({
  44. id: p.id,
  45. title: p.title,
  46. createdAt: p.createdAt ? p.createdAt.toISOString() : null,
  47. }));
  48. } catch (error) {
  49. console.error("Failed to load blog posts", error);
  50. return [];
  51. }
  52. });