Skip to content

Commit 3f915df

Browse files
committed
fix(hono): set headers in the SSE writeHead shim instead of appending
writeHead semantics replace a header an earlier middleware may have left on the response — appending merged both values into one comma-joined header (e.g. "max-age=100, no-cache, no-transform" for Cache-Control, which intermediaries may treat as cacheable).
1 parent 3640860 commit 3f915df

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,9 @@ function honoWrapper(compiler, options = {}, usePlugin = false) {
10001000

10011001
if (headers) {
10021002
for (const name of Object.keys(headers)) {
1003-
context.res.headers.append(name, String(headers[name]));
1003+
// `set`, not `append`: writeHead replaces headers left by
1004+
// earlier middleware instead of comma-joining values.
1005+
context.res.headers.set(name, String(headers[name]));
10041006
}
10051007
}
10061008

test/middleware.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7010,6 +7010,33 @@ describe.each([
70107010
);
70117011
});
70127012

7013+
if (name === "hono") {
7014+
it("replaces headers left by earlier middleware on the SSE handshake", async () => {
7015+
const compiler = getCompiler(webpackConfig);
7016+
[server, req, instance] = await frameworkFactory(
7017+
name,
7018+
framework,
7019+
compiler,
7020+
{ hot: true },
7021+
{
7022+
setupMiddlewares: (middlewares) => [
7023+
async (c, next) => {
7024+
c.res.headers.set("Cache-Control", "max-age=100");
7025+
await next();
7026+
},
7027+
...middlewares,
7028+
],
7029+
},
7030+
);
7031+
7032+
const res = await readSseHandshake(req.get("/__webpack_hmr"));
7033+
7034+
// writeHead must replace the pre-set value, not append to it
7035+
// ("max-age=100, no-cache, no-transform").
7036+
expect(res.headers["cache-control"]).toBe("no-cache, no-transform");
7037+
});
7038+
}
7039+
70137040
it("routes SSE handshake errors to the framework error handler", async () => {
70147041
const compiler = getCompiler(webpackConfig);
70157042
[server, req, instance] = await frameworkFactory(

0 commit comments

Comments
 (0)