koajs-nocache
Version:
A koa middleware to add nocache response headers.
32 lines (28 loc) • 753 B
JavaScript
/**
* nocache middleware.
*
* @param {Object} [options]
* - {String|Array} methods need add nocache headers.
* default is ['GET']
*
* @return {Function} nocache middleware
*/
module.exports = function nocache(options) {
options = options || {};
let methods = options.methods;
if (!methods) {
methods = ['GET'];
}
if (typeof methods === 'string') {
methods = [methods];
}
return async function nocacheMiddleware(ctx, next) {
if (methods.includes(ctx.method)) {
ctx.set('Cache-Control', 'no-cache, no-store, must-revalidate, proxy-revalidate');
ctx.set('Surrogate-Control', 'no-store');
ctx.set('Expires', '0');
ctx.set('Pragma', 'no-cache');
}
await next();
};
};