Skip to content

Images binding: allow setting headers on .response()#6881

Open
Monark-Arkmon wants to merge 1 commit into
cloudflare:mainfrom
Monark-Arkmon:monark/images-response-headers
Open

Images binding: allow setting headers on .response()#6881
Monark-Arkmon wants to merge 1 commit into
cloudflare:mainfrom
Monark-Arkmon:monark/images-response-headers

Conversation

@Monark-Arkmon

@Monark-Arkmon Monark-Arkmon commented Jul 13, 2026

Copy link
Copy Markdown

response() now accepts an optional headers option, so you can set Cache-Control (or any other header) without manually rebuilding the Response:

result.response({
  headers: { "Cache-Control": "public, max-age=3600, stale-while-revalidate=86400" },
});

content-type is always taken from the transformed image and can't be overridden by a header you pass in.

Before: you had to intercept the response, spread its headers, and rebuild it just to add one header:

const result = await env.IMAGES.input(stream)
  .transform({ width: 800 })
  .output({ format: "image/webp" });

const response = result.response();
return new Response(response.body, {
  headers: {
    ...Object.fromEntries(response.headers),
    "Cache-Control": "public, max-age=3600, stale-while-revalidate=86400",
  },
});

After: pass the header straight to response():

const result = await env.IMAGES.input(stream)
  .transform({ width: 800 })
  .output({ format: "image/webp" });

return result.response({
  headers: { "Cache-Control": "public, max-age=3600, stale-while-revalidate=86400" },
});

This pairs nicely with Workers Cache, now that Cache-Control on the response is all you need to get a transform cached at the edge, it was a bit annoying to have to hand-roll headers just to set that one thing.

Implemented as a generic headers?: HeadersInit passthrough rather than a Cache-Control-specific option, since the underlying problem (copying headers manually) applies to any header, not just cache-control.

@Monark-Arkmon Monark-Arkmon requested review from a team as code owners July 13, 2026 16:26
@codspeed-hq

codspeed-hq Bot commented Jul 14, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 9.69%

⚡ 1 improved benchmark
✅ 71 untouched benchmarks
⏩ 129 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
request[RegExpBenchmark] 4.5 ms 4.1 ms +9.69%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing Monark-Arkmon:monark/images-response-headers (9c97ada) with main (03c396e)

Open in CodSpeed

Footnotes

  1. 129 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants