From 9ecaf52b4c9f3ebb2fd949434551b10f7f07dd19 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Tue, 6 Aug 2019 15:27:19 -0700 Subject: [PATCH 1/2] 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. --- src/index.js | 2 ++ test/fixtures/docs.txt | 1 + test/integration.js | 14 +++++++++++--- 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/docs.txt diff --git a/src/index.js b/src/index.js index 9a8a824..593029c 100644 --- a/src/index.js +++ b/src/index.js @@ -24,6 +24,8 @@ const etags = new Map(); const calculateSha = (handlers, absolutePath) => new Promise((resolve, reject) => { const hash = createHash('sha1'); + hash.update(absolutePath); + hash.update('-'); const rs = handlers.createReadStream(absolutePath); rs.on('error', reject); rs.on('data', buf => hash.update(buf)); diff --git a/test/fixtures/docs.txt b/test/fixtures/docs.txt new file mode 100644 index 0000000..4dbb717 --- /dev/null +++ b/test/fixtures/docs.txt @@ -0,0 +1 @@ +## This is a markdown file diff --git a/test/integration.js b/test/integration.js index 756537b..578259e 100644 --- a/test/integration.js +++ b/test/integration.js @@ -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'), + '"f636be8ebeec6d60536a00a9d197843becfdd0c6"' + ); + + response = await fetch(`${url}/docs.txt`); + t.is(response.status, 200); t.is( response.headers.get('etag'), - '"4e5f19df3bfe8db7d588edfc3960991aa0715ccf"' + '"6658be9c345e8b79034c781e555c15a38302bc2c"' ); }); From 4804354dfb7aabe28d257b62ec1a701f0a91e515 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Tue, 6 Aug 2019 15:35:58 -0700 Subject: [PATCH 2/2] Only consider the extname --- src/index.js | 2 +- test/integration.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 593029c..85bbe76 100644 --- a/src/index.js +++ b/src/index.js @@ -24,7 +24,7 @@ const etags = new Map(); const calculateSha = (handlers, absolutePath) => new Promise((resolve, reject) => { const hash = createHash('sha1'); - hash.update(absolutePath); + hash.update(path.extname(absolutePath)); hash.update('-'); const rs = handlers.createReadStream(absolutePath); rs.on('error', reject); diff --git a/test/integration.js b/test/integration.js index 578259e..61b812b 100644 --- a/test/integration.js +++ b/test/integration.js @@ -1340,13 +1340,13 @@ test('etag header is set', async t => { t.is(response.status, 200); t.is( response.headers.get('etag'), - '"f636be8ebeec6d60536a00a9d197843becfdd0c6"' + '"60be4422531fce1513df34cbcc90bed5915a53ef"' ); response = await fetch(`${url}/docs.txt`); t.is(response.status, 200); t.is( response.headers.get('etag'), - '"6658be9c345e8b79034c781e555c15a38302bc2c"' + '"ba114dbc69e41e180362234807f093c3c4628f90"' ); });