Skip to content

Commit

Permalink
fix: allow adding custom Vary to static assets handler (#2835)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniluk4000 authored Nov 5, 2024
1 parent e728f9b commit 11ec5ba
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/runtime/internal/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getResponseHeader,
removeResponseHeader,
setResponseHeader,
appendResponseHeader,
setResponseStatus,
} from "h3";
import type { PublicAsset } from "nitropack/types";
Expand Down Expand Up @@ -49,7 +50,7 @@ export default eventHandler((event) => {
"",
];
if (encodings.length > 1) {
setResponseHeader(event, "Vary", "Accept-Encoding");
appendResponseHeader(event, "Vary", "Accept-Encoding");
}

for (const encoding of encodings) {
Expand Down
10 changes: 10 additions & 0 deletions test/fixture/plugins/vary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default defineNitroPlugin((app) => {
app.hooks.hook("request", (event) => {
if (event.path.endsWith(".css")) {
setResponseHeader(event, "Vary", "Origin");
}
if (event.path.endsWith(".js")) {
setResponseHeader(event, "Vary", ["Origin"]);
}
});
});
Empty file added test/fixture/public/foo.css
Empty file.
1 change: 1 addition & 0 deletions test/fixture/public/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const hello = "world";
2 changes: 2 additions & 0 deletions test/presets/cloudflare-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ describe.skipIf(isWindows)("nitro:preset:cloudflare-pages", async () => {
"/_swagger",
"/_unignored.txt",
"/favicon.ico",
"/foo.css",
"/foo.js",
"/json-string",
"/prerender",
"/prerender-custom",
Expand Down
37 changes: 37 additions & 0 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,43 @@ export function testNitro(
expect(data).toMatch("<h1 >Hello JSX!</h1>");
});

it.runIf(ctx.nitro?.options.serveStatic)(
"handles custom Vary header",
async () => {
let headers = (
await callHandler({
url: "/foo.css",
headers: { "Accept-Encoding": "gzip" },
})
).headers;
if (headers["vary"])
expect(
headers["vary"].includes("Origin") &&
headers["vary"].includes("Accept-Encoding")
).toBeTruthy();

headers = (
await callHandler({
url: "/foo.css",
headers: { "Accept-Encoding": "" },
})
).headers;
if (headers["vary"]) expect(headers["vary"]).toBe("Origin");

headers = (
await callHandler({
url: "/foo.js",
headers: { "Accept-Encoding": "gzip" },
})
).headers;
if (headers["vary"])
expect(
headers["vary"].includes("Origin") &&
headers["vary"].includes("Accept-Encoding")
).toBeTruthy();
}
);

it("handles route rules - headers", async () => {
const { headers } = await callHandler({ url: "/rules/headers" });
expect(headers["cache-control"]).toBe("s-maxage=60");
Expand Down

0 comments on commit 11ec5ba

Please sign in to comment.