youraitools-api / tree / scripts/gen_sitemap.mjs ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
#!/usr/bin/env node
// Build step: write public/sitemap.xml (static, no Function invocations) from the same TOOLS/SERVICES list as the index,
// so a tool added to functions/tools.js appears automatically on the next build.
// Run: node scripts/gen_sitemap.mjs            (probes each URL live; drops any that is not 200 text/html, with a warning)
//      node scripts/gen_sitemap.mjs --offline  (no probing; used by tests)
// Only human HTML pages: no paid routes, /v1/*, /mcp, JSON-only or /.well-known routes.
import { writeFileSync } from 'node:fs';
import { TOOLS, SERVICES } from '../functions/tools.js';

const BASE = 'https://api.youraitools.co';
// Shareable report/card pages (free, cached HTML with a canonical link). Use the canonical form of each URL.
export const SAMPLES = [
  '/docs/which-api',
  '/agentready/r/example.com',
  '/agentready/r/developers.cloudflare.com',
  '/agentready/r/youraitools.co',
  '/solverdict/t/EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
  '/solverdict/t/So11111111111111111111111111111111111111112',
  '/solverdict/t/DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263',
  '/tokenverdict/t/0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
  '/tokenverdict/t/0x532f27101965dd16442e59d40670faf5ebb142e4',
];
const EXCLUDE = /(^|\/)(v1|mcp|\.well-known)(\/|$)|\.json$|\/(download|openapi)/;

export function sitemapEntries() {
  const out = [{ path: '/', changefreq: 'daily', priority: '1.0' }];
  for (const t of [...TOOLS, ...SERVICES]) out.push({ path: `/${t.prefix}/`, changefreq: 'weekly', priority: '0.8' });
  for (const p of SAMPLES) out.push({ path: p, changefreq: 'weekly', priority: '0.5' });
  for (const e of out) if (EXCLUDE.test(e.path)) throw new Error(`not a human page: ${e.path}`);
  return out;
}

const xmlEsc = (s) => String(s).replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&apos;' }[c]));
export function sitemapXml(entries, lastmod) {
  return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${entries.map((e) => `  <url><loc>${xmlEsc(BASE + e.path)}</loc><lastmod>${lastmod}</lastmod><changefreq>${e.changefreq}</changefreq><priority>${e.priority}</priority></url>`).join('\n')}\n</urlset>\n`;
}

async function isHtml200(url) {
  for (let i = 0; i < 2; i++) {
    try {
      const r = await fetch(url, { headers: { accept: 'text/html', 'user-agent': 'youraitools-sitemap-build' }, redirect: 'manual', signal: AbortSignal.timeout(20000) });
      await r.body?.cancel();
      if (r.status === 200 && /^text\/html/i.test(r.headers.get('content-type') || '')) return true;
    } catch {}
  }
  return false;
}

if (import.meta.url === `file://${process.argv[1]}`) {
  let entries = sitemapEntries();
  if (!process.argv.includes('--offline')) {
    const ok = await Promise.all(entries.map((e) => isHtml200(BASE + e.path)));
    for (const [i, e] of entries.entries()) if (!ok[i]) console.warn(`skip (not 200 text/html): ${e.path}`);
    entries = entries.filter((_, i) => ok[i]);
  }
  const lastmod = new Date().toISOString().slice(0, 10);
  writeFileSync(new URL('../public/sitemap.xml', import.meta.url), sitemapXml(entries, lastmod));
  console.log(`public/sitemap.xml: ${entries.length} URLs, lastmod ${lastmod}`);
}

agentgate-source: code mirror on Cloudflare Pages. All repos