|
| 1 | +import { NextResponse } from "next/server"; |
| 2 | + |
| 3 | +const SITE_URL = |
| 4 | + process.env.SITE_URL || |
| 5 | + process.env.NEXT_PUBLIC_SITE_URL || |
| 6 | + "https://readmegen-ai.vercel.app"; |
| 7 | + |
| 8 | +// IndexNow key matches the file in /public/85f2c293fce3483d896a5d9dc98974e5.txt |
| 9 | +const INDEXNOW_KEY = "85f2c293fce3483d896a5d9dc98974e5"; |
| 10 | + |
| 11 | +const URLS_TO_INDEX = [ |
| 12 | + `${SITE_URL}/`, |
| 13 | + `${SITE_URL}/features`, |
| 14 | + `${SITE_URL}/examples`, |
| 15 | + `${SITE_URL}/docs`, |
| 16 | + `${SITE_URL}/generate`, |
| 17 | +]; |
| 18 | + |
| 19 | +/** |
| 20 | + * POST /api/indexnow |
| 21 | + * Notifies Bing IndexNow of updated URLs for timely crawling and indexing. |
| 22 | + * Call this endpoint after deploying significant content changes. |
| 23 | + */ |
| 24 | +export async function POST() { |
| 25 | + const host = new URL(SITE_URL).hostname; |
| 26 | + |
| 27 | + const payload = { |
| 28 | + host, |
| 29 | + key: INDEXNOW_KEY, |
| 30 | + keyLocation: `${SITE_URL}/${INDEXNOW_KEY}.txt`, |
| 31 | + urlList: URLS_TO_INDEX, |
| 32 | + }; |
| 33 | + |
| 34 | + try { |
| 35 | + const response = await fetch("https://api.indexnow.org/indexnow", { |
| 36 | + method: "POST", |
| 37 | + headers: { "Content-Type": "application/json; charset=utf-8" }, |
| 38 | + body: JSON.stringify(payload), |
| 39 | + }); |
| 40 | + |
| 41 | + if (response.ok || response.status === 202) { |
| 42 | + return NextResponse.json( |
| 43 | + { success: true, submitted: URLS_TO_INDEX.length }, |
| 44 | + { status: 200 }, |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + return NextResponse.json( |
| 49 | + { success: false, status: response.status }, |
| 50 | + { status: 502 }, |
| 51 | + ); |
| 52 | + } catch (error) { |
| 53 | + console.error("IndexNow submission failed:", error); |
| 54 | + return NextResponse.json( |
| 55 | + { success: false, error: "Network error during IndexNow submission" }, |
| 56 | + { status: 502 }, |
| 57 | + ); |
| 58 | + } |
| 59 | +} |
0 commit comments