50 lines
2.3 KiB
TypeScript
50 lines
2.3 KiB
TypeScript
import BlogPage from "../../components/BlogPage";
|
||
import type { Metadata } from 'next';
|
||
import { getPageMetadata } from "../../lib/seo";
|
||
|
||
export async function generateMetadata(): Promise<Metadata> {
|
||
return getPageMetadata('blog', {
|
||
canonicalPath: '/blog',
|
||
fallbackTitle: 'مجله سلامت و دانشنامه مقالات تخصصی پت',
|
||
fallbackDesc: 'جدیدترین مقالات علمی، راهنمای تغذیه، بیماریهای سگ و گربه و توصیههای بالینی متخصصان و دامپزشکان کنینا.',
|
||
});
|
||
}
|
||
|
||
async function getBlogs() {
|
||
const apiUrl = process.env.INTERNAL_API_URL || process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4001/api';
|
||
const apiBase = apiUrl.endsWith('/api') ? apiUrl : `${apiUrl.replace(/\/$/, '')}/api`;
|
||
try {
|
||
const res = await fetch(`${apiBase}/blogs`, { next: { revalidate: 60 } });
|
||
if (!res.ok) {
|
||
console.error(`[Blog] Failed to fetch blogs: ${res.status}`);
|
||
return [];
|
||
}
|
||
const data = await res.json();
|
||
const list = Array.isArray(data.data) ? data.data : Array.isArray(data) ? data : [];
|
||
return list.map((b: Record<string, unknown>) => {
|
||
const authorObj = b.author as Record<string, string> | undefined;
|
||
const authorName = authorObj ? `${authorObj.firstName || ''} ${authorObj.lastName || ''}`.trim() : 'نویسنده کنینا';
|
||
return {
|
||
id: String(b.id || ''),
|
||
slug: String(b.slug || ''),
|
||
title: String(b.title || ''),
|
||
excerpt: String(b.metaDescription || (typeof b.content === 'string' ? b.content.substring(0, 150).replace(/<[^>]+>/g, '') + '...' : '')),
|
||
category: String(b.category || 'تغذیه و سلامت'),
|
||
date: b.createdAt ? new Date(String(b.createdAt)).toLocaleDateString('fa-IR') : '',
|
||
readTime: '۵ دقیقه',
|
||
image: String(b.imageUrl || b.coverImage || 'https://images.unsplash.com/photo-1548767797-d8c844163c4c?auto=format&fit=crop&q=80&w=800'),
|
||
author: authorName || 'نویسنده کنینا',
|
||
content: String(b.content || ''),
|
||
};
|
||
});
|
||
} catch (error) {
|
||
console.error('[Blog] Error fetching blogs:', error);
|
||
return [];
|
||
}
|
||
}
|
||
|
||
export default async function Blog() {
|
||
const blogs = await getBlogs();
|
||
return <BlogPage blogs={blogs} />;
|
||
}
|