hono icon indicating copy to clipboard operation
hono copied to clipboard

Ensure mutable headers with `hono/timing` middleware

Open timkelty opened this issue 3 months ago • 1 comments

What is the feature you are proposing?

Whenever I use hono/timing, I end up having to wrap it to avoid TypeError: Can't modify immutable headers..

import type { MiddlewareHandler } from "hono";
import { timing } from "hono/timing";

type TimingConfig = Parameters<typeof timing>[0];

export default (config?: TimingConfig): MiddlewareHandler => {
	return async (c, next) => {
		return await timing(config)(c, async () => {
			await next();

			// Ensure the response is mutable
			if (c.res instanceof Response) {
				c.res = new Response(c.res.body, c.res);
			}
		});
	};
};

Because that middleware is likely to be book-ending the entire middleware chain, it seems like a common pitfall. Think it would be worth including in the middleware itself?

If so, I'm happy to PR.

timkelty avatar Oct 09 '25 12:10 timkelty

Hi @timkelty!

I want to know what the issue is with the TypeError: Can't modify immutable headers. error you face. Can you provide examples for reprocue it?

yusukebe avatar Oct 22 '25 00:10 yusukebe