Skip to content

Commit

Permalink
Include file extension in ETag header generation (#101)
Browse files Browse the repository at this point in the history
* Include file path in `ETag` header generation

This is so that an index file that has its file extension changed is
properly invalidated by the browser so that the changed `Content-Type`
header is respected.

* Only consider the extname
  • Loading branch information
TooTallNate authored Aug 8, 2019
1 parent e40b0d1 commit d1eb559
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const etags = new Map();
const calculateSha = (handlers, absolutePath) =>
new Promise((resolve, reject) => {
const hash = createHash('sha1');
hash.update(path.extname(absolutePath));
hash.update('-');
const rs = handlers.createReadStream(absolutePath);
rs.on('error', reject);
rs.on('data', buf => hash.update(buf));
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/docs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## This is a markdown file
14 changes: 11 additions & 3 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1331,14 +1331,22 @@ test('allow symlinks by setting the option', async t => {
});

test('etag header is set', async t => {
const directory = 'single-directory';
const url = await getUrl({
renderSingle: true,
etag: true
});
const response = await fetch(`${url}/${directory}`);

let response = await fetch(`${url}/docs.md`);
t.is(response.status, 200);
t.is(
response.headers.get('etag'),
'"60be4422531fce1513df34cbcc90bed5915a53ef"'
);

response = await fetch(`${url}/docs.txt`);
t.is(response.status, 200);
t.is(
response.headers.get('etag'),
'"4e5f19df3bfe8db7d588edfc3960991aa0715ccf"'
'"ba114dbc69e41e180362234807f093c3c4628f90"'
);
});

0 comments on commit d1eb559

Please sign in to comment.