Skip to content

Commit

Permalink
Support ?maxAge
Browse files Browse the repository at this point in the history
Fixes #534.
  • Loading branch information
espadrine committed Apr 9, 2016
1 parent e8a2b1c commit b89dc72
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,12 @@ vendorDomain.on('error', function(err) {

function cache(f) {
return function getRequest(data, match, end, ask) {
// Cache management - no cache, so it won't be cached by GitHub's CDN.
ask.res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
if (data.maxAge !== undefined && /^[0-9]+$/.test(data.maxAge)) {
var maxAge = +data.maxAge;
} else {
// Cache management - no cache, so it won't be cached by GitHub's CDN.
ask.res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
}
var reqTime = new Date();
var date = (reqTime).toGMTString();
ask.res.setHeader('Expires', date); // Proxies, GitHub, see #221.
Expand Down Expand Up @@ -269,6 +273,10 @@ function cache(f) {
};
requestCache.set(cacheIndex, updatedCache);
if (!cachedVersionSent) {
// Set the cache interval if specified.
if (maxAge !== undefined) {
ask.res.setHeader('Cache-Control', 'max-age=' + maxAge);
}
badge(badgeData, makeSend(format, ask.res, end));
}
}, cachedRequest);
Expand Down Expand Up @@ -5070,13 +5078,9 @@ function(data, match, end, ask) {
incrMonthlyAnalytics(analytics.rawFlatSquareMonthly);
}

if (ask.req.headers['cache-control']) {
ask.res.setHeader('Cache-Control', ask.req.headers['cache-control']);
} else {
// Cache management - the badge is constant.
var cacheDuration = (3600*24*1)|0; // 1 day.
ask.res.setHeader('Cache-Control', 'public, max-age=' + cacheDuration);
}
// Cache management - the badge is constant.
var cacheDuration = (3600*24*1)|0; // 1 day.
ask.res.setHeader('Cache-Control', 'max-age=' + cacheDuration);
if (+(new Date(ask.req.headers['if-modified-since'])) >= +serverStartTime) {
ask.res.statusCode = 304;
ask.res.end(); // not modified.
Expand Down Expand Up @@ -5115,7 +5119,7 @@ function(data, match, end, ask) {

// Cache management - the badge is constant.
var cacheDuration = (3600*24*1)|0; // 1 day.
ask.res.setHeader('Cache-Control', 'public, max-age=' + cacheDuration);
ask.res.setHeader('Cache-Control', 'max-age=' + cacheDuration);
if (+(new Date(ask.req.headers['if-modified-since'])) >= +serverStartTime) {
ask.res.statusCode = 304;
ask.res.end(); // not modified.
Expand Down Expand Up @@ -5160,6 +5164,7 @@ function getLabel(label, data) {
}

// data (URL query) can include `label`, `style`, `logo`, `logoWidth`, `link`.
// It can also include `maxAge`.
function getBadgeData(defaultLabel, data) {
var label = getLabel(defaultLabel, data);
var template = data.style || 'default';
Expand Down

0 comments on commit b89dc72

Please sign in to comment.