Skip to content

Commit cb2599c

Browse files
author
Paul Russell
committed
fix(content-negotiation) respect encoding file presence for Vary header
1 parent b2276b4 commit cb2599c

File tree

5 files changed

+171
-4
lines changed

5 files changed

+171
-4
lines changed

src/runtime/internal/static.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ export default eventHandler((event) => {
3636
withLeadingSlash(withoutTrailingSlash(parseURL(event.path).pathname))
3737
);
3838

39+
for (const encoding of Object.values(EncodingMap)) {
40+
if (getAsset(id + encoding)) {
41+
appendResponseHeader(event, "Vary", "Accept-Encoding");
42+
break;
43+
}
44+
}
45+
3946
let asset: PublicAsset | undefined;
4047

4148
const encodingHeader = String(
@@ -49,9 +56,6 @@ export default eventHandler((event) => {
4956
.sort(),
5057
"",
5158
];
52-
if (encodings.length > 1) {
53-
appendResponseHeader(event, "Vary", "Accept-Encoding");
54-
}
5559

5660
for (const encoding of encodings) {
5761
for (const _id of [id + encoding, joinURL(id, "index.html" + encoding)]) {

test/fixture/public/foo.css

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* This is a randomly generated CSS file.
3+
* It's intended to be around 100 lines long.
4+
* Please note: Some properties or values might not be practical or valid in a real-world scenario.
5+
*/
6+
7+
:root {
8+
--primary-color: #3498db;
9+
--secondary-color: #2ecc71;
10+
--text-color: #333;
11+
--background-color: #f8f8f8;
12+
--border-radius-small: 4px;
13+
--spacing-unit: 16px;
14+
}
15+
16+
body {
17+
font-family: 'Arial', sans-serif;
18+
line-height: 1.6;
19+
margin: 0;
20+
padding: var(--spacing-unit);
21+
background-color: var(--background-color);
22+
color: var(--text-color);
23+
overflow-x: hidden;
24+
}
25+
26+
h1, h2, h3, h4, h5, h6 {
27+
font-weight: 700;
28+
margin-top: calc(var(--spacing-unit) * 1.5);
29+
margin-bottom: calc(var(--spacing-unit) * 0.5);
30+
color: var(--primary-color);
31+
}
32+
33+
a {
34+
color: var(--secondary-color);
35+
text-decoration: none;
36+
transition: color 0.3s ease-in-out;
37+
}
38+
39+
a:hover {
40+
color: darken(var(--secondary-color), 10%);
41+
text-decoration: underline;
42+
}
43+
44+
.container {
45+
max-width: 1200px;
46+
margin: 0 auto;
47+
padding: var(--spacing-unit);
48+
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
49+
background-color: #fff;
50+
border-radius: var(--border-radius-small);
51+
}
52+
53+
.button {
54+
display: inline-block;
55+
padding: 10px 20px;
56+
background-color: var(--primary-color);
57+
color: #fff;
58+
border: none;
59+
border-radius: var(--border-radius-small);
60+
cursor: pointer;
61+
font-size: 1rem;
62+
transition: background-color 0.3s ease;
63+
}
64+
65+
.button:hover {
66+
background-color: darken(var(--primary-color), 10%);
67+
}
68+
69+
.card {
70+
border: 1px solid #ddd;
71+
border-radius: var(--border-radius-small);
72+
padding: var(--spacing-unit);
73+
margin-bottom: var(--spacing-unit);
74+
background-color: #fff;
75+
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
76+
overflow: hidden;
77+
}
78+
79+
.card img {
80+
max-width: 100%;
81+
height: auto;
82+
border-radius: var(--border-radius-small);
83+
margin-bottom: 10px;
84+
}
85+
86+
input[type="text"],
87+
input[type="email"],
88+
textarea {
89+
width: 100%;
90+
padding: 10px;
91+
margin-bottom: var(--spacing-unit);
92+
border: 1px solid #ccc;
93+
border-radius: var(--border-radius-small);
94+
box-sizing: border-box; /* Ensures padding doesn't add to the width */
95+
}
96+
97+
/* Flexbox example */
98+
.flex-wrapper {
99+
display: flex;
100+
flex-wrap: wrap;
101+
gap: var(--spacing-unit);
102+
justify-content: space-around;
103+
align-items: flex-start;
104+
}
105+
106+
.flex-item {
107+
flex: 1 1 300px; /* Grow, shrink, base width */
108+
min-height: 150px;
109+
background-color: #ecf0f1;
110+
padding: var(--spacing-unit);
111+
border-radius: var(--border-radius-small);
112+
text-align: center;
113+
display: flex;
114+
flex-direction: column;
115+
justify-content: center;
116+
align-items: center;
117+
}
118+
119+
/* Utility classes */
120+
.text-center {
121+
text-align: center;
122+
}
123+
124+
.margin-top-large {
125+
margin-top: calc(var(--spacing-unit) * 2);
126+
}
127+
128+
.hidden {
129+
display: none !important;
130+
}
131+
132+
@media (max-width: 768px) {
133+
.container {
134+
padding: calc(var(--spacing-unit) / 2);
135+
}
136+
137+
.flex-item {
138+
flex-basis: 100%;
139+
}
140+
}

test/fixture/public/foo.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/presets/cloudflare-pages.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ describe.skipIf(isWindows)("nitro:preset:cloudflare-pages", async () => {
5353
"/_unignored.txt",
5454
"/favicon.ico",
5555
"/foo.css",
56+
"/foo.css.br",
57+
"/foo.css.gz",
5658
"/foo.js",
59+
"/foo.js.br",
60+
"/foo.js.gz",
5761
"/json-string",
5862
"/prerender",
5963
"/prerender-custom",

test/tests.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,11 @@ export function testNitro(
334334
headers: { "Accept-Encoding": "" },
335335
})
336336
).headers;
337-
if (headers["vary"]) expect(headers["vary"]).toBe("Origin");
337+
if (headers["vary"])
338+
expect(
339+
headers["vary"].includes("Origin") &&
340+
headers["vary"].includes("Accept-Encoding")
341+
).toBeTruthy();
338342

339343
headers = (
340344
await callHandler({

0 commit comments

Comments
 (0)