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。
#Claude Code
#sitemap
#SEO
#XML
#automation
Related Posts
Use Cases
Use Cases
用 Claude Code 加速个人项目开发【附实战案例】
详解如何用 Claude Code 大幅提升个人项目的开发速度。包含从创意到上线的完整实战案例和工作流。
Use Cases
Use Cases
如何用 Claude Code 自动化代码重构
详解如何利用 Claude Code 高效完成代码重构自动化。包含实用提示词和真实项目中的重构模式。
Use Cases
Use Cases
Complete CORS Configuration Guide:Claude Code 实战指南
了解complete cors configuration guide:Claude Code 实战. 包含实用技巧和代码示例。