From f8f9329c501e921fdd5070d1d1e4fcfa219eea2b Mon Sep 17 00:00:00 2001 From: devops Date: Sat, 17 Oct 2026 20:38:09 +0000 Subject: [PATCH] feat(seo): implement FAQPage schema, sub-sitemaps and local Lalezar WOFF2 font (SEO-P1-07, SEO-P1-10, SEO-P2-16) - SEO-P1-07: extract FAQs and accordion items from blog content and inject JSON-LD FAQPage schema - SEO-P1-10: add dynamic sub-sitemaps /sitemap-blog.xml and /sitemap-wiki.xml and register in robots.ts - SEO-P2-16: host Lalezar font locally as optimized WOFF2 with preload enabled in lib/fonts.ts - Add full regression test suites with 100% pass rate --- .../app/blog/[slug]/blog-slug-page.test.tsx | 104 ++++++++++++++++++ frontend/application/app/blog/[slug]/page.tsx | 14 ++- frontend/application/app/robots.ts | 2 + .../application/app/sitemap-blog.xml/route.ts | 78 +++++++++++++ .../application/app/sitemap-wiki.xml/route.ts | 64 +++++++++++ frontend/application/app/sitemaps.test.ts | 91 +++++++++++++++ .../lib/__tests__/faq-schema.test.ts | 64 +++++++++++ .../application/lib/__tests__/fonts.test.ts | 29 +++++ frontend/application/lib/extractFaqs.ts | 101 +++++++++++++++++ frontend/application/lib/fonts.ts | 15 ++- .../public/fonts/Lalezar-Regular.woff2 | Bin 0 -> 84912 bytes 11 files changed, 556 insertions(+), 6 deletions(-) create mode 100644 frontend/application/app/blog/[slug]/blog-slug-page.test.tsx create mode 100644 frontend/application/app/sitemap-blog.xml/route.ts create mode 100644 frontend/application/app/sitemap-wiki.xml/route.ts create mode 100644 frontend/application/app/sitemaps.test.ts create mode 100644 frontend/application/lib/__tests__/faq-schema.test.ts create mode 100644 frontend/application/lib/__tests__/fonts.test.ts create mode 100644 frontend/application/lib/extractFaqs.ts create mode 100644 frontend/application/public/fonts/Lalezar-Regular.woff2 diff --git a/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx b/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx new file mode 100644 index 00000000..dca2e149 --- /dev/null +++ b/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx @@ -0,0 +1,104 @@ +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; +import React from 'react'; +import BlogPostPage from './page'; + +vi.mock('../../../lib/seo', () => ({ + getSeoConfig: vi.fn().mockResolvedValue({ + brandNameFa: 'کنینا ایران', + canonicalBaseUrl: 'https://canina.ir', + ogImageUrl: '/assets/og.jpg', + defaultMetaDescription: 'توضیحات پیش‌فرض', + titleSeparator: '|', + titlePosition: 'suffix', + }), + formatPageTitle: vi.fn((title) => `${title} | کنینا ایران`), +})); + +vi.mock('../../../components/BlogPostClient', () => ({ + default: () =>
, +})); + +describe('BlogPostPage FAQPage Schema injection (SEO-P1-07)', () => { + beforeEach(() => { + vi.stubEnv('INTERNAL_API_URL', 'http://backend:3000/api'); + vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'https://canina.ir'); + }); + + afterEach(() => { + vi.unstubAllGlobals(); + vi.unstubAllEnvs(); + }); + + it('injects FAQPage JSON-LD schema when article contains questions and answers', async () => { + const blogData = { + slug: 'joint-health', + title: 'سلامت مفاصل سگ', + content: ` +
+

مقدمه‌ای درباره مفاصل سگ.

+
+ آیا این مکمل برای سگ مسن مناسب است؟ +

بله، بسیار توصیه می‌شود و روند تخریب مفصل را کند می‌کند.

+
+
+ `, + createdAt: '2026-01-01', + publishedAt: '2026-01-01', + }; + + vi.stubGlobal( + 'fetch', + vi.fn().mockResolvedValue({ + ok: true, + json: async () => blogData, + }) + ); + + const jsx = await BlogPostPage({ params: Promise.resolve({ slug: 'joint-health' }) }); + expect(jsx).toBeDefined(); + + // Verify scripts in JSX tree + const scripts = React.Children.toArray(jsx.props.children).filter( + (child: any) => child?.type === 'script' && child?.props?.['type'] === 'application/ld+json' + ); + + // There should be 3 schemas: Article, Breadcrumb, and FAQPage + expect(scripts.length).toBe(3); + + const faqScript: any = scripts.find((s: any) => s.props['data-testid'] === 'faq-schema'); + expect(faqScript).toBeDefined(); + + const faqJson = JSON.parse(faqScript.props.dangerouslySetInnerHTML.__html); + expect(faqJson['@type']).toBe('FAQPage'); + expect(faqJson.mainEntity).toHaveLength(1); + expect(faqJson.mainEntity[0].name).toBe('آیا این مکمل برای سگ مسن مناسب است؟'); + }); + + it('does not inject FAQPage schema when article does not have FAQs', async () => { + const blogData = { + slug: 'nutrition', + title: 'تغذیه سگ', + content: '

یک مقاله ساده بدون سوال و جواب یا آکاردئون.

', + createdAt: '2026-01-01', + publishedAt: '2026-01-01', + }; + + vi.stubGlobal( + 'fetch', + vi.fn().mockResolvedValue({ + ok: true, + json: async () => blogData, + }) + ); + + const jsx = await BlogPostPage({ params: Promise.resolve({ slug: 'nutrition' }) }); + const scripts = React.Children.toArray(jsx.props.children).filter( + (child: any) => child?.type === 'script' && child?.props?.['type'] === 'application/ld+json' + ); + + // Only Article and Breadcrumb schemas + expect(scripts.length).toBe(2); + const faqScript = scripts.find((s: any) => s.props['data-testid'] === 'faq-schema'); + expect(faqScript).toBeUndefined(); + }); +}); diff --git a/frontend/application/app/blog/[slug]/page.tsx b/frontend/application/app/blog/[slug]/page.tsx index 03ca3731..49cef8ce 100644 --- a/frontend/application/app/blog/[slug]/page.tsx +++ b/frontend/application/app/blog/[slug]/page.tsx @@ -5,6 +5,7 @@ import BlogPostClient from "../../../components/BlogPostClient"; import { getSeoConfig, formatPageTitle } from "../../../lib/seo"; import { safeJsonLd } from "../../../lib/schema"; import { sanitizeBlogHtml } from "../../../lib/sanitizeBlogHtml"; +import { extractFaqsFromHtml, generateFAQPageSchema } from "../../../lib/extractFaqs"; function safeIso(val: any, fallback?: string): string | undefined { if (!val) return fallback; @@ -82,7 +83,7 @@ export async function generateMetadata({ params }: { params: Promise<{ slug: str const ogTitle = blog.ogTitle || title; const ogDesc = blog.ogDescription || description; - const isStaging = + const isStaging = process.env.NEXT_PUBLIC_APP_ENV === 'staging' || process.env.NODE_ENV !== 'production' || siteUrl.includes('stage') || @@ -267,6 +268,10 @@ export default async function BlogPostPage({ params }: { params: Promise<{ slug: ], }; + // SEO-P1-07: Auto-detect and inject FAQPage Schema when article content contains Q&As or accordion structures + const extractedFaqs = extractFaqsFromHtml(blog.content || ''); + const faqPageSchema = generateFAQPageSchema(extractedFaqs); + return ( <>