面包屑导航实现:Claude Code 实战指南
用 Claude Code 实现同时兼顾 UX 与 SEO 的面包屑导航,含结构化数据、可访问性和响应式样式示例。
用面包屑同时提升 UX 与 SEO
面包屑(Breadcrumb)是一种用于直观告诉用户当前所处位置的导航元素。只要实现得当,既能提升可用性又能改善 SEO,两者兼得。
基础面包屑组件
> 帮我写一个面包屑组件。
> 要支持结构化数据(JSON-LD),无障碍友好,并且适配移动端。
// components/Breadcrumb.tsx
interface BreadcrumbItem {
label: string;
href?: string;
}
interface BreadcrumbProps {
items: BreadcrumbItem[];
}
export function Breadcrumb({ items }: BreadcrumbProps) {
const jsonLd = {
'@context': 'https://schema.org',
'@type': 'BreadcrumbList',
itemListElement: items.map((item, index) => ({
'@type': 'ListItem',
position: index + 1,
name: item.label,
item: item.href ? `https://example.com${item.href}` : undefined,
})),
};
return (
<>
<script
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
/>
<nav aria-label="面包屑导航" className="breadcrumb">
<ol className="flex flex-wrap items-center gap-1 text-sm text-gray-600">
{items.map((item, index) => (
<li key={index} className="flex items-center">
{index > 0 && (
<span className="mx-2 text-gray-400" aria-hidden="true">/</span>
)}
{item.href && index < items.length - 1 ? (
<a
href={item.href}
className="text-blue-600 hover:underline"
>
{item.label}
</a>
) : (
<span aria-current="page" className="text-gray-800 font-medium">
{item.label}
</span>
)}
</li>
))}
</ol>
</nav>
</>
);
}
根据 URL 自动生成面包屑
// utils/breadcrumb.ts
const labelMap: Record<string, string> = {
blog: '博客',
category: '分类',
tags: '标签',
about: '关于',
};
export function generateBreadcrumbs(pathname: string): BreadcrumbItem[] {
const segments = pathname.split('/').filter(Boolean);
const items: BreadcrumbItem[] = [
{ label: '首页', href: '/' },
];
let currentPath = '';
segments.forEach((segment, index) => {
currentPath += `/${segment}`;
const isLast = index === segments.length - 1;
items.push({
label: labelMap[segment] || decodeURIComponent(segment),
href: isLast ? undefined : currentPath,
});
});
return items;
}
在 Astro 中集成
---
// layouts/BlogPost.astro
import { Breadcrumb } from '../components/Breadcrumb';
const { frontmatter } = Astro.props;
const breadcrumbItems = [
{ label: '首页', href: '/' },
{ label: '博客', href: '/blog' },
{ label: frontmatter.category, href: `/blog/category/${frontmatter.category}` },
{ label: frontmatter.title },
];
---
<Breadcrumb items={breadcrumbItems} />
样式变体
/* 箭头风格 */
.breadcrumb-arrow li + li::before {
content: '›';
margin: 0 0.5rem;
color: #9ca3af;
}
/* 图标风格 */
.breadcrumb-icon li:first-child a::before {
content: '';
display: inline-block;
width: 16px;
height: 16px;
background: url('/icons/home.svg') no-repeat center;
margin-right: 4px;
vertical-align: middle;
}
/* 移动端:只显示最后两级 */
@media (max-width: 640px) {
.breadcrumb li:not(:nth-last-child(-n+2)) {
display: none;
}
.breadcrumb li:nth-last-child(2)::before {
content: '... / ';
}
}
测试
import { describe, it, expect } from 'vitest';
import { generateBreadcrumbs } from './breadcrumb';
describe('generateBreadcrumbs', () => {
it('首页', () => {
const result = generateBreadcrumbs('/');
expect(result).toEqual([{ label: '首页', href: '/' }]);
});
it('博客文章页', () => {
const result = generateBreadcrumbs('/blog/my-article');
expect(result).toHaveLength(3);
expect(result[1]).toEqual({ label: '博客', href: '/blog' });
expect(result[2].href).toBeUndefined();
});
});
总结
面包屑导航是兼顾 SEO 优化 和 无障碍 的重要组件。让 Claude Code 一起把结构化数据也实现进去,Google 搜索结果就能正常显示面包屑。更多信息请参阅 Google 搜索中心。
#Claude Code
#面包屑
#navigation
#结构化数据
#UX
免费
免费 PDF:5 分钟看懂 Claude Code 速查表
只需留下邮箱,我们就会立即把这份 A4 一页速查表 PDF 发送给你。
我们会严格保护你的个人信息,绝不发送垃圾邮件。
把 Claude Code 变成真正能带来结果的工作流
先领取中文说明的免费 PDF,再进入英文商品页选择合适的教材。如果你需要团队落地、流程设计或内容变现支持,也可以直接咨询。
本文作者
Masa
深度使用 Claude Code 的工程师。运营 claudecode-lab.com——一个涵盖 10 种语言、超过 2,000 页内容的科技媒体。
相关文章
Tips & Tricks
Claude Code 的 7 个 CLAUDE.md 模板 | 可以直接复制到真实项目
面向个人应用、内容站、API、团队仓库和遗留代码库的 7 个实用 CLAUDE.md 模板,附常见失败案例。
Tips & Tricks
Claude Code Approval 与 Sandbox 指南 | 日常安全使用的实战设置
用可直接参考的 settings、hooks、失败案例与流程示例,讲清楚 Claude Code 的 allow / ask / deny / sandbox 应该怎么分。
Tips & Tricks
Claude Code 完全入门指南 2026 | 从零到实战应用的 7 个步骤
专为 Claude Code 新手打造的完整入门指南。从安装到融入真实开发工作流——涵盖 Masa 刚开始使用时踩过的所有坑。