All files / src/pages sitemap-pages.xml.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70                                                                                                                                           
import { getCollection } from "astro:content";
import type { APIContext } from "astro";
import { SITE_URL, formatDate, xmlEscape } from "../utils/sitemap";
 
export async function GET(_context: APIContext) {
  const posts = await getCollection("blog");
 
  // 新しい順にソート
  const sorted = posts.sort(
    (a, b) => new Date(b.data.date).getTime() - new Date(a.data.date).getTime(),
  );
 
  const latestPostDate = formatDate(sorted[0].data.date);
 
  const urls: string[] = [];
 
  // ホームページ(lastmod = 最新記事日)
  urls.push(`  <url>
    <loc>${SITE_URL}/</loc>
    <lastmod>${latestPostDate}</lastmod>
  </url>`);
 
  // About ページ
  urls.push(`  <url>
    <loc>${SITE_URL}/about/</loc>
    <lastmod>${latestPostDate}</lastmod>
  </url>`);
 
  // タグ一覧ページ
  urls.push(`  <url>
    <loc>${SITE_URL}/tags/</loc>
    <lastmod>${latestPostDate}</lastmod>
  </url>`);
 
  // タグ別ページ(記事3件以上のタグのみ)
  const tagCount: { [key: string]: number } = {};
  sorted.forEach((post) => {
    post.data.tags?.forEach((tag) => {
      if (tag) tagCount[tag] = (tagCount[tag] || 0) + 1;
    });
  });
 
  Object.entries(tagCount)
    .filter(([, count]) => count >= 3)
    .forEach(([tag]) => {
      urls.push(`  <url>
    <loc>${SITE_URL}/tag/${xmlEscape(tag)}/</loc>
    <lastmod>${latestPostDate}</lastmod>
  </url>`);
    });
 
  // ページネーションはnoindex設定のためサイトマップから除外
 
  // privacy-policies(lastmod なし)
  urls.push(`  <url>
    <loc>${SITE_URL}/privacy-policies/</loc>
  </url>`);
 
  const xml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls.join("\n")}
</urlset>`;
 
  return new Response(xml, {
    headers: {
      "Content-Type": "application/xml; charset=utf-8",
    },
  });
}