Markdown Copies Of Pages For Agents Without Hurting SEO
Jake MarshIn this post
When I made this blog readable by agents, every post got a Markdown copy at its URL plus .md, and the normal URL answers with Markdown when a client sends Accept: text/markdown. Each HTML page also links its copy with <link rel="alternate" type="text/markdown">, so agents can find it.
So can search engines. Now every page exists twice with the same text, and the copy has no <head> to put a canonical tag in. The fix is three response headers on the copy. Getting them to reach clients took more care than I expected, especially in Next.js.
The Three Headers#
Here’s a Markdown copy on this blog:
$ curl -sI https://deallocatedobjects.com/posts/the-art-of-the-reveal.md
HTTP/2 200
content-type: text/markdown; charset=utf-8
vary: Accept
link: </llms.txt>; rel="describedby"; type="text/plain"; title="Guide for AI agents", </sitemap.xml>; rel="sitemap"; type="application/xml", </feed>; rel="alternate"; type="application/atom+xml"
link: <https://deallocatedobjects.com/posts/the-art-of-the-reveal>; rel="canonical"
x-robots-tag: noindex, followLink: <...>; rel="canonical" says the HTML page is the real one. Google supports the canonical as an HTTP header for exactly this case, documents that aren’t HTML, in its guide to duplicate URLs. Use an absolute URL there, as the same page asks. It also says to pick one place per URL, header or <link> element, since giving both invites them to disagree. A Markdown file has no element, so the header is the only choice anyway.
X-Robots-Tag: noindex, follow keeps the copy out of results. Google’s robots meta tag docs say the header form is how you noindex non-HTML resources. follow is already Google’s default (it follows links unless told nofollow); I spell it out so the intent is obvious to anyone reading the headers.
Google’s duplicate-URL guide does say it doesn’t recommend noindex for choosing a canonical, “because it will completely block the page from Search.” For the copy, blocking it completely is what I want. The canonical header still names the page that should rank.
Vary: Accept matters wherever one URL answers with HTML or Markdown depending on the request. Without it, a cache in the middle can store the Markdown and hand it to the next browser (or the reverse). The .md URL itself never negotiates, but sending Vary: Accept there is harmless, and on this blog it’s on every response.
One more thing: don’t disallow *.md in robots.txt to keep the copies out. Google only sees X-Robots-Tag and the canonical when it can fetch the URL, and its docs say blocked URLs have their rules “ignored.”
In nginx#
The blog is static files behind nginx. A map turns the Accept header into a suffix, the post location rewrites to the .md file when it’s set, and the .md location adds the two headers:
location ~ ^/posts/[^/.]+$ {
if ($deallocated_md) { rewrite ^ $uri.md last; }
try_files $uri.html =404;
}
location ~ ^(?<md_page>/.*?)(?:index)?\.md$ {
types { }
default_type text/markdown;
include snippets/deallocatedobjects-headers.conf;
add_header Link "<https://deallocatedobjects.com$md_page>; rel=\"canonical\"" always;
add_header X-Robots-Tag "noindex, follow" always;
try_files $uri =404;
}The include line is the nginx gotcha. The shared snippet holds the security headers, Vary: Accept and the sitewide Link to llms.txt, and it’s included at the server level. But per the nginx docs, add_header directives are inherited “if and only if there are no add_header directives defined on the current level.” The moment this location adds its own canonical, every server-level header disappears from .md responses, so the location includes the snippet again.
That’s why the curl output above has two link: lines. nginx sends each add_header as its own line, and that’s fine: RFC 8288 defines Link as a comma-separated list, so two lines mean the same thing as one line with both values.
In Next.js#
In a Next.js app, a proxy (Next 16’s name for middleware) rewrites /guides/foo.md, or /guides/foo with Accept: text/markdown and no text/html, to a route handler that returns the copy. This is from Rookiefeed:
export function markdownResponse(body: string, canonicalPath: string) {
return new Response(body, {
headers: {
"Content-Type": "text/markdown; charset=utf-8",
Vary: "Accept",
Link: `<${siteUrl(canonicalPath)}>; rel="canonical"`,
"X-Robots-Tag": "noindex, follow",
"Cache-Control": "public, max-age=600",
},
});
}The same app also sent a sitewide Link header from next.config.ts, pointing agents at llms.txt and the sitemap:
async headers() {
const discovery = '</llms.txt>; rel="service-doc"; type="text/plain", </sitemap.xml>; rel="sitemap"; type="application/xml"';
return [
{ source: "/:path*", headers: [{ key: "Link", value: discovery }, ...SECURITY_HEADERS] },
];
},With both in place, the .md responses came back with the discovery Link and no canonical. The route’s Link never reached the client.
How Next Combines Them#
The headers() docs only cover two config rules setting the same key: “the last header key will override the first.” They don’t say what happens when a config rule and a route handler set the same header, so I read the code.
Config headers are applied first. The router collects every matching rule into one object (resHeaders[key] = value, so a later rule replaces an earlier one) and calls res.setHeader for each before your route runs. Then send-response.ts copies your route’s Response headers onto that same response, like this (16.4.5, trimmed):
const headersWithMultipleValuesAllowed = [
'set-cookie',
'www-authenticate',
'proxy-authenticate',
'vary'
];
response.headers.forEach((value, name)=>{
// only append the header if it is either not present in the outbound response
// or if the header supports multiple values
const isHeaderPresent = typeof res.getHeader(name) !== 'undefined';
if (headersWithMultipleValuesAllowed.includes(name.toLowerCase()) || !isHeaderPresent) {
res.appendHeader(name, value);
}
});So for any header your config already set, the route’s value is skipped without a warning. Four names are exceptions and get appended. Link isn’t one of them, so the config’s Link stays and the route’s canonical is dropped. Vary is one of them, which is why the route’s Vary: Accept makes it through:
$ curl -sI https://rookiefeed.com/guides/how-much-does-mc-authority-cost.md
HTTP/2 200
content-type: text/markdown; charset=utf-8
vary: rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch, Accept, Accept-Encoding
link: <https://rookiefeed.com/guides/how-much-does-mc-authority-cost>; rel="canonical"
x-robots-tag: noindex, followThat’s the output after the fix, which is to keep the config rule off the .md paths with a negative lookahead in source:
{ source: "/:path*", headers: SECURITY_HEADERS },
// Not on .md twins: a config Link header replaces the route's own rel="canonical".
{ source: "/:path((?!.*\\.md$).*)", headers: [{ key: "Link", value: discovery }] },If you still want the discovery links on the copies, have the route send them itself, in the same header as the canonical. Runheed does that:
$ curl -sI https://runheed.com/errors/econnreset.md
link: <https://runheed.com/errors/econnreset>; rel="canonical", </llms.txt>; rel="llms-txt", </openapi.json>; rel="describedby"; type="application/json", </SKILL.md>; rel="describedby"; type="text/markdown"
x-robots-tag: noindex, followRookiefeed’s config test now checks that the discovery rule matches /guides/how-much-does-mc-authority-cost and doesn’t match the .md version, so the exclusion can’t quietly go away.
Vary On The HTML Side#
Vary has its own trap, on the HTML page rather than the copy. When /errors/econnreset negotiates, the HTML response needs Vary: Accept too, and setting it in the proxy isn’t enough. Next’s app-page handler runs after the proxy and calls res.setHeader('Vary', 'rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch'), and setHeader replaces whatever was there.
How much that matters depends on your cache headers. Rookiefeed’s pages are sent private, no-store, so nothing between the app and the browser keeps a copy. Runheed’s are s-maxage=31536000, which invites a shared cache to store them, so Runheed patches ServerResponse.prototype.setHeader and appendHeader from instrumentation.ts to add Accept to Vary on the negotiated paths, each token once:
$ curl -sI https://runheed.com/errors/econnreset
content-type: text/html; charset=utf-8
link: <https://runheed.com/errors/econnreset.md>; rel="alternate"; type="text/markdown", </llms.txt>; rel="llms-txt", </openapi.json>; rel="describedby"; type="application/json", </SKILL.md>; rel="describedby"; type="text/markdown"
vary: rsc, next-router-state-tree, next-router-prefetch, next-router-segment-prefetch, Accept, Accept-EncodingChecking Your Own#
Whatever the stack, the check is three requests:
curl -sI https://example.com/page.md
curl -sI -H "Accept: text/markdown" https://example.com/page
curl -sI https://example.com/pageThe first two should carry the absolute canonical Link, X-Robots-Tag: noindex, follow and Accept in Vary. The third should be HTML, with Accept in Vary if the URL negotiates and your responses can be cached. Run them against the deployed site, since that’s the response where config, proxy and route headers all meet.