All files / src/pages indexnow.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                                                                                 
import { getCollection } from "astro:content";
import type { APIContext } from "astro";
 
const SITE_URL = "https://tubone-project24.xyz";
 
export async function GET(_context: APIContext) {
  const posts = await getCollection("blog");
 
  const sorted = posts
    .filter((post) => !post.data.noindex)
    .sort(
      (a, b) =>
        new Date(b.data.date).getTime() - new Date(a.data.date).getTime(),
    );
 
  // 最新30件のURLをIndexNowに通知対象とする
  const recentPosts = sorted.slice(0, 30);
 
  const urlList = recentPosts
    .map((post) => `    <url><loc>${SITE_URL}/${post.slug}/</loc></url>`)
    .join("\n");
 
  // 静的ページも含める
  const staticUrls = [
    `    <url><loc>${SITE_URL}/</loc></url>`,
    `    <url><loc>${SITE_URL}/about/</loc></url>`,
  ].join("\n");
 
  const xml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${staticUrls}
${urlList}
</urlset>`;
 
  return new Response(xml, {
    headers: {
      "Content-Type": "application/xml; charset=utf-8",
    },
  });
}