Files
Rich Haines 9ab9c715d6 Add AI agent detection via @vercel/agent-readability (#421)
* Add @vercel/agent-readability dependency

* Add AI agent detection via @vercel/agent-readability

* Fix md-tracking types, add Vary header and discovery options

- Update TrackMdRequestParams to include 'agent-rewrite' requestType and detectionMethod
- Import DetectionMethod type from @vercel/agent-readability
- Include detectionMethod in tracking fetch body
- Add Vary: Accept header on agent-rewrite responses
- Add discovery options (sitemapUrl, indexUrl) to generateNotFoundMarkdown

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Update pnpm-lock.yaml

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 14:23:35 +02:00

57 lines
1.5 KiB
TypeScript

import type { DetectionMethod } from "@vercel/agent-readability";
import { siteId } from "@/geistdocs";
const PLATFORM_URL = "https://geistdocs.com/md-tracking";
interface TrackMdRequestParams {
acceptHeader: string | null;
/** Detection method used to identify the agent (only for agent-rewrite requests) */
detectionMethod?: DetectionMethod | null;
path: string;
referer: string | null;
/** How the markdown was requested: 'md-url' for direct .md URLs, 'header-negotiated' for Accept header, 'agent-rewrite' for detected AI agents */
requestType?: "md-url" | "header-negotiated" | "agent-rewrite";
userAgent: string | null;
}
/**
* Track a markdown page request via the geistdocs platform.
* Fire-and-forget: errors are logged but don't affect the response.
*/
export async function trackMdRequest({
path,
userAgent,
referer,
acceptHeader,
requestType,
detectionMethod,
}: TrackMdRequestParams): Promise<void> {
try {
const response = await fetch(PLATFORM_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
path,
siteId: siteId ?? "geistdocs-unknown",
userAgent,
referer,
acceptHeader,
requestType,
detectionMethod,
}),
});
if (!response.ok) {
console.error(
"MD tracking failed:",
response.status,
await response.text()
);
}
} catch (error) {
console.error("MD tracking error:", error);
}
}