Is it possible to set max-age?
Extarys opened this issue · 2 comments
I would like to know how to set max-age for a specific path.
The logo in this example.
module.exports = function(fastify, opts, next) {
fastify.register(require('fastify-caching'))
fastify.get('/logo.png', handler.logo)
fastify.get('/nocache.js', handler.js)
}
module.exports = {
logo: function(request, reply) {
const stream = fs.createReadStream(
path.join(appRoot.path, '/build/assets/logo.png'),
'utf8'
)
reply
.header('Content-Type', 'image/png')
.send(stream)
}
nocache: function(request, reply) {
const stream = fs.createReadStream(
path.join(appRoot.path, '/build/assets/nocache.js'),
'utf8'
)
reply
.header('Content-Type', 'application/javascript; charset=UTF-8')
.send(stream)
}
}
expiresIn (Default: undefined): a value, in seconds, for the max-age the resource may be cached. When this is set, and privacy is not set to no-cache, then ', max-age=' will be appended to the cache-control header.
Yes, you can, though there's a catch. From the snippet above:
When this (expiresIn) is set, and privacy is not set to no-cache, then ', max-age=' will be appended to the cache-control header.
However, looking at the code this isn't true because if privacy is not defined it will fail the if condition and skip the block which applies the max-age value from expiresIn. I would suggest setting privacy to public and setting expiresIn to your desired max-age in seconds.
Ohh thanks, I'll look into it! :D