Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow adding custom Vary to static assets handler #2835

Merged
merged 7 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
daniluk4000 marked this conversation as resolved.
Show resolved Hide resolved
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