Defaults for Fastify that everyone can agree on™.
This plugin adds some useful utilities to your Fastify instance, see the API section to learn more.
Why are these APIs here and not included with Fastify?
Because Fastify aims to be as small and focused as possible, every utility that is not essential should be shipped as a standalone plugin.
Install
npm i @fastify/sensible
Compatibility
Plugin version
Fastify version
>=6.x
^5.x
^5.x
^4.x
^4.x
^3.x
>=2.x <4.x
^2.x
^1.x
^1.x
Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin
in the table above.
See Fastify's LTS policy for more details.
The reply interface is decorated with a helper to configure cache control response headers.
// configure a single typefastify.get('/', (req, reply) => { reply.cacheControl('public') reply.send('ok')})// configure multiple typesfastify.get('/', (req, reply) => { reply.cacheControl('public') reply.cacheControl('immutable') reply.send('ok')})// configure a type timefastify.get('/', (req, reply) => { reply.cacheControl('max-age'
reply.preventCache
The reply interface is decorated with a helper to set the cache control header to a no caching configuration.
fastify.get('/', (req, reply) => { // will set cache-control to 'no-store, max-age=0, private' // and for HTTP/1.0 compatibility // will set pragma to 'no-cache' and expires to 0 reply.preventCache() reply.send('ok')})
reply.revalidate
The reply interface is decorated with a helper to set the cache control header to a no caching configuration.
fastify.get('/', (req, reply) => { reply.revalidate() // will set to 'max-age=0, must-revalidate' reply.send('ok')})
reply.staticCache
The reply interface is decorated with a helper to set the cache control header to a public and immutable configuration.
fastify.get('/', (req, reply) => { // the time can be defined as a string reply.staticCache(42) // will set to 'public, max-age=42, immutable' reply.send('ok')})
reply.stale
The reply interface is decorated with a helper to set the cache control header for stale content.
fastify.get('/', (req, reply) => { // the time can be defined as a string reply.stale('while-revalidate', 42) reply.stale('if-error', 1) reply.send('ok')})
reply.maxAge
The reply interface is decorated with a helper to set max age of the response. It can be used in conjunction with reply.stale, see here.
fastify.get('/', (req, reply) => { // the time can be defined as a string reply.maxAge(86400) reply.stale('while-revalidate', 42) reply.send('ok')})
request.forwarded
The request interface is decorated with jshttp/forwarded, the API is the same, but you do not need to pass the request object: