feat(blog): enhance blog data handling with normalization and improved image management

This commit is contained in:
hamed
2026-06-19 00:35:11 +03:30
parent 4dca516598
commit e9136ea42b
10 changed files with 77 additions and 71 deletions
+27 -19
View File
@@ -2,6 +2,9 @@ import BlogPage from "@/components/blog";
import Layout from "@/components/layout/StLayout";
import { fetchReq } from "@/lib/req";
import { getStateInfo } from "@/lib/getStateInfo";
import { normalizeBlog, imageUrl } from "@/helper";
const FALLBACK_IMG = "https://www.nobat724.com/assets/images/logo.png";
export async function generateMetadata({ params }) {
const { slug } = await params;
@@ -10,12 +13,16 @@ export async function generateMetadata({ params }) {
const siteName = matchedCity?.site_name || "نوبت 724";
try {
const blog = await fetchReq(`${API_URL}/api/v1/blog/${slug}`);
const res = await fetchReq(`${API_URL}/api/v1/blog/${slug}`);
const blog = res?.data?.data;
if (!blog) return {};
const title = `${blog.title} | ${siteName}`;
const rawText = blog.body ? blog.body.replace(/<[^>]*>/g, "").trim() : "";
const rawText = (blog.summary || blog.body || "")
.replace(/<[^>]*>/g, "")
.trim();
const description = rawText.slice(0, 160) || blog.title;
const image = blog.image_url ? imageUrl(blog.image_url) : FALLBACK_IMG;
return {
title,
@@ -24,18 +31,16 @@ export async function generateMetadata({ params }) {
title,
description,
type: "article",
...(blog.created && { publishedTime: blog.created }),
images: blog.images?.[0]
? [blog.images[0]]
: ["https://www.nobat724.com/assets/images/logo.png"],
...(blog.created_at && {
publishedTime: new Date(blog.created_at * 1000).toISOString(),
}),
images: [image],
},
twitter: {
card: "summary_large_image",
title,
description,
images: blog.images?.[0]
? [blog.images[0]]
: ["https://www.nobat724.com/assets/images/logo.png"],
images: [image],
},
};
} catch {
@@ -47,24 +52,27 @@ async function Blog({ params }) {
const { slug } = await params;
const API_URL = process.env.NEXT_PUBLIC_API_URL;
const blog = await fetchReq(`${API_URL}/api/v1/blog/${slug}`);
const res = await fetchReq(`${API_URL}/api/v1/blog/${slug}`);
const blog = normalizeBlog(res?.data?.data);
let relatedBlogs = [];
if (blog?.tag?.[0]?.id) {
const tagId = blog.tag[0].id;
const relatedResponse = await fetchReq(
`${API_URL}/api/v1/blogs?page=1&limit=12&tag=${tagId}`
);
relatedBlogs = relatedResponse?.blogs || [];
}
const relatedResponse = await fetchReq(
`${API_URL}/api/v1/blogs?page=1&limit=6`
);
relatedBlogs = (relatedResponse?.data || [])
.filter((b) => b.uuid !== blog?.uuid)
.slice(0, 4)
.map(normalizeBlog);
const jsonLd = blog
? {
"@context": "https://schema.org",
"@type": "Article",
headline: blog.title,
...(blog.images?.[0] && { image: blog.images[0] }),
...(blog.created && { datePublished: blog.created }),
...(blog.images?.[0]?.url && { image: imageUrl(blog.images[0].url) }),
...(blog.created && {
datePublished: new Date(blog.created * 1000).toISOString(),
}),
...(blog.author && { author: { "@type": "Person", name: blog.author } }),
}
: null;