fix(blog): unify structured author identity
Deploy Canina / deploy (push) Successful in 5m16s

This commit is contained in:
devops committed 2026-10-20 07:15:20 +00:00
1 parent 62c58c7c89
commit d670690816
4 files changed
+69 -21

No files matched your search

@@ -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: () => <div data-testid="blog-post-client" />,
default: ({ authorName, authorTeam }: { authorName: string; authorTeam: string }) => (
<div data-testid="blog-post-client">نویسنده: {authorName} تیم: {authorTeam}</div>
),
}));
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: '<p>x</p>',
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',
+10 -14
View File
@@ -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:
<BlogPostClient
blog={blog}
publishedDate={publishedDate}
authorName={authorName}
authorName={resolvedAuthor.name}
authorTeam={resolvedAuthor.teamName}
sanitizedContent={sanitizedContent}
/>
</>
@@ -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<number>(blog.viewCount || 0);
const [commentName, setCommentName] = useState("");
@@ -211,7 +212,7 @@ export default function BlogPostClient({ blog, publishedDate, authorName, saniti
<div>
<div className="font-black text-medical-gray-900 text-base">نویسنده: {authorName}</div>
<div className="font-bold text-xs text-medical-gray-400 mt-0.5">
تیم: {blog.authorTeam || BLOG_TEAM_FALLBACK}
تیم: {authorTeam || blog.authorTeam || BLOG_TEAM_FALLBACK}
</div>
</div>
</div>
+28 -4
View File
@@ -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 };
}