SEO:Claude Code 实战指南
了解seo:Claude Code 实战. 包含实用技巧和代码示例。
サイトマップ自動生成でSEO対策を效率化
サイトマップはGoogleなどの搜索エンジンにサイト構造を伝える重要な文件です。页面が増えるたびに手動で更新するのは現実的ではありません。借助 Claude Code,构建時に自動生成される仕組みを短时间で构建可以。
基本的サイトマップ生成スクリプト
> XMLサイトマップを自動生成するスクリプト创建。
> 全页面のURLを収集し、lastmod・changefreq・priorityを配置して。
// scripts/generate-sitemap.ts
import { globby } from 'globby';
import { writeFileSync } from 'fs';
interface SitemapEntry {
url: string;
lastmod: string;
changefreq: 'daily' | 'weekly' | 'monthly';
priority: number;
}
async function generateSitemap() {
const baseUrl = 'https://example.com';
// 页面文件を収集
const pages = await globby([
'src/pages/**/*.astro',
'src/content/blog/**/*.mdx',
'!src/pages/api/**',
'!src/pages/404.astro',
]);
const entries: SitemapEntry[] = pages.map(page => {
const path = page
.replace('src/pages/', '')
.replace('src/content/blog/', 'blog/')
.replace(/\.(astro|mdx)$/, '')
.replace(/\/index$/, '/');
return {
url: `${baseUrl}/${path}`,
lastmod: new Date().toISOString().split('T')[0],
changefreq: path.includes('blog') ? 'weekly' : 'monthly',
priority: path === '' ? 1.0 : path.includes('blog') ? 0.7 : 0.8,
};
});
const xml = buildXml(entries);
writeFileSync('public/sitemap.xml', xml);
console.log(`✅ サイトマップ生成完了: ${entries.length}ページ`);
}
function buildXml(entries: SitemapEntry[]): string {
return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${entries.map(e => ` <url>
<loc>${e.url}</loc>
<lastmod>${e.lastmod}</lastmod>
<changefreq>${e.changefreq}</changefreq>
<priority>${e.priority}</priority>
</url>`).join('\n')}
</urlset>`;
}
generateSitemap();
動的ルートへの支持
ブ日志文章如同動的に生成される页面も、内容目录から自動的に収集可以。
// Astro项目での集成例
import { getCollection } from 'astro:content';
export async function getStaticPaths() {
const posts = await getCollection('blog');
return posts.map(post => ({
params: { slug: post.slug },
props: { post },
}));
}
// サイトマップ用にメタ情報を输出
async function collectBlogEntries(baseUrl: string): Promise<SitemapEntry[]> {
const posts = await getCollection('blog');
return posts
.filter(post => !post.data.draft)
.map(post => ({
url: `${baseUrl}/blog/${post.slug}`,
lastmod: post.data.pubDate.toISOString().split('T')[0],
changefreq: 'weekly' as const,
priority: 0.7,
}));
}
サイトマップ索引の实现
大規模サイトでは、サイトマップを分割して索引文件で管理します。
function buildSitemapIndex(sitemaps: string[]): string {
const baseUrl = 'https://example.com';
return `<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${sitemaps.map(name => ` <sitemap>
<loc>${baseUrl}/${name}</loc>
<lastmod>${new Date().toISOString().split('T')[0]}</lastmod>
</sitemap>`).join('\n')}
</sitemapindex>`;
}
// 用途別にサイトマップを分割
const sitemapFiles = [
'sitemap-pages.xml',
'sitemap-blog.xml',
'sitemap-categories.xml',
];
writeFileSync('public/sitemap.xml', buildSitemapIndex(sitemapFiles));
构建流水线への組み込み
{
"scripts": {
"build": "astro build && tsx scripts/generate-sitemap.ts",
"sitemap": "tsx scripts/generate-sitemap.ts",
"postbuild": "tsx scripts/generate-sitemap.ts && tsx scripts/ping-search-engines.ts"
}
}
robots.txtとの联动
> robots.txtにサイトマップのURL添加。
User-agent: *
Allow: /
Sitemap: https://example.com/sitemap.xml
Google Search Consoleへの通知
// scripts/ping-search-engines.ts
async function pingSearchEngines(sitemapUrl: string) {
const endpoints = [
`https://www.google.com/ping?sitemap=${encodeURIComponent(sitemapUrl)}`,
`https://www.bing.com/ping?sitemap=${encodeURIComponent(sitemapUrl)}`,
];
for (const url of endpoints) {
try {
const res = await fetch(url);
console.log(`Ping ${url}: ${res.status}`);
} catch (error) {
console.error(`Ping失敗: ${url}`, error);
}
}
}
pingSearchEngines('https://example.com/sitemap.xml');
总结
サイトマップの自動生成は、SEO対策の基本中の基本です。借助 Claude Code,動的ルートの収集から构建流水线への組み込みまで一気に实现可以。CI/CD流水线に組み込むことで、部署のたびに最新のサイトマップが自動的に生成されます。詳しい仕様はsitemaps.org。
免费 PDF:5 分钟看懂 Claude Code 速查表
只需留下邮箱,我们就会立即把这份 A4 一页速查表 PDF 发送给你。
我们会严格保护你的个人信息,绝不发送垃圾邮件。
把 Claude Code 变成真正能带来结果的工作流
先领取中文说明的免费 PDF,再进入英文商品页选择合适的教材。如果你需要团队落地、流程设计或内容变现支持,也可以直接咨询。
本文作者
Masa
深度使用 Claude Code 的工程师。运营 claudecode-lab.com——一个涵盖 10 种语言、超过 2,000 页内容的科技媒体。
相关文章
每天发布多语言 Claude Code 文章前,要先检查的 7 件事
一份实用清单,帮助你每天发布多语言 Claude Code 文章时避免漏语言、CTA 错位和线上内容未更新。
Codex Automations 是什么?让 AI 在你睡觉时完成内容运营
用 Codex Automations 自动查看流量、选择主题、写文章、改善转化路径并部署网站的实用指南。
Claude Code × GCP Cloud Functions 完全指南 | 极速开发无服务器函数
用 Claude Code 高效开发 GCP Cloud Functions。从 HTTP/Pub/Sub/Firestore 触发器实现到本地测试、部署自动化,基于 Masa 的实战经验,附完整可运行代码示例。