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                                                                                                               
import { getCollection } from "astro:content";
import type { APIContext } from "astro";
import { SITE_URL, formatDate } from "../utils/sitemap";
 
const POSTS_PER_PAGE = 10;
 
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 totalPages = Math.ceil(sorted.length / POSTS_PER_PAGE);
  const latestPostDate = formatDate(sorted[0].data.date);
 
  const urls: string[] = [];
 
  // ホームページ(lastmod = 最新記事日)
  urls.push(`  <url>
    <loc>${SITE_URL}/</loc>
    <lastmod>${latestPostDate}</lastmod>
  </url>`);
 
  // ページネーション(/pages/2/ 〜 /pages/N/)
  for (let page = 2; page <= totalPages; page++) {
    const start = (page - 1) * POSTS_PER_PAGE;
    const pagePosts = sorted.slice(start, start + POSTS_PER_PAGE);
    const pageLastmod =
      pagePosts.length > 0
        ? formatDate(pagePosts[0].data.date)
        : latestPostDate;
    urls.push(`  <url>
    <loc>${SITE_URL}/pages/${page}/</loc>
    <lastmod>${pageLastmod}</lastmod>
  </url>`);
  }
 
  // 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",
    },
  });
}