(urls)
| 39 | }; |
| 40 | |
| 41 | const submit = async (urls) => { |
| 42 | if (!key) { |
| 43 | console.log("INDEXNOW_KEY is not set; skip IndexNow submission."); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | const urlList = normalizeUrls(urls).filter((url) => { |
| 48 | try { |
| 49 | return new URL(url).hostname === host; |
| 50 | } catch { |
| 51 | return false; |
| 52 | } |
| 53 | }); |
| 54 | |
| 55 | if (urlList.length === 0) { |
| 56 | throw new Error( |
| 57 | "No valid URLs to submit. Pass URLs as args, set INDEXNOW_URLS, or use --sitemap.", |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | const response = await fetch(endpoint, { |
| 62 | method: "POST", |
| 63 | headers: { |
| 64 | "Content-Type": "application/json; charset=utf-8", |
| 65 | }, |
| 66 | body: JSON.stringify({ |
| 67 | host, |
| 68 | key, |
| 69 | keyLocation, |
| 70 | urlList, |
| 71 | }), |
| 72 | }); |
| 73 | |
| 74 | if (!response.ok) { |
| 75 | const body = await response.text(); |
| 76 | throw new Error( |
| 77 | `IndexNow submission failed: ${response.status} ${response.statusText}\n${body}`, |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | console.log(`Submitted ${urlList.length} URL(s) to IndexNow.`); |
| 82 | }; |
| 83 | |
| 84 | const sitemapUrls = shouldReadSitemap ? await readSitemapUrls() : []; |
| 85 |
no test coverage detected