Skip to content

Commit 471cc0d

Browse files
committed
fix(static): honor zero encoding quality
1 parent 645e138 commit 471cc0d

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

src/runtime/internal/static.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ export default defineHandler((event) => {
2121
const encodings = [
2222
...encodingHeader
2323
.split(",")
24-
.map((e) => EncodingMap[e.trim().split(";")[0].trim() as keyof typeof EncodingMap])
24+
.map((entry) => {
25+
const [name, ...parameters] = entry.split(";");
26+
const quality = parameters
27+
.map((parameter) => parameter.trim().split("="))
28+
.find(([key]) => key.toLowerCase() === "q")?.[1];
29+
return quality !== undefined && Number(quality) === 0
30+
? undefined
31+
: EncodingMap[name.trim() as keyof typeof EncodingMap];
32+
})
2533
.filter(Boolean)
2634
.sort(),
2735
"",

test/unit/static-middleware.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,34 @@ describe("runtime static middleware", () => {
8585
expect(readAsset).toHaveBeenCalledWith("/foo.css.gz");
8686
expect(event.res.headers.get("Content-Encoding")).toBe("gzip");
8787
});
88+
89+
it("does not match compressed assets with zero quality", async () => {
90+
getAsset.mockImplementation((id: string) => {
91+
if (id === "/foo.css.gz") {
92+
return {
93+
etag: '"compressed"',
94+
mtime: "2024-01-01T00:00:00.000Z",
95+
size: 4,
96+
type: "text/css",
97+
encoding: "gzip",
98+
};
99+
}
100+
if (id === "/foo.css") {
101+
return {
102+
etag: '"plain"',
103+
mtime: "2024-01-01T00:00:00.000Z",
104+
size: 4,
105+
type: "text/css",
106+
};
107+
}
108+
return undefined;
109+
});
110+
readAsset.mockResolvedValue("body");
111+
const event = createEvent("/foo.css", "gzip; q=0.0");
112+
113+
await handler(event);
114+
115+
expect(readAsset).toHaveBeenCalledWith("/foo.css");
116+
expect(event.res.headers.get("Content-Encoding")).toBeNull();
117+
});
88118
});

0 commit comments

Comments
 (0)