// middleware.ts (Next.js 16+: proxy.ts, rename middleware to proxy)
// Middleware cannot see the final route response. This fallback reports status
// 0 (unknown). For complete HTTP status coverage use the Express finish hook
// around your Next server or a hosting-edge response hook. Set
// AIOLA_FINAL_STATUS_CAPTURE=1 only after that hook covers EVERY request.
import { NextResponse } from "next/server"
import type { NextFetchEvent, NextRequest } from "next/server"
// aiola-bot-tracking v3
const BOT_RE = /(?:(?:^|[\s;(])(?:ChatGPT-Agent)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:ChatGPT-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:OAI-SearchBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:OAI-AdsBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:GPTBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Claude-Agent)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Claude-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Claude-SearchBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:ClaudeBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:anthropic-ai)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Perplexity-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:PerplexityBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:MistralAI-Training)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:MistralAI-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:MistralAI-Index)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:xAI-SearchBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Grok-DeepSearch)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:GrokBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:xAI-Web-Crawler)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:xAI-Grok)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:xAI-Bot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Google-GeminiNotebook|Google-NotebookLM)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Google-Read-Aloud)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Google-Agent|GoogleAgent)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Google-InspectionTool)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Google-CloudVertexBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:GoogleOther)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Googlebot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Applebot(?!-Extended))(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Copilot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:BingBot|bingbot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:msnbot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:NovaAct)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Amzn-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Amzn-SearchBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Amazonbot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:TikTokSpider)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Bytespider)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Doubaobot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Meta-ExternalFetcher|meta-externalfetcher)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:meta-webindexer)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:meta-externalads)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:facebookexternalhit)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Meta-ExternalAgent)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:CCBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:cohere-ai|cohere-training-data-crawler)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:DuckAssistBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:ExaSearchBot(?:-Images)?)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:ExaUser)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:YouBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Kimi-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Kimi-SearchBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:KimiBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Qwen-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:QwenBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:TongyiBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:AliyunBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Baiduspider)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:ERNIEBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:YiyanBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:ChatGLM-Spider)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:DeepSeekBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:AI2Bot\-Dolma)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:AI2Bot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Diffbot-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Diffbot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:FirecrawlAgent)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Timpibot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:ImagesiftBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:LinerBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:QualifiedBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:YandexAdditional(Bot)?)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:YandexBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Kagibot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:DuckDuckBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:SeznamBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:PetalBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Qwantbot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:AdsBot-Google[-A-Za-z]*)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Storebot\-Google)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Google\-Safety)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:APIs\-Google)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:FeedFetcher\-Google)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Google\-Site\-Verification)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:AdIdxBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:BingPreview)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:MicrosoftPreview)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:BingVideoPreview)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:yisouspider)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:iTMS)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Yandex[A-Za-z]*)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Sogou[A-Za-z]*)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Exabot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Exalead)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Twitterbot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:LinkedInBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:WhatsApp(?=\/))(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:TelegramBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Discordbot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Mediapartners[-A-Za-z]*)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:SemrushBot[-A-Za-z]*)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Semrush[A-Za-z]*)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:AhrefsBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:MJ12bot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:DotBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Chrome\-Lighthouse)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Lighthouse)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:PageSpeed)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:GTmetrix)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Pingdom)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:UptimeRobot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:HeadlessChrome)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:PhantomJS)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Cloudflare\-AI\-Search\-External)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Omgilibot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Omgili)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Webzio\-Extended)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:AmazonBuyForMe)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:FacebookBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Kimi-Agent)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Liner-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Manus-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:HarkBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Shap-User)(?![A-Za-z0-9_-]))|(?:^|[\s;(])((?!CUBOT(?:[/\s;)]|$))[A-Za-z0-9_-]*(?:bot|crawler|spider|slurp)[A-Za-z0-9_-]*|Google-Extended)(?=[/\s;)]|$)/i
const ASSET_RE = /\.(png|jpe?g|gif|webp|avif|svg|ico|css|js|mjs|map|woff2?|ttf|otf|eot|webmanifest|mp4|webm)$/i
const isChatGPTAgent = function isChatGPTAgent(value = "") {
try {
const url = new URL(value.trim().replace(/^"|"$/g, ""));
return url.protocol === "https:" && url.hostname === "chatgpt.com" && !url.username && !url.password && (!url.port || url.port === "443");
}
catch {
return false;
}
}
const purpose = (h: Headers) => [h.get("sec-purpose") || h.get("purpose"), h.get("rsc") === "1" ? "rsc" : ""].filter(Boolean).join(";")
// Never put the optional server key in browser code. Only forward an IP from
// your hosting provider's trusted connection metadata, not arbitrary headers.
const sendBot = async (body: Record<string, unknown>, key: string | undefined) => {
const response = await fetch("https://aiola.app/api/bot-crawl-ingest", {
method: "POST", headers: { "Content-Type": "application/json", ...(key ? { "X-Aiola-Server-Key": key } : {}) },
body: JSON.stringify({ tracking_id: "YOUR_TRACKING_ID", ...body }),
})
if (!response.ok) throw new Error("Aiola crawler capture failed: " + response.status)
}
export function middleware(req: NextRequest, ev: NextFetchEvent) {
const h = new Headers(req.headers), request_id = crypto.randomUUID()
h.set("x-aiola-request-id", request_id)
h.set("x-aiola-path", req.nextUrl.pathname)
h.set("x-aiola-method", req.method)
const ua = h.get("user-agent") || "", signature_agent = h.get("signature-agent") || ""
if (process.env.AIOLA_FINAL_STATUS_CAPTURE !== "1" && (BOT_RE.test(ua) || isChatGPTAgent(signature_agent)) && !ASSET_RE.test(req.nextUrl.pathname)) {
ev.waitUntil(sendBot({ request_id, url_path: req.nextUrl.pathname, url_host: req.nextUrl.hostname,
ua, signature_agent, status: 0, http_method: req.method, request_purpose: purpose(h),
is_test: h.get("x-aiola-test") === "1",
}, process.env.AIOLA_BOT_SERVER_KEY).catch(error => console.error(error)))
}
return NextResponse.next({ request: { headers: h } })
}
export const config = { matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"] }
// lib/aiola-response.ts — separate file, for Route Handlers owning a Response.
// This supplements the middleware fallback using the SAME request ID. It does
// not cover Server Components; use a hosting response hook for those.
import { after } from "next/server"
import type { NextRequest } from "next/server"
// aiola-bot-tracking v3
const BOT_RE = /(?:(?:^|[\s;(])(?:ChatGPT-Agent)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:ChatGPT-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:OAI-SearchBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:OAI-AdsBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:GPTBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Claude-Agent)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Claude-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Claude-SearchBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:ClaudeBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:anthropic-ai)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Perplexity-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:PerplexityBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:MistralAI-Training)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:MistralAI-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:MistralAI-Index)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:xAI-SearchBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Grok-DeepSearch)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:GrokBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:xAI-Web-Crawler)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:xAI-Grok)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:xAI-Bot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Google-GeminiNotebook|Google-NotebookLM)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Google-Read-Aloud)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Google-Agent|GoogleAgent)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Google-InspectionTool)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Google-CloudVertexBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:GoogleOther)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Googlebot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Applebot(?!-Extended))(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Copilot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:BingBot|bingbot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:msnbot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:NovaAct)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Amzn-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Amzn-SearchBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Amazonbot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:TikTokSpider)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Bytespider)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Doubaobot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Meta-ExternalFetcher|meta-externalfetcher)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:meta-webindexer)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:meta-externalads)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:facebookexternalhit)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Meta-ExternalAgent)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:CCBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:cohere-ai|cohere-training-data-crawler)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:DuckAssistBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:ExaSearchBot(?:-Images)?)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:ExaUser)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:YouBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Kimi-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Kimi-SearchBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:KimiBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Qwen-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:QwenBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:TongyiBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:AliyunBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Baiduspider)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:ERNIEBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:YiyanBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:ChatGLM-Spider)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:DeepSeekBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:AI2Bot\-Dolma)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:AI2Bot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Diffbot-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Diffbot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:FirecrawlAgent)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Timpibot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:ImagesiftBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:LinerBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:QualifiedBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:YandexAdditional(Bot)?)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:YandexBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Kagibot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:DuckDuckBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:SeznamBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:PetalBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Qwantbot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:AdsBot-Google[-A-Za-z]*)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Storebot\-Google)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Google\-Safety)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:APIs\-Google)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:FeedFetcher\-Google)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Google\-Site\-Verification)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:AdIdxBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:BingPreview)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:MicrosoftPreview)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:BingVideoPreview)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:yisouspider)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:iTMS)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Yandex[A-Za-z]*)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Sogou[A-Za-z]*)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Exabot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Exalead)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Twitterbot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:LinkedInBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:WhatsApp(?=\/))(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:TelegramBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Discordbot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Mediapartners[-A-Za-z]*)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:SemrushBot[-A-Za-z]*)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Semrush[A-Za-z]*)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:AhrefsBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:MJ12bot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:DotBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Chrome\-Lighthouse)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Lighthouse)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:PageSpeed)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:GTmetrix)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Pingdom)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:UptimeRobot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:HeadlessChrome)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:PhantomJS)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Cloudflare\-AI\-Search\-External)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Omgilibot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Omgili)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Webzio\-Extended)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:AmazonBuyForMe)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:FacebookBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Kimi-Agent)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Liner-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Manus-User)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:HarkBot)(?![A-Za-z0-9_-]))|(?:(?:^|[\s;(])(?:Shap-User)(?![A-Za-z0-9_-]))|(?:^|[\s;(])((?!CUBOT(?:[/\s;)]|$))[A-Za-z0-9_-]*(?:bot|crawler|spider|slurp)[A-Za-z0-9_-]*|Google-Extended)(?=[/\s;)]|$)/i
const ASSET_RE = /\.(png|jpe?g|gif|webp|avif|svg|ico|css|js|mjs|map|woff2?|ttf|otf|eot|webmanifest|mp4|webm)$/i
const isChatGPTAgent = function isChatGPTAgent(value = "") {
try {
const url = new URL(value.trim().replace(/^"|"$/g, ""));
return url.protocol === "https:" && url.hostname === "chatgpt.com" && !url.username && !url.password && (!url.port || url.port === "443");
}
catch {
return false;
}
}
const purpose = (h: Headers) => [h.get("sec-purpose") || h.get("purpose"), h.get("rsc") === "1" ? "rsc" : ""].filter(Boolean).join(";")
// Never put the optional server key in browser code. Only forward an IP from
// your hosting provider's trusted connection metadata, not arbitrary headers.
const sendBot = async (body: Record<string, unknown>, key: string | undefined) => {
const response = await fetch("https://aiola.app/api/bot-crawl-ingest", {
method: "POST", headers: { "Content-Type": "application/json", ...(key ? { "X-Aiola-Server-Key": key } : {}) },
body: JSON.stringify({ tracking_id: "YOUR_TRACKING_ID", ...body }),
})
if (!response.ok) throw new Error("Aiola crawler capture failed: " + response.status)
}
export function withAiolaBotCapture(handler: (req: NextRequest) => Response | Promise<Response>) {
return async function captured(req: NextRequest) {
const response = await handler(req)
const h = req.headers, url = new URL(req.url), ua = h.get("user-agent") || ""
const signature_agent = h.get("signature-agent") || ""
if ((BOT_RE.test(ua) || isChatGPTAgent(signature_agent)) && !ASSET_RE.test(url.pathname)) {
const request_id = h.get("x-aiola-request-id") || crypto.randomUUID()
after(() => sendBot({ request_id, url_path: url.pathname, url_host: url.hostname,
ua, signature_agent, status: response.status, http_method: req.method,
request_purpose: purpose(h), is_test: h.get("x-aiola-test") === "1",
}, process.env.AIOLA_BOT_SERVER_KEY).catch(error => console.error(error)))
}
return response
}
}
// In an existing Route Handler: export const GET = withAiolaBotCapture(yourHandler)
// after() schedules work; it does not discover a Server Component's status.
// app/not-found.tsx — merge this logic into the EXISTING component, preserve UI.
// Read headers BEFORE after(). Reuse the middleware request ID. A streamed
// not-found page can return HTTP 200: report semantics separately as below.
// const h = await headers()
// after(() => sendBot({ request_id: h.get("x-aiola-request-id"),
// url_path: h.get("x-aiola-path"), url_host: (h.get("host") || "").split(":")[0],
// ua: h.get("user-agent"), signature_agent: h.get("signature-agent"),
// status: 0, not_found: true, http_method: h.get("x-aiola-method") || "GET",
// request_purpose: purpose(h), is_test: h.get("x-aiola-test") === "1",
// }, process.env.AIOLA_BOT_SERVER_KEY))
// Apply the bot/asset guard. Use only when middleware capture is active; a
// complete response hook is authoritative. Query-time ID dedupe is required.