39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
// app/api/sitemap/route.ts
|
|
import { NextResponse } from "next/server";
|
|
|
|
export async function GET(req: Request) {
|
|
try {
|
|
const { searchParams } = new URL(req.url);
|
|
const u = searchParams.get("u");
|
|
if (!u) return NextResponse.json({ error: "Missing ?u=" }, { status: 400 });
|
|
|
|
const target = new URL(u.match(/^https?:\/\//i) ? u : `https://${u}`);
|
|
|
|
// This is correct: no double-escaping
|
|
const candidates: string[] = /\/sitemap(.*)\.xml$/i.test(target.pathname)
|
|
? [target.toString()]
|
|
: [
|
|
new URL("/sitemap.xml", target.origin).toString(),
|
|
new URL("/sitemap_index.xml", target.origin).toString(),
|
|
];
|
|
|
|
// fetch & parse each candidate; you can also call your backend util if exposed
|
|
const urls:any = new Set<string>();
|
|
|
|
|
|
// very light probe: just check existence; swap to real parser if needed
|
|
for (const href of candidates) {
|
|
try {
|
|
const r = await fetch(href, { headers: { "user-agent": "CrawlerX/1.0" }, cache: "no-store" });
|
|
if (r.ok) urls.add(href);
|
|
} catch {}
|
|
}
|
|
|
|
// If you want full expansion, call your backend endpoint instead of the above loop
|
|
|
|
return NextResponse.json({ ok: true, origin: target.origin, count: urls.size, urls: [...urls] });
|
|
} catch (e: any) {
|
|
return NextResponse.json({ error: e?.message || "Bad URL" }, { status: 400 });
|
|
}
|
|
}
|