From d67069081690cfb9dae8c5c1094dff5059142f08 Mon Sep 17 00:00:00 2001 From: devops Date: Tue, 20 Oct 2026 07:15:20 +0000 Subject: [PATCH] fix(blog): unify structured author identity --- .../app/blog/[slug]/blog-slug-page.test.tsx | 29 ++++++++++++++++- frontend/application/app/blog/[slug]/page.tsx | 24 ++++++-------- .../application/components/BlogPostClient.tsx | 5 +-- frontend/application/lib/blogAuthor.ts | 32 ++++++++++++++++--- 4 files changed, 69 insertions(+), 21 deletions(-) diff --git a/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx b/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx index 20732c11..1beeb247 100644 --- a/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx +++ b/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx @@ -1,5 +1,6 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import React from 'react'; +import { renderToStaticMarkup } from 'react-dom/server'; import BlogPostPage, { generateMetadata } from './page'; vi.mock('../../../lib/seo', () => ({ @@ -15,7 +16,9 @@ vi.mock('../../../lib/seo', () => ({ })); vi.mock('../../../components/BlogPostClient', () => ({ - default: () =>
, + default: ({ authorName, authorTeam }: { authorName: string; authorTeam: string }) => ( +
نویسنده: {authorName} تیم: {authorTeam}
+ ), })); describe('BlogPostPage FAQPage Schema injection (SEO-P1-07)', () => { @@ -98,6 +101,30 @@ describe('BlogPostPage FAQPage Schema injection (SEO-P1-07)', () => { }); }); + it('uses a custom authorTeam as the authorless Organization identity in metadata and rendering', async () => { + const organizationName = 'تیم دامپزشکی کنینا'; + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => ({ + slug: 'custom-team', + title: 'Custom team article', + content: '

x

', + publishedAt: '2026-01-01T00:00:00Z', + author: null, + authorTeam: organizationName, + }) })); + + const metadata = await generateMetadata({ params: Promise.resolve({ slug: 'custom-team' }) }); + expect((metadata.openGraph as any)?.authors).toEqual([organizationName]); + + const jsx = await BlogPostPage({ params: Promise.resolve({ slug: 'custom-team' }) }); + const children = React.Children.toArray(jsx.props.children) as any[]; + const article = JSON.parse(children[0].props.dangerouslySetInnerHTML.__html); + expect(article.author).toMatchObject({ '@type': 'Organization', name: organizationName }); + + const html = renderToStaticMarkup(jsx); + expect(html).toContain(`نویسنده: ${organizationName}`); + expect(html).toContain(`تیم: ${organizationName}`); + }); + it('does not inject FAQPage schema when article does not have FAQs', async () => { const blogData = { slug: 'nutrition', diff --git a/frontend/application/app/blog/[slug]/page.tsx b/frontend/application/app/blog/[slug]/page.tsx index 2877c415..98c9f22d 100644 --- a/frontend/application/app/blog/[slug]/page.tsx +++ b/frontend/application/app/blog/[slug]/page.tsx @@ -1,4 +1,5 @@ -import { BLOG_TEAM_FALLBACK, validContentUpdatedAt } from "../../../lib/blogDates"; +import { validContentUpdatedAt } from "../../../lib/blogDates"; +import { resolveBlogAuthorIdentity } from "../../../lib/blogAuthor"; import React from "react"; import { notFound } from 'next/navigation'; import type { Metadata } from 'next'; @@ -91,10 +92,7 @@ export async function generateMetadata({ params }: { params: Promise<{ slug: str siteUrl.includes('test') || siteUrl.includes('dev'); - const personAuthorName = blog.author - ? `${blog.author.firstName || ''} ${blog.author.lastName || ''}`.trim() - : ''; - const authorName = personAuthorName || BLOG_TEAM_FALLBACK; + const resolvedAuthor = resolveBlogAuthorIdentity(blog); const publishedTime = safeIso(blog.publishedAt) || safeIso(blog.createdAt); const modifiedTime = safeIso(validContentUpdatedAt(blog)); @@ -150,7 +148,7 @@ export async function generateMetadata({ params }: { params: Promise<{ slug: str type: "article", publishedTime, ...(modifiedTime ? { modifiedTime } : {}), - authors: [authorName], + authors: [resolvedAuthor.name], tags: blog.tags?.map((t: any) => typeof t === 'string' ? t : t?.name).filter(Boolean) || [], }, twitter: { @@ -174,10 +172,7 @@ export default async function BlogPostPage({ params }: { params: Promise<{ slug: const rawSiteUrl = process.env.NEXT_PUBLIC_SITE_URL || config.canonicalBaseUrl || 'https://canina.ir'; const siteUrl = rawSiteUrl.replace(/\/+$/, ''); - const personAuthorName = blog.author - ? `${blog.author.firstName || ''} ${blog.author.lastName || ''}`.trim() - : ''; - const authorName = personAuthorName || BLOG_TEAM_FALLBACK; + const resolvedAuthor = resolveBlogAuthorIdentity(blog); const publishedDate = safeLocalDate(blog.publishedAt) || safeLocalDate(blog.createdAt); const canonicalUrl = blog.canonicalUrl @@ -190,14 +185,14 @@ export default async function BlogPostPage({ params }: { params: Promise<{ slug: const sanitizedContent = sanitizeBlogHtml(blog.content); const schemaType = blog.schemaType || 'BlogPosting'; - const authorSchema = blog.author && (blog.author.firstName || blog.author.lastName) + const authorSchema = resolvedAuthor.type === 'Person' ? { '@type': 'Person', - name: authorName, + name: resolvedAuthor.name, } : { '@type': 'Organization', - name: blog.authorTeam || BLOG_TEAM_FALLBACK, + name: resolvedAuthor.name, url: siteUrl, }; @@ -294,7 +289,8 @@ export default async function BlogPostPage({ params }: { params: Promise<{ slug: diff --git a/frontend/application/components/BlogPostClient.tsx b/frontend/application/components/BlogPostClient.tsx index a6622d3d..6fbe803b 100644 --- a/frontend/application/components/BlogPostClient.tsx +++ b/frontend/application/components/BlogPostClient.tsx @@ -26,10 +26,11 @@ interface BlogPostClientProps { blog: any; publishedDate: string; authorName: string; + authorTeam?: string; sanitizedContent: string; } -export default function BlogPostClient({ blog, publishedDate, authorName, sanitizedContent }: BlogPostClientProps) { +export default function BlogPostClient({ blog, publishedDate, authorName, authorTeam, sanitizedContent }: BlogPostClientProps) { const [copied, setCopied] = useState(false); const [viewCount, setViewCount] = useState(blog.viewCount || 0); const [commentName, setCommentName] = useState(""); @@ -211,7 +212,7 @@ export default function BlogPostClient({ blog, publishedDate, authorName, saniti
نویسنده: {authorName}
- تیم: {blog.authorTeam || BLOG_TEAM_FALLBACK} + تیم: {authorTeam || blog.authorTeam || BLOG_TEAM_FALLBACK}
diff --git a/frontend/application/lib/blogAuthor.ts b/frontend/application/lib/blogAuthor.ts index 9536b0d3..c485580c 100644 --- a/frontend/application/lib/blogAuthor.ts +++ b/frontend/application/lib/blogAuthor.ts @@ -1,13 +1,37 @@ -export const DEFAULT_BLOG_AUTHOR = "تیم پژوهش و محتوای علمی کنینا"; +import { BLOG_TEAM_FALLBACK } from './blogDates'; type BlogAuthor = { firstName?: string | null; lastName?: string | null; }; +interface BlogAuthorInput { + author?: BlogAuthor | null; + authorTeam?: string | null; +} + +export interface ResolvedBlogAuthor { + type: 'Person' | 'Organization'; + name: string; + teamName: string; +} + export function resolveBlogAuthor(author?: BlogAuthor | null): string { const name = author - ? `${author.firstName || ""} ${author.lastName || ""}`.trim() - : ""; - return name || DEFAULT_BLOG_AUTHOR; + ? `${author.firstName || ''} ${author.lastName || ''}`.trim() + : ''; + return name || BLOG_TEAM_FALLBACK; +} + +export function resolveBlogAuthorIdentity(blog: BlogAuthorInput): ResolvedBlogAuthor { + const personName = blog.author + ? `${blog.author.firstName || ''} ${blog.author.lastName || ''}`.trim() + : ''; + const teamName = blog.authorTeam?.trim() || BLOG_TEAM_FALLBACK; + + if (personName) { + return { type: 'Person', name: personName, teamName }; + } + + return { type: 'Organization', name: teamName, teamName }; }