Skip to content

Commit

Permalink
refactor: use appendResponseHeader, fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
daniluk4000 committed Nov 4, 2024
1 parent 0f9f018 commit 0e49e1a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
7 changes: 2 additions & 5 deletions 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,11 +50,7 @@ export default eventHandler((event) => {
"",
];
if (encodings.length > 1) {
const varyHeader = getResponseHeader(event, 'Vary');

if(!varyHeader) setResponseHeader(event, "Vary", "Accept-Encoding");
else if (typeof varyHeader === "string" && !varyHeader.includes('Accept-Encoding')) setResponseHeader(event, "Vary", `${ varyHeader }, Accept-Encoding`);
else if (Array.isArray(varyHeader) && !varyHeader.includes('Accept-Encoding')) setResponseHeader(event, "Vary", [...varyHeader, 'Accept-Encoding']);
appendResponseHeader(event, "Vary", "Accept-Encoding");
}

for (const encoding of encodings) {
Expand Down
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";
50 changes: 32 additions & 18 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,24 +310,38 @@ export function testNitro(
expect(data).toMatch("<h1 >Hello JSX!</h1>");
});

it.skipIf(ctx.preset.startsWith('cloudflare') || ctx.preset === 'vercel-edge')(
"handles custom Vary header",
async () => {
let headers = (await callHandler({ url: "/foo.css", headers: { "Accept-Encoding": "gzip" } })).headers;
if (headers["vary"]) expect(headers["vary"]).toBe("Origin, Accept-Encoding");

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;

//For some reason, sometimes there are no space between Origin and Accept-Encoding when passing headers as an array
if (headers["vary"]) {
expect(headers["vary"].includes('Origin')).toBeTruthy();
expect(headers["vary"].includes('Accept-Encoding')).toBeTruthy();
}
}
);
it.skipIf(
ctx.preset.startsWith("cloudflare") || ctx.preset === "vercel-edge"
)("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" });
Expand Down

0 comments on commit 0e49e1a

Please sign in to comment.