-
Notifications
You must be signed in to change notification settings - Fork 10
feat: AI agent redirect middleware + llms-txt plugin #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
katieschilling
wants to merge
1
commit into
main
Choose a base branch
from
katieschilling/ai-agent-redirect
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { next } from "@vercel/edge"; | ||
|
|
||
| // Skip middleware for static assets | ||
| const STATIC_EXTENSIONS = | ||
| /\.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf|eot|json|xml|txt|md|map|webp|avif)$/i; | ||
|
|
||
| // Known AI agent and bot User-Agent patterns | ||
| const AGENT_UA_PATTERNS = [ | ||
| /\bClaudeBot\b/i, | ||
| /\bChatGPT-User\b/i, | ||
| /\bGPTBot\b/i, | ||
| /\bGoogle-Extended\b/i, | ||
| /\bPerplexityBot\b/i, | ||
| /\bCohere-AI\b/i, | ||
| /\bAnthropic\b/i, | ||
| /\bClaude\b/i, | ||
| /\bOAI-SearchBot\b/i, | ||
| /\bYouBot\b/i, | ||
| /\bAI2Bot\b/i, | ||
| /\bApplebot-Extended\b/i, | ||
| /\bMeta-ExternalAgent\b/i, | ||
| /\bMeta-ExternalFetcher\b/i, | ||
| /\bFirecrawl\b/i, | ||
| /\bJinaBot\b/i, | ||
| ]; | ||
|
|
||
| export const config = { | ||
| matcher: "/blog/:path*", | ||
| }; | ||
|
|
||
| function isAgent(request) { | ||
| const accept = request.headers.get("accept") || ""; | ||
| if (accept.includes("text/markdown")) { | ||
| return true; | ||
| } | ||
|
|
||
| const ua = request.headers.get("user-agent") || ""; | ||
| return AGENT_UA_PATTERNS.some((pattern) => pattern.test(ua)); | ||
| } | ||
|
|
||
| function rewriteToMarkdown(pathname, requestUrl) { | ||
| // Rewrite HTML URL to its .md equivalent: | ||
| // /blog/some-post/ -> /blog/some-post.md | ||
| // /blog/tags/foo/ -> /blog/tags/foo.md | ||
| // /blog/ -> /blog/index.md | ||
| const subpath = pathname.replace(/^\/blog\/?/, "").replace(/\/+$/, ""); | ||
| let mdPath; | ||
| if (!subpath) { | ||
| mdPath = "/blog/index.md"; | ||
| } else { | ||
| mdPath = `/blog/${subpath}.md`; | ||
| } | ||
|
|
||
| return new Response(null, { | ||
| status: 307, | ||
| headers: { | ||
| Location: new URL(mdPath, requestUrl).toString(), | ||
| Vary: "Accept, User-Agent", | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| export default function middleware(request) { | ||
| const url = new URL(request.url); | ||
| const pathname = url.pathname; | ||
|
|
||
| // Skip static assets | ||
| if (STATIC_EXTENSIONS.test(pathname)) { | ||
| return next(); | ||
| } | ||
|
|
||
| if (isAgent(request)) { | ||
| return rewriteToMarkdown(pathname, request.url); | ||
| } | ||
|
|
||
| // For normal HTML requests, add Link header pointing to llms.txt for discovery | ||
| return next({ | ||
| headers: { | ||
| Link: '</blog/llms.txt>; rel="alternate"; type="text/plain"', | ||
| Vary: "Accept, User-Agent", | ||
| }, | ||
| }); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Middleware redirects non-post blog paths to likely nonexistent files
Medium Severity
The
config.matcherof"/blog/:path*"causes the middleware to redirect AI agents on all blog sub-paths — including tag listing pages (/blog/tags/), specific tag pages (/blog/tags/foo/), and pagination pages (/blog/page/2/) — to.mdequivalents like/blog/tags.mdor/blog/page/2.md. The PR description states the plugin generates "per-post.mdfiles", so these non-post paths likely have no corresponding.mdfile, resulting in AI agents receiving a 307 redirect followed by a 404.Additional Locations (1)
middleware.mjs#L40-L52