-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
65 lines (52 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var isNumber = require('lodash.isnumber');
var util = require('util');
var onHeaders = require('on-headers');
module.exports = function (defaults) {
return function cacheControl(req, res, next) {
res.cacheControl = defaults;
onHeaders(res, function () {
var options = this.cacheControl || {};
var cacheControl = [];
if (options.private) {
cacheControl.push('private');
} else if (options.public) {
cacheControl.push('public');
}
if (options.noStore) {
options.noCache = true;
cacheControl.push('no-store');
}
if (options.noCache) {
options.maxAge = 0;
delete options.sMaxAge;
cacheControl.push('no-cache');
}
if (options.noTransform) {
cacheControl.push('no-transform');
}
if (options.proxyRevalidate) {
cacheControl.push('proxy-revalidate');
}
if (options.mustRevalidate) {
cacheControl.push('must-revalidate');
} else if (!options.noCache) {
if (options.staleIfError) {
cacheControl.push(util.format('stale-if-error=%d', options.staleIfError));
}
if (options.staleWhileRevalidate) {
cacheControl.push(util.format('stale-while-revalidate=%d', options.staleWhileRevalidate));
}
}
if (isNumber(options.maxAge)) {
cacheControl.push(util.format('max-age=%d', options.maxAge));
}
if (isNumber(options.sMaxAge)) {
cacheControl.push(util.format('s-maxage=%d', options.sMaxAge));
}
if (cacheControl.length) {
this.setHeader('Cache-Control', cacheControl.join(','));
}
});
next();
}
}