Files
myeasycms-v2/apps/web/app/(marketing)/blog/[slug]/page.tsx
giancarlo bf7e3185de Refactor cache functions to use explicit loaders
The commit refactors the previous implementation of using cache functions across several modules. They are now explicitly defined as loaders to improve readability and maintain a consistent style. This prevents the cache function calls from getting too nested and difficult to understand, especially in asynchronous cases. Additionally, the user session related hooks are deleted which were not used anymore.
2024-06-04 01:03:57 +07:00

75 lines
1.5 KiB
TypeScript

import { cache } from 'react';
import type { Metadata } from 'next';
import { notFound } from 'next/navigation';
import { createCmsClient } from '@kit/cms';
import { withI18n } from '~/lib/i18n/with-i18n';
import { Post } from '../../blog/_components/post';
const getPostBySlug = cache(postLoader);
async function postLoader(slug: string) {
const client = await createCmsClient();
return client.getContentItemBySlug({ slug, collection: 'posts' });
}
export async function generateMetadata({
params,
}: {
params: { slug: string };
}): Promise<Metadata | undefined> {
const post = await getPostBySlug(params.slug);
if (!post) {
notFound();
}
const { title, publishedAt, description, image } = post;
return Promise.resolve({
title,
description,
openGraph: {
title,
description,
type: 'article',
publishedTime: publishedAt,
url: post.url,
images: image
? [
{
url: image,
},
]
: [],
},
twitter: {
card: 'summary_large_image',
title,
description,
images: image ? [image] : [],
},
});
}
async function BlogPost({ params }: { params: { slug: string } }) {
const post = await getPostBySlug(params.slug);
if (!post) {
notFound();
}
return (
<div className={'container sm:max-w-none sm:p-0'}>
<Post post={post} content={post.content} />
</div>
);
}
export default withI18n(BlogPost);