|
| 1 | +import { NextResponse } from "next/server"; |
| 2 | + |
| 3 | +const INDEXNOW_KEY = process.env.INDEXNOW_KEY; |
| 4 | +const SITE_URL = "https://www.githubster.com"; |
| 5 | + |
| 6 | +const URLS_TO_INDEX = [ |
| 7 | + SITE_URL, |
| 8 | +]; |
| 9 | + |
| 10 | +export async function GET() { |
| 11 | + if (!INDEXNOW_KEY) { |
| 12 | + return NextResponse.json( |
| 13 | + { success: false, message: "INDEXNOW_KEY is not configured" }, |
| 14 | + { status: 500 } |
| 15 | + ); |
| 16 | + } |
| 17 | + |
| 18 | + try { |
| 19 | + const response = await fetch("https://api.indexnow.org/indexnow", { |
| 20 | + method: "POST", |
| 21 | + headers: { |
| 22 | + "Content-Type": "application/json; charset=utf-8", |
| 23 | + }, |
| 24 | + body: JSON.stringify({ |
| 25 | + host: "www.githubster.com", |
| 26 | + key: INDEXNOW_KEY, |
| 27 | + keyLocation: `${SITE_URL}/${INDEXNOW_KEY}.txt`, |
| 28 | + urlList: URLS_TO_INDEX, |
| 29 | + }), |
| 30 | + }); |
| 31 | + |
| 32 | + if (response.ok || response.status === 202) { |
| 33 | + return NextResponse.json({ |
| 34 | + success: true, |
| 35 | + status: response.status, |
| 36 | + message: "URLs submitted to IndexNow successfully", |
| 37 | + urls: URLS_TO_INDEX, |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + const text = await response.text(); |
| 42 | + return NextResponse.json( |
| 43 | + { |
| 44 | + success: false, |
| 45 | + status: response.status, |
| 46 | + message: `IndexNow API error: ${text}`, |
| 47 | + }, |
| 48 | + { status: response.status } |
| 49 | + ); |
| 50 | + } catch (error) { |
| 51 | + return NextResponse.json( |
| 52 | + { |
| 53 | + success: false, |
| 54 | + message: error instanceof Error ? error.message : "Unknown error", |
| 55 | + }, |
| 56 | + { status: 500 } |
| 57 | + ); |
| 58 | + } |
| 59 | +} |
0 commit comments