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();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,85 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { normalizeRouteSlug, safeEncodeSlug } from './slug';
|
||||
|
||||
describe('normalizeRouteSlug', () => {
|
||||
it('normalizes single-encoded Persian slug to plain decoded string', () => {
|
||||
// آغوز encoded once
|
||||
const input = '%D8%A2%D8%BA%D9%88%D8%B2';
|
||||
expect(normalizeRouteSlug(input)).toBe('آغوز');
|
||||
});
|
||||
|
||||
it('normalizes already-decoded Persian slug without changes', () => {
|
||||
const input = 'آغوز';
|
||||
expect(normalizeRouteSlug(input)).toBe('آغوز');
|
||||
});
|
||||
|
||||
it('normalizes Persian spaced slug', () => {
|
||||
// آب پنیر
|
||||
expect(normalizeRouteSlug('%D8%A2%D8%A8%20%D9%BE%D9%86%DB%8C%D8%B1')).toBe('آب پنیر');
|
||||
expect(normalizeRouteSlug('آب پنیر')).toBe('آب پنیر');
|
||||
});
|
||||
|
||||
it('handles ASCII slugs safely', () => {
|
||||
expect(normalizeRouteSlug('HACCP')).toBe('HACCP');
|
||||
expect(normalizeRouteSlug('IFS')).toBe('IFS');
|
||||
expect(normalizeRouteSlug('BARF')).toBe('BARF');
|
||||
});
|
||||
|
||||
it('handles slash in slug such as EPA/DHA without turning into multiple path segments or breaking', () => {
|
||||
expect(normalizeRouteSlug('EPA%2FDHA')).toBe('EPA/DHA');
|
||||
expect(normalizeRouteSlug('EPA/DHA')).toBe('EPA/DHA');
|
||||
});
|
||||
|
||||
it('handles double-encoded slug safely without double-decoding legit single %', () => {
|
||||
// If incoming slug was double-encoded: %25D8%25A2%25D8%25BA%25D9%2588%25D8%25B2
|
||||
expect(normalizeRouteSlug('%25D8%25A2%25D8%25BA%25D9%2588%25D8%25B2')).toBe('آغوز');
|
||||
});
|
||||
|
||||
it('handles malformed percent encoding gracefully without throwing URIError', () => {
|
||||
expect(normalizeRouteSlug('100%pure')).toBe('100%pure');
|
||||
expect(normalizeRouteSlug('%E0%A4%A')).toBe('%E0%A4%A');
|
||||
expect(normalizeRouteSlug('%')).toBe('%');
|
||||
expect(normalizeRouteSlug('test%8')).toBe('test%8');
|
||||
});
|
||||
|
||||
it('handles Ca:P ratio slug', () => {
|
||||
// نسبت Ca:P
|
||||
expect(normalizeRouteSlug('%D9%86%D8%B3%D8%A8%D8%AA%20Ca%3AP')).toBe('نسبت Ca:P');
|
||||
});
|
||||
});
|
||||
|
||||
describe('safeEncodeSlug', () => {
|
||||
it('encodes slug exactly once for API and URL consumption', () => {
|
||||
expect(safeEncodeSlug('آغوز')).toBe('%D8%A2%D8%BA%D9%88%D8%B2');
|
||||
expect(safeEncodeSlug('HACCP')).toBe('HACCP');
|
||||
expect(safeEncodeSlug('EPA/DHA')).toBe('EPA%2FDHA');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Edge cases: legitimate single % not double-decoded', () => {
|
||||
it('preserves slug with 100% discount or pure keyword without destroying %', () => {
|
||||
// A slug that literally has "100%pure" (malformed percent)
|
||||
expect(normalizeRouteSlug('100%pure')).toBe('100%pure');
|
||||
// If someone literally passed 100%25pure (100%pure encoded)
|
||||
expect(normalizeRouteSlug('100%25pure')).toBe('100%pure');
|
||||
});
|
||||
|
||||
it('handles all sitemap sample slugs cleanly', () => {
|
||||
const samples = [
|
||||
'%D8%A2%D8%BA%D9%88%D8%B2',
|
||||
'%D8%A2%D8%A8%20%D9%BE%D9%86%DB%8C%D8%B1',
|
||||
'HACCP',
|
||||
'IFS',
|
||||
'BARF',
|
||||
'EPA%2FDHA',
|
||||
'%D9%86%D8%B3%D8%A8%D8%AA%20Ca%3AP',
|
||||
'%D8%A8%DB%8C%D9%88%D8%AA%DB%8C%D9%86',
|
||||
];
|
||||
for (const sample of samples) {
|
||||
const normalized = normalizeRouteSlug(sample);
|
||||
expect(normalized).toBeTruthy();
|
||||
const reencoded = safeEncodeSlug(normalized);
|
||||
expect(normalizeRouteSlug(reencoded)).toBe(normalized);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Safe slug normalization and encoding for SEO routes.
|
||||
*
|
||||
* Handles:
|
||||
* - Single-encoded Persian/UTF-8 (%D8%A2%D8%BA%D9%88%D8%B2 -> آغوز)
|
||||
* - Double-encoded slugs (%25D8%25A2... -> آغوز)
|
||||
* - Raw Unicode (آغوز -> آغوز)
|
||||
* - Slugs with spaces or encoded spaces (آب پنیر / %D8%A2%D8%A8%20%D9%BE%D9%86%DB%8C%D8%B1)
|
||||
* - Slashes like EPA/DHA (EPA%2FDHA -> EPA/DHA)
|
||||
* - Malformed percent sequences without throwing URIError (e.g. 100%pure, %E0%A4%A)
|
||||
* - Safe single-encoding for outgoing API requests and canonical URLs.
|
||||
*/
|
||||
|
||||
function safeDecode(val: string): string {
|
||||
try {
|
||||
return decodeURIComponent(val);
|
||||
} catch {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes an incoming route parameter slug to a clean, decoded UTF-8 string.
|
||||
* Decodes iteratively (up to 2 passes) if double-encoded, but does not alter strings
|
||||
* where percent is not a valid percent-encoding sequence.
|
||||
*/
|
||||
export function normalizeRouteSlug(rawSlug: string): string {
|
||||
if (!rawSlug) return '';
|
||||
|
||||
let current = String(rawSlug).trim();
|
||||
|
||||
// If input contains '%', attempt safe decoding
|
||||
if (current.includes('%')) {
|
||||
const decodedOnce = safeDecode(current);
|
||||
if (decodedOnce !== current) {
|
||||
// Check if it was double-encoded (e.g. %25D8... -> %D8... -> Persian)
|
||||
if (decodedOnce.includes('%')) {
|
||||
const decodedTwice = safeDecode(decodedOnce);
|
||||
// Only accept second decode if it produced a valid string that differs
|
||||
if (decodedTwice !== decodedOnce) {
|
||||
return decodedTwice;
|
||||
}
|
||||
}
|
||||
return decodedOnce;
|
||||
}
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes a normalized slug safely for outgoing HTTP query/path or canonical URL.
|
||||
*/
|
||||
export function safeEncodeSlug(slug: string): string {
|
||||
if (!slug) return '';
|
||||
return encodeURIComponent(slug);
|
||||
}
|
||||
Reference in new issue
Block a user