Skip to content
Open
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
8 changes: 7 additions & 1 deletion packages/sirv/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,13 @@ export default function (dir, opts={}) {
return res.end();
}

if (gzips || brots) {
// When a users "requests" a file, sirv may "choose" a different (compressed) version based
// on the current options (gzip, brotli), request headers ("Accept-Encoding"), and available files.
// We should only set "Vary: Accept-Encoding" if a compressed version of the requested file exists
// and was chosen instead of the requested file.
const isChosenFileCompressed = !!data.headers['Content-Encoding'];
const doesChosenFileMatchRequestedFile = data.abs.endsWith(pathname);
if (isChosenFileCompressed && !doesChosenFileMatchRequestedFile) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a second look, !doesChosenFileMatchRequestedFile might alone suffice for this check since choosing a compressed version of a requested file is the only reason they'd differ.

res.setHeader('Vary', 'Accept-Encoding');
}

Expand Down
1 change: 1 addition & 0 deletions tests/public/no-compressed-version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no-compressed-version.txt
90 changes: 90 additions & 0 deletions tests/sirv.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,51 @@ brotli('should be preferred when "Accept-Encoding" allows both', async () => {
}
});

brotli('should set "Vary: Accept-Encoding" when a compressed version of the requested file exists', async () => {
let server = utils.http({ brotli: true });
let headers = { 'Accept-Encoding': 'br,gzip' };

try {
let res = await server.send('GET', '/index.html', { headers });
assert.is(res.headers['content-type'], 'text/html;charset=utf-8');
assert.is(res.headers['vary'], 'Accept-Encoding');
assert.is(res.data, 'brotli html\n');
assert.is(res.statusCode, 200);
} finally {
server.close();
}
});

brotli('should not set "Vary: Accept-Encoding" when a compressed file is requested directly', async () => {
let server = utils.http({ brotli: true });
let headers = { 'Accept-Encoding': 'br,gzip' };

try {
let res = await server.send('GET', '/index.html.br', { headers });
assert.is(res.headers['content-type'], 'text/html;charset=utf-8');
assert.is(res.headers['vary'], undefined);
assert.is(res.data, 'brotli html\n');
assert.is(res.statusCode, 200);
} finally {
server.close();
}
});

brotli('should not set "Vary: Accept-Encoding" when a compressed version of the requested file does not exist', async () => {
let server = utils.http({ brotli: true });
let headers = { 'Accept-Encoding': 'br,gzip' };

try {
let res = await server.send('GET', '/no-compressed-version.txt', { headers });
assert.is(res.headers['content-type'], 'text/plain');
assert.is(res.headers['vary'], undefined);
assert.is(res.data, 'no-compressed-version.txt\n');
assert.is(res.statusCode, 200);
} finally {
server.close();
}
});

brotli.run();

// ---
Expand Down Expand Up @@ -885,6 +930,51 @@ gzip('should defer to brotli when "Accept-Encoding" allows both', async () => {
}
});

gzip('should set "Vary: Accept-Encoding" when a compressed version of the requested file exists', async () => {
let server = utils.http({ gzip: true });
let headers = { 'Accept-Encoding': 'br,gzip' };

try {
let res = await server.send('GET', '/index.html', { headers });
assert.is(res.headers['content-type'], 'text/html;charset=utf-8');
assert.is(res.headers['vary'], 'Accept-Encoding');
assert.is(res.data, 'gzip html\n');
assert.is(res.statusCode, 200);
} finally {
server.close();
}
});

gzip('should not set "Vary: Accept-Encoding" when a compressed file is requested directly', async () => {
let server = utils.http({ gzip: true });
let headers = { 'Accept-Encoding': 'br,gzip' };

try {
let res = await server.send('GET', '/index.html.gz', { headers });
assert.is(res.headers['content-type'], 'text/html;charset=utf-8');
assert.is(res.headers['vary'], undefined);
assert.is(res.data, 'gzip html\n');
assert.is(res.statusCode, 200);
} finally {
server.close();
}
});

gzip('should not set "Vary: Accept-Encoding" when a compressed version of the requested file does not exist', async () => {
let server = utils.http({ gzip: true });
let headers = { 'Accept-Encoding': 'br,gzip' };

try {
let res = await server.send('GET', '/no-compressed-version.txt', { headers });
assert.is(res.headers['content-type'], 'text/plain');
assert.is(res.headers['vary'], undefined);
assert.is(res.data, 'no-compressed-version.txt\n');
assert.is(res.statusCode, 200);
} finally {
server.close();
}
});

gzip.run();

// ---
Expand Down