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 }) => ( +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: