Skip to content

Commit 0a4b35a

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 f01d281 commit 0a4b35a

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
@@ -1001,7 +1001,9 @@ function honoWrapper(compiler, options = {}, usePlugin = false) {
10011001

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

test/middleware.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7033,6 +7033,33 @@ describe.each([
70337033
);
70347034
});
70357035

7036+
if (name === "hono") {
7037+
it("replaces headers left by earlier middleware on the SSE handshake", async () => {
7038+
const compiler = getCompiler(webpackConfig);
7039+
[server, req, instance] = await frameworkFactory(
7040+
name,
7041+
framework,
7042+
compiler,
7043+
{ hot: true },
7044+
{
7045+
setupMiddlewares: (middlewares) => [
7046+
async (c, next) => {
7047+
c.res.headers.set("Cache-Control", "max-age=100");
7048+
await next();
7049+
},
7050+
...middlewares,
7051+
],
7052+
},
7053+
);
7054+
7055+
const res = await readSseHandshake(req.get("/__webpack_hmr"));
7056+
7057+
// writeHead must replace the pre-set value, not append to it
7058+
// ("max-age=100, no-cache, no-transform").
7059+
expect(res.headers["cache-control"]).toBe("no-cache, no-transform");
7060+
});
7061+
}
7062+
70367063
it("routes SSE handshake errors to the framework error handler", async () => {
70377064
const compiler = getCompiler(webpackConfig);
70387065
[server, req, instance] = await frameworkFactory(

0 commit comments

Comments
 (0)