diff --git a/index.js b/index.js index 1831e5a..74027fc 100644 --- a/index.js +++ b/index.js @@ -174,6 +174,7 @@ async function fastifyStatic (fastify, opts) { pumpOptions, checkedEncodings ) { + const pathnameOrig = pathname const options = Object.assign({}, sendOptions, pumpOptions) if (rootPath) { @@ -346,7 +347,7 @@ async function fastifyStatic (fastify, opts) { return pumpSendToReply( request, reply, - pathname, + pathnameOrig, rootPath, rootPathOffset, undefined, diff --git a/test/static-pre-compressed/dir-gz/index.html b/test/static-pre-compressed/dir-gz/index.html new file mode 100644 index 0000000..8551365 --- /dev/null +++ b/test/static-pre-compressed/dir-gz/index.html @@ -0,0 +1,5 @@ + + + dir-gz index + + diff --git a/test/static-pre-compressed/dir-gz/index.html.gz b/test/static-pre-compressed/dir-gz/index.html.gz new file mode 100644 index 0000000..4de92ee Binary files /dev/null and b/test/static-pre-compressed/dir-gz/index.html.gz differ diff --git a/test/static.test.js b/test/static.test.js index abf2a8d..28dd4dc 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -46,6 +46,9 @@ const indexBr = fs.readFileSync( const dirIndexBr = fs.readFileSync( './test/static-pre-compressed/dir/index.html.br' ) +const dirIndexGz = fs.readFileSync( + './test/static-pre-compressed/dir-gz/index.html.gz' +) const uncompressedStatic = fs .readFileSync('./test/static-pre-compressed/uncompressed.html') .toString('utf8') @@ -3496,6 +3499,35 @@ t.test( } ) +t.test( + 'will serve precompressed gzip index in subdir', + async (t) => { + const pluginOptions = { + root: path.join(__dirname, '/static-pre-compressed'), + preCompressed: true + } + + const fastify = Fastify() + + fastify.register(fastifyStatic, pluginOptions) + t.teardown(fastify.close.bind(fastify)) + + const response = await fastify.inject({ + method: 'GET', + url: '/dir-gz', + headers: { + 'accept-encoding': 'gzip, deflate, br' + } + }) + + genericResponseChecks(t, response) + t.equal(response.headers['content-encoding'], 'gzip') + t.equal(response.statusCode, 200) + t.same(response.rawPayload, dirIndexGz) + t.end() + } +) + t.test( 'will serve precompressed index with alternative index option', async (t) => {