Skip to content

Open source alternative to Semrush and Ahrefs

Make the IndexNow card show whether the key file is set up (#1014)

Ben Senescub76ba8e06089
diff --git a/docs/maintainers/specs/0016-indexnow.md b/docs/maintainers/specs/0016-indexnow.md
index 271830bb..29f0119d 100644
--- a/docs/maintainers/specs/0016-indexnow.md
+++ b/docs/maintainers/specs/0016-indexnow.md
@@ -16,7 +16,7 @@ Submitting is free. Any member of the organization can set up the key and submit
## Surfaces
-- **Settings → Integrations** has an IndexNow card. It explains which engines participate, creates the key, offers the key file for download, shows where to host it, and accepts a list of URLs to submit.
+- **Settings → Integrations** has an IndexNow card. It explains which engines participate, creates the key, offers the key file for download, and checks the file on the bare domain and its `www.` host, with a status for each. A host that redirects to the same file on the other host, or doesn't load, has no pages to submit and is left out. Once every remaining host serves the file, the setup instructions collapse. The card accepts a list of URLs to submit and suggests asking a connected agent to submit pages, or to add the key file when it can edit the site's code.
- **MCP**: `submit_indexnow_urls` submits URLs for a project and creates the key on first use. When a key file is missing, the result names the file and its contents, so an agent with access to the site's code can add it and try again.
## Alternatives
diff --git a/src/client/features/settings/IndexNowSettings.tsx b/src/client/features/settings/IndexNowSettings.tsx
index 8a3ad393..2aa5efdc 100644
--- a/src/client/features/settings/IndexNowSettings.tsx
+++ b/src/client/features/settings/IndexNowSettings.tsx
@@ -1,6 +1,6 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type { ReactNode } from "react";
-import { Download } from "lucide-react";
+import { ChevronRight, CircleCheck, CircleX, Download } from "lucide-react";
import { toast } from "sonner";
import { z } from "zod";
import { revalidateLogic } from "@tanstack/react-form";
@@ -8,8 +8,15 @@ import { useAppForm } from "@/client/components/form/useAppForm";
import { CardShell } from "@/client/components/CardShell";
import { QueryError } from "@/client/components/QueryState";
import { SafeExternalLink } from "@/client/components/SafeExternalLink";
+import { CopyButton } from "@/client/components/CopyButton";
import { Button } from "@/client/components/ui/button";
import {
+ Collapsible,
+ CollapsibleContent,
+ CollapsibleTrigger,
+} from "@/client/components/ui/collapsible";
+import {
+ checkIndexNowKeyFiles,
getIndexNowSetup,
setUpIndexNow,
submitIndexNowUrls,
@@ -28,6 +35,27 @@ function splitUrls(value: string) {
.filter(Boolean);
}
+const AGENT_PROMPT = "Submit the pages I just published to IndexNow.";
+
+type KeyFileHost = Awaited<
+ ReturnType<typeof checkIndexNowKeyFiles>
+>["hosts"][number];
+
+const KEY_FILE_LABELS: Record<KeyFileHost["status"], string> = {
+ found: "Found",
+ missing: "Not found",
+ wrong_content: "Should contain only the key",
+ redirects: "Redirects. The file must load at this address itself",
+ moved: "Redirects to another address",
+ unreachable: "Couldn't reach this address",
+ unknown: "Couldn't check right now",
+};
+
+// A host that forwards to another one or doesn't load has no pages to
+// submit, so it doesn't need the file.
+const isUnused = (host: KeyFileHost) =>
+ host.status === "moved" || host.status === "unreachable";
+
const pluralUrls = (count: number) => `${count} URL${count === 1 ? "" : "s"}`;
function downloadKeyFile(key: string) {
@@ -58,6 +86,14 @@ export function IndexNowSettings({ projectId }: { projectId: string }) {
onSuccess: (setup) => queryClient.setQueryData(setupQueryKey, setup),
});
+ const keyFilesQueryKey = ["indexnow-key-files", projectId];
+ const hasKey = Boolean(setupQuery.data?.domain && setupQuery.data.key);
+ const keyFilesQuery = useQuery({
+ queryKey: keyFilesQueryKey,
+ queryFn: () => checkIndexNowKeyFiles({ data: { projectId } }),
+ enabled: hasKey,
+ });
+
const submitMutation = useMutation({
mutationFn: (urls: string[]) =>
submitIndexNowUrls({ data: { projectId, urls } }),
@@ -86,6 +122,9 @@ export function IndexNowSettings({ projectId }: { projectId: string }) {
`Skipped ${pluralUrls(result.skipped.length)} not on ${setupQuery.data?.domain}`,
);
}
+ if (failed.length > 0) {
+ void queryClient.invalidateQueries({ queryKey: keyFilesQueryKey });
+ }
if (failed.length === 0 && sent > 0) formApi.reset();
},
});
@@ -112,8 +151,9 @@ export function IndexNowSettings({ projectId }: { projectId: string }) {
<CardShell title="IndexNow">
<div className="flex flex-1 flex-col gap-4 text-sm">
<p className="text-muted-foreground">
- Bing and other search engines recrawl the pages you submit sooner.
- Google doesn't support IndexNow.
+ Tell Bing about new and updated pages as soon as you publish them,
+ instead of waiting for it to crawl your site. Bing also powers
+ DuckDuckGo and Yahoo results. Google doesn't use IndexNow.
</p>
{!domain ? (
<p className="text-muted-foreground">
@@ -132,34 +172,20 @@ export function IndexNowSettings({ projectId }: { projectId: string }) {
</div>
) : (
<ol className="space-y-5">
- <Step number={1} title="Download your key file">
- <Button
- variant="outline"
- size="sm"
- className="w-fit"
- onClick={() => downloadKeyFile(key)}
- >
- <Download className="size-4" />
- Download key file
- </Button>
- </Step>
- <Step number={2} title="Upload it to the root of your site">
- <p className="text-muted-foreground">
- It should open at{" "}
- <SafeExternalLink
- url={`https://${domain}/${key}.txt`}
- label={`https://${domain}/${key}.txt`}
- className="inline-flex items-center gap-1 font-mono break-all text-foreground underline underline-offset-2"
- />
- </p>
- <p className="text-muted-foreground">
- If your pages are on www.{domain}, upload it there too.
- </p>
- </Step>
+ <KeyFileStep
+ domain={domain}
+ indexNowKey={key}
+ hosts={keyFilesQuery.data?.hosts}
+ isChecking={keyFilesQuery.isFetching}
+ onCheck={() => void keyFilesQuery.refetch()}
+ />
<Step
- number={3}
- title="Submit pages you added, changed or removed"
+ number={2}
+ title="Submit pages when you publish or update them"
>
+ <p className="text-muted-foreground">
+ Paste just the pages that changed, not your whole site.
+ </p>
<form.AppForm>
<form.Form className="space-y-3">
<form.AppField name="urls">
@@ -178,6 +204,25 @@ export function IndexNowSettings({ projectId }: { projectId: string }) {
<form.SubmitButton>Submit to IndexNow</form.SubmitButton>
</form.Form>
</form.AppForm>
+ <div className="space-y-1 rounded-md bg-muted/50 p-3 text-muted-foreground">
+ <div className="flex flex-wrap items-center gap-2">
+ <span>
+ Or ask your agent:{" "}
+ <span className="text-foreground">"{AGENT_PROMPT}"</span>
+ </span>
+ <CopyButton
+ value={AGENT_PROMPT}
+ successMessage="Prompt copied"
+ size="icon-xs"
+ variant="ghost"
+ label="Copy prompt"
+ />
+ </div>
+ <p className="text-xs">
+ If it can edit your site's code, it can add the key file
+ too.
+ </p>
+ </div>
</Step>
</ol>
)}
@@ -187,6 +232,130 @@ export function IndexNowSettings({ projectId }: { projectId: string }) {
);
}
+function KeyFileStep({
+ domain,
+ indexNowKey,
+ hosts,
+ isChecking,
+ onCheck,
+}: {
+ domain: string;
+ indexNowKey: string;
+ hosts: KeyFileHost[] | undefined;
+ isChecking: boolean;
+ onCheck: () => void;
+}) {
+ const used = hosts?.filter((host) => !isUnused(host)) ?? [];
+ // Until the check answers, list both addresses without a status.
+ const shown: { host: string; status?: KeyFileHost["status"] }[] = !hosts
+ ? [{ host: domain }, { host: `www.${domain}` }]
+ : used.length > 0
+ ? used
+ : hosts;
+ const verified =
+ used.length > 0 && used.every((host) => host.status === "found");
+
+ return (
+ <Step
+ number={1}
+ title={
+ verified ? "Your key file is set up" : "Add this key file to your site"
+ }
+ >
+ {verified ? null : (
+ <>
+ <Button
+ variant="outline"
+ size="sm"
+ className="w-fit"
+ onClick={() => downloadKeyFile(indexNowKey)}
+ >
+ <Download className="size-4" />
+ Download key file
+ </Button>
+ <p className="text-muted-foreground">
+ Put it at the root of your site, so it opens at:
+ </p>
+ </>
+ )}
+ <ul className="space-y-1">
+ {shown.map((host) => (
+ <li
+ key={host.host}
+ className="flex flex-wrap items-center gap-x-2 gap-y-0.5"
+ >
+ <SafeExternalLink
+ url={`https://${host.host}/${indexNowKey}.txt`}
+ label={`https://${host.host}/${indexNowKey}.txt`}
+ className="inline-flex items-center gap-1 font-mono text-xs break-all text-foreground underline underline-offset-2"
+ />
+ {host.status ? <KeyFileStatus status={host.status} /> : null}
+ </li>
+ ))}
+ </ul>
+ <Button
+ variant="outline"
+ size="xs"
+ className="w-fit"
+ pending={isChecking}
+ onClick={onCheck}
+ >
+ Check again
+ </Button>
+ {verified ? null : (
+ <Collapsible>
+ <CollapsibleTrigger className="group inline-flex items-center gap-1 text-muted-foreground hover:text-foreground">
+ <ChevronRight
+ className="size-3.5 transition-transform group-data-[panel-open]:rotate-90"
+ aria-hidden
+ />
+ How do I add a file to my site?
+ </CollapsibleTrigger>
+ <CollapsibleContent>
+ <ul className="mt-2 list-disc space-y-1 pl-5 text-muted-foreground">
+ <li>
+ Site built from code: put the file in the folder your site
+ serves as-is, often called <code>public</code> or{" "}
+ <code>static</code>.
+ </li>
+ <li>
+ WordPress: upload it with your host's file manager or SFTP to
+ the folder that holds <code>wp-config.php</code>. SEO plugins
+ like Rank Math can also submit to IndexNow for you.
+ </li>
+ <li>
+ On Cloudflare: turn on Crawler Hints in Cloudflare's caching
+ settings and it submits changed pages to IndexNow for you.
+ </li>
+ <li>
+ Website builders like Webflow, Shopify, Squarespace and Wix
+ usually don't let you add files at the root. Check whether yours
+ already supports IndexNow.
+ </li>
+ </ul>
+ </CollapsibleContent>
+ </Collapsible>
+ )}
+ </Step>
+ );
+}
+
+function KeyFileStatus({ status }: { status: KeyFileHost["status"] }) {
+ const problem =
+ status === "missing" ||
+ status === "wrong_content" ||
+ status === "redirects";
+ const Icon = status === "found" ? CircleCheck : problem ? CircleX : undefined;
+ return (
+ <span
+ className={`inline-flex items-center gap-1 text-xs ${status === "found" ? "text-success" : problem ? "text-destructive" : "text-muted-foreground"}`}
+ >
+ {Icon ? <Icon className="size-3.5" aria-hidden /> : null}
+ {KEY_FILE_LABELS[status]}
+ </span>
+ );
+}
+
function Step({
number,
title,
diff --git a/src/server/features/indexnow/services/IndexNowService.test.ts b/src/server/features/indexnow/services/IndexNowService.test.ts
index 81059e76..83f1f1ef 100644
--- a/src/server/features/indexnow/services/IndexNowService.test.ts
+++ b/src/server/features/indexnow/services/IndexNowService.test.ts
@@ -116,6 +116,13 @@ describe("IndexNowService.submitUrls", () => {
headers: { location: "https://www.example.com/" },
}),
],
+ [
+ "redirects to the same file on www",
+ new Response(null, {
+ status: 301,
+ headers: { location: `https://www.example.com/${KEY}.txt` },
+ }),
+ ],
])("doesn't submit when the key file %s", async (_case, keyFile) => {
respond(keyFile);
@@ -171,3 +178,23 @@ describe("IndexNowService.submitUrls", () => {
expect(result.hosts[0]?.message).toContain("https://example.com");
});
});
+
+describe("IndexNowService.checkKeyFiles", () => {
+ it("tells a host that forwards to www apart from a missing file", async () => {
+ fetchMock.mockImplementation(async (input) =>
+ input === `https://example.com/${KEY}.txt`
+ ? new Response(null, {
+ status: 301,
+ headers: { location: `https://www.example.com/${KEY}.txt` },
+ })
+ : new Response("Not found", { status: 404 }),
+ );
+
+ const result = await IndexNowService.checkKeyFiles(scope);
+
+ expect(result.hosts).toEqual([
+ { host: "example.com", status: "moved" },
+ { host: "www.example.com", status: "missing" },
+ ]);
+ });
+});
diff --git a/src/server/features/indexnow/services/IndexNowService.ts b/src/server/features/indexnow/services/IndexNowService.ts
index 6b4fb21a..8e35520e 100644
--- a/src/server/features/indexnow/services/IndexNowService.ts
+++ b/src/server/features/indexnow/services/IndexNowService.ts
@@ -63,16 +63,19 @@ async function setUp(scope: ProjectScope) {
return { domain: project.domain, key: saved.indexnowKey, created: true };
}
-/**
- * Catches the setup mistakes engines would otherwise reject later: a missing
- * file, a page served in its place, or a redirect (engines want the file
- * itself). Anything inconclusive, like a bot wall or a timeout, returns null
- * and the submission goes ahead.
- */
-async function findKeyFileProblem(
- host: string,
- key: string,
-): Promise<string | null> {
+type KeyFileStatus =
+ | "found"
+ | "missing"
+ | "wrong_content"
+ | "redirects"
+ // Redirects to the same file on another host, like a bare domain that
+ // forwards everything to www. Its pages live on that other host.
+ | "moved"
+ | "unreachable"
+ // A bot wall, server error, timeout or stalled body: the file may be fine.
+ | "unknown";
+
+async function checkKeyFile(host: string, key: string): Promise<KeyFileStatus> {
const url = `https://${host}/${key}.txt`;
let response: Response;
try {
@@ -81,23 +84,64 @@ async function findKeyFileProblem(
redirect: "manual",
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
- } catch {
- return null;
+ } catch (error) {
+ return error instanceof DOMException && error.name === "TimeoutError"
+ ? "unknown"
+ : "unreachable";
}
if (!response.ok) {
void response.body?.cancel().catch(() => {});
- if (response.status === 404 || response.status === 410) {
- return `No key file at the root of https://${host}. Upload it there, then try again.`;
- }
+ if (response.status === 404 || response.status === 410) return "missing";
if (response.status >= 300 && response.status < 400) {
- return `The key file on https://${host} redirects. Upload it to https://${host} itself.`;
+ const target = URL.parse(response.headers.get("location") ?? "", url);
+ return target &&
+ target.hostname !== host &&
+ target.pathname === `/${key}.txt`
+ ? "moved"
+ : "redirects";
}
- return null;
+ return "unknown";
}
- // A body that stalls or drops mid-read is inconclusive, like a timeout.
const body = await readBoundedText(response).catch(() => undefined);
- if (body === undefined || body?.trim() === key) return null;
- return `The key file on https://${host} should contain only the key.`;
+ if (body === undefined) return "unknown";
+ return body?.trim() === key ? "found" : "wrong_content";
+}
+
+/**
+ * Catches the setup mistakes engines would otherwise reject later: a missing
+ * file, a page served in its place, or a redirect (engines want the file
+ * itself). Anything inconclusive returns null and the submission goes ahead.
+ */
+async function findKeyFileProblem(
+ host: string,
+ key: string,
+): Promise<string | null> {
+ switch (await checkKeyFile(host, key)) {
+ case "missing":
+ return `No key file at the root of https://${host}. Upload it there, then try again.`;
+ case "wrong_content":
+ return `The key file on https://${host} should contain only the key.`;
+ case "redirects":
+ case "moved":
+ return `The key file on https://${host} redirects. Upload it to https://${host} itself.`;
+ default:
+ return null;
+ }
+}
+
+/** Where the key file is hosted, for the bare domain and its www host. */
+async function checkKeyFiles(scope: ProjectScope) {
+ const project = await loadProject(scope);
+ const domain = project.domain;
+ const key = project.indexnowKey;
+ if (!domain || !key) return { hosts: [] };
+ const hosts = await Promise.all(
+ [domain, `www.${domain}`].map(async (host) => ({
+ host,
+ status: await checkKeyFile(host, key),
+ })),
+ );
+ return { hosts };
}
function describeResponse(
@@ -226,5 +270,6 @@ async function submitUrls(scope: ProjectScope, urls: string[]) {
export const IndexNowService = {
getSetup,
setUp,
+ checkKeyFiles,
submitUrls,
} as const;
diff --git a/src/serverFunctions/indexnow.ts b/src/serverFunctions/indexnow.ts
index 47e0d98f..2430376d 100644
--- a/src/serverFunctions/indexnow.ts
+++ b/src/serverFunctions/indexnow.ts
@@ -11,6 +11,11 @@ export const getIndexNowSetup = createServerFn({ method: "GET" })
.validator(indexNowProjectSchema)
.handler(async ({ context }) => IndexNowService.getSetup(context));
+export const checkIndexNowKeyFiles = createServerFn({ method: "GET" })
+ .middleware(requireProjectContext)
+ .validator(indexNowProjectSchema)
+ .handler(async ({ context }) => IndexNowService.checkKeyFiles(context));
+
export const setUpIndexNow = createServerFn({ method: "POST" })
.middleware(requireProjectContext)
.validator(indexNowProjectSchema)