Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix index not being served for precompressed assets #205

Merged
merged 6 commits into from May 18, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions index.js
Expand Up @@ -81,6 +81,12 @@ async function fastifyStatic (fastify, opts) {
encodingExt = checkEncodingHeaders(request.headers, checkedExtensions)

if (encodingExt) {
if (pathname.endsWith('/')) {
pathname = findIndexFile(pathname, options.root, options.index)
if (!pathname) {
return reply.callNotFound()
}
}
pathnameForSend = pathname + '.' + encodingExt
}
}
Expand Down Expand Up @@ -376,6 +382,14 @@ function checkPath (fastify, rootPath) {

const supportedEncodings = ['br', 'gzip', 'deflate']

function findIndexFile (pathname, root, indexFiles = ['index.html']) {
return indexFiles.find(filename => {
const p = path.join(root, pathname, filename)
const stats = statSync(p, { throwIfNoEntry: false })
return stats && !stats.isDirectory()
})
}

// Adapted from https://github.com/fastify/fastify-compress/blob/fa5c12a5394285c86d9f438cb39ff44f3d5cde79/index.js#L442
function checkEncodingHeaders (headers, checked) {
if (!('accept-encoding' in headers)) return
Expand Down
5 changes: 5 additions & 0 deletions test/static-pre-compressed/index.html
@@ -0,0 +1,5 @@
<html>
<body>
index
</body>
</html>
Binary file added test/static-pre-compressed/index.html.br
Binary file not shown.
62 changes: 62 additions & 0 deletions test/static.test.js
Expand Up @@ -38,6 +38,9 @@ const allThreeBr = fs.readFileSync(
const gzipOnly = fs.readFileSync(
'./test/static-pre-compressed/gzip-only.html.gz'
)
const indexBr = fs.readFileSync(
'./test/static-pre-compressed/index.html.br'
)
const uncompressedStatic = fs
.readFileSync('./test/static-pre-compressed/uncompressed.html')
.toString('utf8')
Expand Down Expand Up @@ -3091,3 +3094,62 @@ t.test(
t.end()
}
)

t.test(
'will serve precompressed index',
async (t) => {
const pluginOptions = {
root: path.join(__dirname, '/static-pre-compressed'),
prefix: '/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: '/static-pre-compressed/',
headers: {
'accept-encoding': 'gzip, deflate, br'
}
})

t.equal(response.headers['content-encoding'], 'br')
t.equal(response.statusCode, 200)
t.same(response.rawPayload, indexBr)
t.end()
}
)

t.test(
'will serve precompressed index with alternative index option',
async (t) => {
const pluginOptions = {
root: path.join(__dirname, '/static-pre-compressed'),
prefix: '/static-pre-compressed/',
preCompressed: true,
index: ['all-three.html']
}

const fastify = Fastify()

fastify.register(fastifyStatic, pluginOptions)
t.teardown(fastify.close.bind(fastify))

const response = await fastify.inject({
method: 'GET',
url: '/static-pre-compressed/',
headers: {
'accept-encoding': 'gzip, deflate, br'
}
})

t.equal(response.headers['content-encoding'], 'br')
t.equal(response.statusCode, 200)
t.same(response.rawPayload, allThreeBr)
t.end()
}
)