fix(blog): harden freshness and authorship

This commit is contained in:
devops committed 2026-10-20 06:31:22 +00:00
1 parent 51ca8c7e34
commit 0b4d9a8463
11 files changed
+157 -26

No files matched your search

@@ -1,6 +1,6 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import React from 'react';
import BlogPostPage from './page';
import BlogPostPage, { generateMetadata } from './page';
vi.mock('../../../lib/seo', () => ({
getSeoConfig: vi.fn().mockResolvedValue({
@@ -79,6 +79,25 @@ describe('BlogPostPage FAQPage Schema injection (SEO-P1-07)', () => {
const jsx=await BlogPostPage({params:Promise.resolve({slug:'fresh'})}); const script:any=React.Children.toArray(jsx.props.children)[0]; const json=JSON.parse(script.props.dangerouslySetInnerHTML.__html); expect(json.dateModified).toBeUndefined(); expect(json.author['@type']).toBe('Organization'); expect(json.author.name).toBe('تیم پژوهش و محتوای علمی کنینا');
});
it('uses the stable Organization fallback visibly and in metadata', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => ({
slug: 'authorless', title: 'Authorless', content: '<p>x</p>', publishedAt: '2026-01-01T00:00:00Z', author: { firstName: '', lastName: '' },
}) }));
const metadata = await generateMetadata({ params: Promise.resolve({ slug: 'authorless' }) });
expect((metadata.openGraph as any)?.authors).toEqual(['تیم پژوهش و محتوای علمی کنینا']);
const jsx = await BlogPostPage({ params: Promise.resolve({ slug: 'authorless' }) });
const children = React.Children.toArray(jsx.props.children) as any[];
const client = children.find((child) => child?.props?.authorName);
expect(client.props.authorName).toBe('تیم پژوهش و محتوای علمی کنینا');
const article = JSON.parse(children[0].props.dangerouslySetInnerHTML.__html);
expect(article.author).toMatchObject({
'@type': 'Organization',
name: 'تیم پژوهش و محتوای علمی کنینا',
});
});
it('does not inject FAQPage schema when article does not have FAQs', async () => {
const blogData = {
slug: 'nutrition',
@@ -91,9 +91,10 @@ export async function generateMetadata({ params }: { params: Promise<{ slug: str
siteUrl.includes('test') ||
siteUrl.includes('dev');
const authorName = blog.author
? `${blog.author.firstName || ''} ${blog.author.lastName || ''}`.trim() || 'نویسنده کنینا'
: 'نویسنده و پژوهشگر کنینا';
const personAuthorName = blog.author
? `${blog.author.firstName || ''} ${blog.author.lastName || ''}`.trim()
: '';
const authorName = personAuthorName || BLOG_TEAM_FALLBACK;
const publishedTime = safeIso(blog.publishedAt) || safeIso(blog.createdAt);
const modifiedTime = safeIso(validContentUpdatedAt(blog));
@@ -173,9 +174,10 @@ 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 authorName = blog.author
? `${blog.author.firstName || ''} ${blog.author.lastName || ''}`.trim() || 'نویسنده کنینا'
: 'نویسنده و پژوهشگر کنینا';
const personAuthorName = blog.author
? `${blog.author.firstName || ''} ${blog.author.lastName || ''}`.trim()
: '';
const authorName = personAuthorName || BLOG_TEAM_FALLBACK;
const publishedDate = safeLocalDate(blog.publishedAt) || safeLocalDate(blog.createdAt);
const canonicalUrl = blog.canonicalUrl
@@ -1,4 +1,4 @@
import { afterEach,describe,expect,it,vi } from "vitest";
import { GET } from "./route";
afterEach(()=>{vi.unstubAllGlobals();vi.unstubAllEnvs()});
describe("blog RSS dates",()=>{it("keeps pubDate immutable and derives lastBuildDate from real valid dates",async()=>{vi.stubEnv("NEXT_PUBLIC_SITE_URL","https://canina.ir");vi.stubGlobal("fetch",vi.fn().mockResolvedValue({ok:true,json:async()=>({data:[{slug:"a",title:"A",status:"PUBLISHED",publishedAt:"2026-01-01T00:00:00Z",contentUpdatedAt:"2026-01-03T00:00:00Z",updatedAt:"2026-01-04T00:00:00Z"}]})}));const xml=await (await GET()).text();expect(xml).toContain(`<pubDate>${new Date("2026-01-01T00:00:00Z").toUTCString()}</pubDate>`);expect(xml).toContain(`<lastBuildDate>${new Date("2026-01-03T00:00:00Z").toUTCString()}</lastBuildDate>`);});});
describe("blog RSS dates",()=>{it("keeps pubDate immutable and derives lastBuildDate from real valid dates",async()=>{vi.stubEnv("NEXT_PUBLIC_SITE_URL","https://canina.ir");vi.stubGlobal("fetch",vi.fn().mockResolvedValue({ok:true,json:async()=>({data:[{slug:"a",title:"A",status:"PUBLISHED",publishedAt:"2026-01-01T00:00:00Z",contentUpdatedAt:"2026-01-03T00:00:00Z",updatedAt:"2026-01-04T00:00:00Z"}]})}));const xml=await (await GET()).text();expect(xml).toContain(`<pubDate>${new Date("2026-01-01T00:00:00Z").toUTCString()}</pubDate>`);expect(xml).toContain(`<lastBuildDate>${new Date("2026-01-03T00:00:00Z").toUTCString()}</lastBuildDate>`);});it("uses the stable fallback author for authorless posts",async()=>{vi.stubGlobal("fetch",vi.fn().mockResolvedValue({ok:true,json:async()=>({data:[{slug:"a",title:"A",status:"PUBLISHED",publishedAt:"2026-01-01T00:00:00Z",author:{firstName:"",lastName:""}}]})}));const xml=await (await GET()).text();expect(xml).toContain("<author>تیم پژوهش و محتوای علمی کنینا</author>");});});
@@ -1,4 +1,4 @@
import { validContentUpdatedAt, publicationDate } from '../../../lib/blogDates';
import { BLOG_TEAM_FALLBACK, validContentUpdatedAt, publicationDate } from '../../../lib/blogDates';
import { NextResponse } from 'next/server';
export const dynamic = 'force-dynamic';
@@ -25,7 +25,8 @@ export async function GET() {
const link = `${baseUrl}/blog/${b.slug}`;
const description = escapeXml(b.excerpt || b.metaDescription || '');
const pubDate = new Date(publicationDate(b)!).toUTCString();
const author = escapeXml(b.author ? `${b.author.firstName || ''} ${b.author.lastName || ''}`.trim() : 'کنینا فارما');
const personName = b.author ? `${b.author.firstName || ''} ${b.author.lastName || ''}`.trim() : '';
const author = escapeXml(personName || BLOG_TEAM_FALLBACK);
const category = escapeXml(b.category?.name || 'دانشنامه سلامت حیوانات');
return `