Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/html-content-type-case-insensitive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@dualmark/cloudflare": patch
"@dualmark/deno": patch
"@dualmark/fastly": patch
"@dualmark/netlify": patch
"@dualmark/vercel": patch
---

Match the HTML `Content-Type` case-insensitively when deciding to inject `Vary: Accept` and the `Link` alternate header.

HTTP media types are case-insensitive (RFC 7231 §3.1.1.1), but the cloudflare, deno, fastly, netlify and vercel adapters gated markdown-twin advertisement on a case-sensitive `contentType.includes("text/html")`. An upstream that emitted `Content-Type: Text/HTML` (or any non-lowercase spelling) skipped the block, so the negotiable HTML response was returned without `Vary: Accept` — risking a shared cache serving HTML to a markdown client — and without the `Link rel="alternate"` twin. The check now lowercases the header first, matching the astro, nuxt and sveltekit adapters which already did this.
2 changes: 1 addition & 1 deletion packages/cloudflare/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export function createAEOWorker<Env extends MinimalEnv = MinimalEnv>(
if (
!shouldSkip(pathname, skipPrefixes, skipExtensions) &&
!pathname.endsWith(".md") &&
upstreamResponse.headers.get("content-type")?.includes("text/html")
upstreamResponse.headers.get("content-type")?.toLowerCase().includes("text/html")
) {
const newHeaders = new Headers(upstreamResponse.headers);
const vary = newHeaders.get("Vary");
Expand Down
18 changes: 18 additions & 0 deletions packages/cloudflare/test/worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,22 @@ describe("createAEOWorker — Link header injection", () => {
expect((res.headers.get("vary") ?? "").toLowerCase()).toContain("accept");
expect(res.headers.get("link")).toBeNull();
});

it("injects Link and Vary on HTML with a mixed-case Content-Type", async () => {
const env: TestEnv = { ASSETS: makeAssets({}) };
const worker = createAEOWorker({
upstream: makeUpstream(
() => new Response("<html></html>", { headers: { "Content-Type": "Text/HTML; charset=UTF-8" } }),
),
});
const req = new Request("https://acme.test/page", {
headers: {
"user-agent": "Mozilla/5.0 Chrome/130",
accept: "text/html,*/*;q=0.8",
},
});
const res = await worker.fetch(req, env, makeCtx());
expect(res.headers.get("link")).toContain('rel="alternate"');
expect((res.headers.get("vary") ?? "").toLowerCase()).toContain("accept");
});
});
2 changes: 1 addition & 1 deletion packages/deno/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export function createAEOHandler(options: CreateAEOHandlerOptions): AEODenoHandl
if (
!shouldSkip(pathname, skipPrefixes, skipExtensions) &&
!pathname.endsWith(".md") &&
upstreamResponse.headers.get("content-type")?.includes("text/html")
upstreamResponse.headers.get("content-type")?.toLowerCase().includes("text/html")
) {
const newHeaders = new Headers(upstreamResponse.headers);
const vary = newHeaders.get("Vary");
Expand Down
9 changes: 9 additions & 0 deletions packages/deno/test/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,15 @@ describe("createAEOHandler — Link header injection", () => {
const res = await handler(new Request("https://acme.test/data"), makeInfo());
expect(res.headers.get("link")).toBeNull();
});

it("injects Link and Vary on HTML with a mixed-case Content-Type", async () => {
const upstream: DenoUpstreamHandler = async () =>
new Response("<html></html>", { headers: { "Content-Type": "Text/HTML; charset=UTF-8" } });
const handler = createAEOHandler({ upstream });
const res = await handler(new Request("https://acme.test/page"), makeInfo());
expect(res.headers.get("link")).toContain('rel="alternate"');
expect((res.headers.get("vary") ?? "").toLowerCase()).toContain("accept");
});
});

describe("createAEOHandler — direct .md handling", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/fastly/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export function createAEORequestHandler(options: CreateAEOFastlyOptions): AEOFas
if (
!shouldSkip(pathname, skipPrefixes, skipExtensions) &&
!pathname.endsWith(".md") &&
upstreamResponse.headers.get("content-type")?.includes("text/html")
upstreamResponse.headers.get("content-type")?.toLowerCase().includes("text/html")
) {
const newHeaders = new Headers(upstreamResponse.headers);
const vary = newHeaders.get("Vary");
Expand Down
24 changes: 24 additions & 0 deletions packages/fastly/test/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,30 @@ describe("Fastly Adapter", () => {
expect(fetch).toHaveBeenCalledWith(req, { backend: "default_backend" });
});

it("injects Link and Vary on HTML with a mixed-case Content-Type", async () => {
vi.stubGlobal(
"fetch",
vi.fn(
async () =>
new Response("<html></html>", {
status: 200,
headers: { "Content-Type": "Text/HTML; charset=UTF-8" },
}),
),
);
const handler = createAEORequestHandler({ backend: "default_backend" });
const req = new Request("https://acme.test/page", {
headers: {
"user-agent": "Mozilla/5.0 Chrome/130",
accept: "text/html,*/*;q=0.8",
},
});
const res = await handler(req);
expect(res.status).toBe(200);
expect(res.headers.get("link")).toContain('rel="alternate"');
expect(res.headers.get("vary")).toContain("Accept");
});

it("handles missing .md (cache miss) for bot — falls to upstream HTML", async () => {
const handler = createAEORequestHandler({
backend: "default_backend",
Expand Down
2 changes: 1 addition & 1 deletion packages/netlify/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export function createAEOWorker(
if (
!shouldSkip(pathname, skipPrefixes, skipExtensions) &&
!pathname.endsWith(".md") &&
originResponse.headers.get("content-type")?.includes("text/html")
originResponse.headers.get("content-type")?.toLowerCase().includes("text/html")
) {
const newHeaders = new Headers(originResponse.headers);
const vary = newHeaders.get("Vary");
Expand Down
19 changes: 19 additions & 0 deletions packages/netlify/test/worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,23 @@ describe("createAEOWorker — default options", () => {
expect(res.status).toBe(301);
expect(res.headers.get("location")).toBe("https://acme.test/blog?ref=twitter");
});

it("injects Link and Vary on HTML with a mixed-case Content-Type", async () => {
const assets = makeAssets({});
const worker = createAEOWorker({ assets });
const req = new Request("https://acme.test/page", {
headers: {
"user-agent": "Mozilla/5.0 Chrome/130",
accept: "text/html,*/*;q=0.8",
},
});
const res = await worker(
req,
makeContext(
() => new Response("<html></html>", { headers: { "Content-Type": "Text/HTML; charset=UTF-8" } }),
),
);
expect(res.headers.get("link")).toContain('rel="alternate"');
expect((res.headers.get("vary") ?? "").toLowerCase()).toContain("accept");
});
});
2 changes: 1 addition & 1 deletion packages/vercel/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export function createAEOMiddleware(
const ct = upstreamResponse.headers.get("content-type");
// Passthrough responses (e.g. NextResponse.next()) have no content-type yet —
// always inject. For concrete responses, only inject on text/html.
if (!ct || ct.includes("text/html")) {
if (!ct || ct.toLowerCase().includes("text/html")) {
const mdPath = toMarkdownPath(pathname);
try {
// Fast path: mutate headers in-place (works for NextResponse.next() and
Expand Down
21 changes: 21 additions & 0 deletions packages/vercel/test/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,27 @@ describe("createAEOMiddleware — Link header injection", () => {
expect((res.headers.get("vary") ?? "").toLowerCase()).toContain("accept");
expect(res.headers.get("link")).toBeNull();
});

it("injects Link and Vary on HTML with a mixed-case Content-Type", async () => {
const middleware = createAEOMiddleware({
upstream: makeUpstream(
() =>
new Response("<html></html>", {
headers: { "Content-Type": "Text/HTML; charset=UTF-8" },
}),
),
fetchAsset,
});
const req = new Request("https://acme.test/page", {
headers: {
"user-agent": "Mozilla/5.0 Chrome/130",
accept: "text/html,*/*;q=0.8",
},
});
const res = await middleware(req, makeCtx());
expect(res.headers.get("link")).toContain('rel="alternate"');
expect((res.headers.get("vary") ?? "").toLowerCase()).toContain("accept");
});
});

describe("createAEOMiddleware — edge cases", () => {
Expand Down
Loading