fix: use getTagSlug() for tag links to prevent empty tag pages
Tags containing both spaces and dashes (e.g. "Writings - 創作") produced mismatched slugs: inline generation created "writings---創作" while getTagSlug() collapsed dashes to "writings-創作", causing tag pages to show no articles. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,7 @@ import Image from 'next/image';
|
||||
import { notFound } from 'next/navigation';
|
||||
import type { Metadata } from 'next';
|
||||
import { allPosts } from 'contentlayer2/generated';
|
||||
import { getPostBySlug, getRelatedPosts, getPostNeighbors } from '@/lib/posts';
|
||||
import { getPostBySlug, getRelatedPosts, getPostNeighbors, getTagSlug } from '@/lib/posts';
|
||||
import { siteConfig } from '@/lib/config';
|
||||
import { ReadingProgress } from '@/components/reading-progress';
|
||||
import { ScrollReveal } from '@/components/scroll-reveal';
|
||||
@@ -217,9 +217,7 @@ export default async function BlogPostPage({ params }: Props) {
|
||||
{post.tags.map((t) => (
|
||||
<Link
|
||||
key={t}
|
||||
href={`/tags/${encodeURIComponent(
|
||||
t.toLowerCase().replace(/\s+/g, '-')
|
||||
)}`}
|
||||
href={`/tags/${encodeURIComponent(getTagSlug(t))}`}
|
||||
className="tag-chip rounded-full bg-accent-soft px-3 py-1 text-sm text-accent-textLight dark:bg-slate-800 dark:text-slate-100 dark:hover:bg-slate-700 dark:hover:text-white"
|
||||
>
|
||||
#{t}
|
||||
|
||||
@@ -3,7 +3,7 @@ import Image from 'next/image';
|
||||
import { notFound } from 'next/navigation';
|
||||
import type { Metadata } from 'next';
|
||||
import { allPages } from 'contentlayer2/generated';
|
||||
import { getPageBySlug } from '@/lib/posts';
|
||||
import { getPageBySlug, getTagSlug } from '@/lib/posts';
|
||||
import { siteConfig } from '@/lib/config';
|
||||
import { ReadingProgress } from '@/components/reading-progress';
|
||||
import { PostLayout } from '@/components/post-layout';
|
||||
@@ -130,9 +130,7 @@ export default async function StaticPage({ params }: Props) {
|
||||
{page.tags.map((t) => (
|
||||
<Link
|
||||
key={t}
|
||||
href={`/tags/${encodeURIComponent(
|
||||
t.toLowerCase().replace(/\s+/g, '-')
|
||||
)}`}
|
||||
href={`/tags/${encodeURIComponent(getTagSlug(t))}`}
|
||||
className="tag-chip rounded-full bg-accent-soft px-3 py-1 text-sm text-accent-textLight dark:bg-slate-800 dark:text-slate-100"
|
||||
>
|
||||
#{t}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { MetadataRoute } from 'next';
|
||||
import { allPosts, allPages } from 'contentlayer2/generated';
|
||||
import { getTagSlug } from '@/lib/posts';
|
||||
|
||||
export default function sitemap(): MetadataRoute.Sitemap {
|
||||
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000';
|
||||
@@ -58,7 +59,7 @@ export default function sitemap(): MetadataRoute.Sitemap {
|
||||
);
|
||||
|
||||
const tagPages = allTags.map((tag) => ({
|
||||
url: `${siteUrl}/tags/${encodeURIComponent(tag.toLowerCase().replace(/\s+/g, '-'))}`,
|
||||
url: `${siteUrl}/tags/${encodeURIComponent(getTagSlug(tag))}`,
|
||||
lastModified: new Date(),
|
||||
changeFrequency: 'weekly' as const,
|
||||
priority: 0.5,
|
||||
|
||||
Reference in New Issue
Block a user