fix(seo): normalize and safely encode wiki route slug to prevent double-encoding 404s
This commit is contained in:
1 parent
721118787a
commit
e34156b2d3
4 files changed
+291
-10
No files matched your search
@@ -6,15 +6,23 @@ import sanitizeHtml from 'sanitize-html';
|
||||
import { getSeoConfig, formatPageTitle } from "../../../lib/seo";
|
||||
import { generateMedicalWebPageSchema, generateBreadcrumbSchema, safeJsonLd } from "../../../lib/schema";
|
||||
import SafeImage from "../../../components/SafeImage";
|
||||
import { normalizeRouteSlug, safeEncodeSlug } from "../../../lib/slug";
|
||||
|
||||
const API_URL = process.env.INTERNAL_API_URL || process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4001/api';
|
||||
const apiBase = API_URL.endsWith('/api') ? API_URL : `${API_URL.replace(/\/$/, '')}/api`;
|
||||
function getApiBase(): string {
|
||||
const apiUrl = process.env.INTERNAL_API_URL || process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4001/api';
|
||||
return apiUrl.endsWith('/api') ? apiUrl : `${apiUrl.replace(/\/$/, '')}/api`;
|
||||
}
|
||||
|
||||
async function getWikiTerm(rawKey: string) {
|
||||
const normalizedKey = normalizeRouteSlug(rawKey);
|
||||
if (!normalizedKey) return null;
|
||||
const encodedKey = safeEncodeSlug(normalizedKey);
|
||||
const apiBase = getApiBase();
|
||||
|
||||
async function getWikiTerm(key: string) {
|
||||
try {
|
||||
// 1. Try scientific terms endpoint
|
||||
const res = await fetch(`${apiBase}/wiki/${encodeURIComponent(key)}`, {
|
||||
next: { revalidate: 3600, tags: ['wiki', `wiki-${key}`] }
|
||||
const res = await fetch(`${apiBase}/wiki/${encodedKey}`, {
|
||||
next: { revalidate: 3600, tags: ['wiki', `wiki-${encodedKey}`] }
|
||||
});
|
||||
if (res.ok) {
|
||||
return await res.json();
|
||||
@@ -25,8 +33,8 @@ async function getWikiTerm(key: string) {
|
||||
|
||||
try {
|
||||
// 2. Fallback to ingredients endpoint if slug is an ingredient slug
|
||||
const ingRes = await fetch(`${apiBase}/ingredients/${encodeURIComponent(key)}`, {
|
||||
next: { revalidate: 3600, tags: ['ingredients', `ing-${key}`] }
|
||||
const ingRes = await fetch(`${apiBase}/ingredients/${encodedKey}`, {
|
||||
next: { revalidate: 3600, tags: ['ingredients', `ing-${encodedKey}`] }
|
||||
});
|
||||
if (ingRes.ok) {
|
||||
const ing = await ingRes.json();
|
||||
@@ -48,6 +56,7 @@ async function getWikiTerm(key: string) {
|
||||
|
||||
async function getRelatedProducts(termName: string) {
|
||||
try {
|
||||
const apiBase = getApiBase();
|
||||
const res = await fetch(`${apiBase}/products?search=${encodeURIComponent(termName)}&limit=4`, {
|
||||
next: { revalidate: 3600, tags: ['products'] },
|
||||
});
|
||||
@@ -62,6 +71,7 @@ async function getRelatedProducts(termName: string) {
|
||||
|
||||
export async function generateMetadata({ params }: { params: Promise<{ slug: string }> }): Promise<Metadata> {
|
||||
const { slug } = await params;
|
||||
const normalizedKey = normalizeRouteSlug(slug);
|
||||
const term = await getWikiTerm(slug);
|
||||
const config = await getSeoConfig();
|
||||
|
||||
@@ -77,7 +87,7 @@ export async function generateMetadata({ params }: { params: Promise<{ slug: str
|
||||
term.metaDescription ||
|
||||
(term.definition ? term.definition.replace(/<[^>]+>/g, '').substring(0, 155).trim() : '') ||
|
||||
`بررسی خواص بالینی، فواید و مکملهای دارویی حاوی ${term.term} در دامپزشکی کنینا آلمان.`;
|
||||
const canonicalUrl = `${config.canonicalBaseUrl.replace(/\/$/, '')}/wiki/${slug}`;
|
||||
const canonicalUrl = `${config.canonicalBaseUrl.replace(/\/$/, '')}/wiki/${safeEncodeSlug(normalizedKey)}`;
|
||||
|
||||
return {
|
||||
title,
|
||||
@@ -102,6 +112,7 @@ export async function generateMetadata({ params }: { params: Promise<{ slug: str
|
||||
|
||||
export default async function WikiTermPage({ params }: { params: Promise<{ slug: string }> }) {
|
||||
const { slug } = await params;
|
||||
const normalizedKey = normalizeRouteSlug(slug);
|
||||
const term = await getWikiTerm(slug);
|
||||
|
||||
if (!term) {
|
||||
@@ -109,18 +120,19 @@ export default async function WikiTermPage({ params }: { params: Promise<{ slug:
|
||||
}
|
||||
|
||||
const relatedProducts = await getRelatedProducts(term.term);
|
||||
const encodedSlug = safeEncodeSlug(normalizedKey);
|
||||
|
||||
const medicalSchema = generateMedicalWebPageSchema({
|
||||
title: `ترکیب درمانی ${term.term} - دانشنامه تخصصی کنینا`,
|
||||
description: term.metaDescription || (term.definition ? term.definition.replace(/<[^>]+>/g, '').substring(0, 160) : ''),
|
||||
url: `https://canina.ir/wiki/${slug}`,
|
||||
url: `https://canina.ir/wiki/${encodedSlug}`,
|
||||
aspects: [term.term, 'Veterinary Supplements', 'Pet Nutrition'],
|
||||
});
|
||||
|
||||
const breadcrumbs = generateBreadcrumbSchema([
|
||||
{ name: 'صفحه اصلی', url: 'https://canina.ir' },
|
||||
{ name: 'دانشنامه ترکیبات', url: 'https://canina.ir/wiki' },
|
||||
{ name: term.term, url: `https://canina.ir/wiki/${slug}` },
|
||||
{ name: term.term, url: `https://canina.ir/wiki/${encodedSlug}` },
|
||||
]);
|
||||
|
||||
return (
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import React from 'react';
|
||||
import WikiTermPage, { generateMetadata } 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} | کنینا ایران`),
|
||||
}));
|
||||
|
||||
const mockNotFound = vi.fn();
|
||||
vi.mock('next/navigation', () => ({
|
||||
notFound: () => {
|
||||
mockNotFound();
|
||||
throw new Error('NEXT_NOT_FOUND');
|
||||
},
|
||||
}));
|
||||
|
||||
describe('WikiTermPage slug decoding and single-encoding', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.stubEnv('INTERNAL_API_URL', 'http://backend:3000/api');
|
||||
vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'https://canina.ir');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
it('correctly fetches Persian term when slug is passed URL-encoded (آغوز)', async () => {
|
||||
const fetchMock = vi.fn()
|
||||
// First call: /wiki/%D8%A2%D8%BA%D9%88%D8%B2
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
key: 'آغوز',
|
||||
term: 'آغوز (Colostrum)',
|
||||
definition: 'اولین شیر پستانداران پس از زایمان.',
|
||||
wikiId: 'colostrum',
|
||||
}),
|
||||
})
|
||||
// Second call: /products?search=...
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({ data: [] }),
|
||||
});
|
||||
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
// Incoming params from Next router for /wiki/%D8%A2%D8%BA%D9%88%D8%B2
|
||||
const jsx = await WikiTermPage({
|
||||
params: Promise.resolve({ slug: '%D8%A2%D8%BA%D9%88%D8%B2' }),
|
||||
});
|
||||
|
||||
expect(jsx).toBeDefined();
|
||||
expect(fetchMock).toHaveBeenCalled();
|
||||
const firstCallUrl = fetchMock.mock.calls[0][0];
|
||||
// Must NOT be double-encoded %25D8%25A2...
|
||||
expect(firstCallUrl).toBe('http://backend:3000/api/wiki/%D8%A2%D8%BA%D9%88%D8%B2');
|
||||
expect(firstCallUrl).not.toContain('%25');
|
||||
});
|
||||
|
||||
it('correctly generates metadata with single-encoded canonical for Persian slug', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
key: 'آغوز',
|
||||
term: 'آغوز (Colostrum)',
|
||||
definition: 'اولین شیر پستانداران پس از زایمان.',
|
||||
wikiId: 'colostrum',
|
||||
}),
|
||||
});
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const meta = await generateMetadata({
|
||||
params: Promise.resolve({ slug: '%D8%A2%D8%BA%D9%88%D8%B2' }),
|
||||
});
|
||||
|
||||
expect(meta.alternates?.canonical).toBe('https://canina.ir/wiki/%D8%A2%D8%BA%D9%88%D8%B2');
|
||||
expect(meta.title).toBe('ترکیب دارویی و خواص آغوز (Colostrum) | کنینا ایران');
|
||||
});
|
||||
|
||||
it('handles EPA/DHA without turning into malformed path or double encoding', async () => {
|
||||
const fetchMock = vi.fn()
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
key: 'EPA/DHA',
|
||||
term: 'EPA/DHA',
|
||||
definition: 'اسیدهای چرب امگا ۳.',
|
||||
wikiId: 'epa-dha',
|
||||
}),
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({ data: [] }),
|
||||
});
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const jsx = await WikiTermPage({
|
||||
params: Promise.resolve({ slug: 'EPA%2FDHA' }),
|
||||
});
|
||||
expect(jsx).toBeDefined();
|
||||
expect(fetchMock.mock.calls[0][0]).toBe('http://backend:3000/api/wiki/EPA%2FDHA');
|
||||
});
|
||||
|
||||
it('returns notFound() when neither scientific term nor ingredient exists', async () => {
|
||||
const fetchMock = vi.fn()
|
||||
// 1. wiki term 404
|
||||
.mockResolvedValueOnce({ ok: false, status: 404 })
|
||||
// 2. ingredient fallback 404
|
||||
.mockResolvedValueOnce({ ok: false, status: 404 });
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
await expect(
|
||||
WikiTermPage({ params: Promise.resolve({ slug: 'non-existent' }) })
|
||||
).rejects.toThrow('NEXT_NOT_FOUND');
|
||||
expect(mockNotFound).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user