@@ -22,8 +22,9 @@ import {
2222} from 'lucide-react'
2323
2424import { LibraryWordmark } from '~/components/LibraryWordmark'
25- import type { LibrarySlim } from '~/libraries'
26- import { formatPublishedDate , getPostsForLibrary } from '~/utils/blog'
25+ import type { LibraryId , LibrarySlim } from '~/libraries'
26+ import { formatPublishedDate } from '~/utils/blog-format'
27+ import type { RelatedPost as RelatedPostData } from '~/utils/blog.functions'
2728import {
2829 categoryMeta ,
2930 getCategoryLibraries ,
@@ -44,10 +45,16 @@ const libraryLinkClassName =
4445const staticPanelClassName =
4546 'rounded-lg border border-zinc-200 bg-white p-4 dark:border-zinc-800 dark:bg-zinc-950'
4647
47- export function CategoryArticle ( { slug } : { slug : CategorySlug } ) {
48+ export function CategoryArticle ( {
49+ slug,
50+ relatedPosts : relatedPostsData ,
51+ } : {
52+ slug : CategorySlug
53+ relatedPosts : Array < RelatedPostData >
54+ } ) {
4855 const meta = categoryMeta [ slug ]
4956 const libraries = getCategoryLibraries ( slug )
50- const relatedPosts = getRelatedPosts ( libraries )
57+ const relatedPosts = reconstructRelatedPosts ( libraries , relatedPostsData )
5158
5259 return (
5360 < div className = "bg-white text-zinc-950 dark:bg-zinc-950 dark:text-zinc-50" >
@@ -75,10 +82,23 @@ export function CategoryArticle({ slug }: { slug: CategorySlug }) {
7582 )
7683}
7784
78- function getRelatedPosts ( libraries : Array < LibrarySlim > ) {
79- return libraries
80- . flatMap ( ( lib ) => getPostsForLibrary ( lib . id ) . map ( ( post ) => ( { post, lib } ) ) )
81- . slice ( 0 , 4 )
85+ /**
86+ * Reconstructs {post, lib} pairs from the server-provided, already-ordered
87+ * and already-sliced related-posts data, using the in-memory `libraries`
88+ * array (pure, client-safe) rather than sending non-serializable LibrarySlim
89+ * objects (e.g. `handleRedirects`) over the server-fn RPC boundary.
90+ */
91+ function reconstructRelatedPosts (
92+ libraries : Array < LibrarySlim > ,
93+ data : Array < RelatedPostData > ,
94+ ) : Array < RelatedPost > {
95+ const libraryById = new Map < LibraryId , LibrarySlim > (
96+ libraries . map ( ( lib ) => [ lib . id , lib ] ) ,
97+ )
98+ return data . flatMap ( ( { libraryId, post } ) => {
99+ const lib = libraryById . get ( libraryId )
100+ return lib ? [ { post, lib } ] : [ ]
101+ } )
82102}
83103
84104function Breadcrumb ( { categoryName } : { categoryName : string } ) {
0 commit comments