Skip to content

Commit

Permalink
Reenable caching for experimental minification (#1089)
Browse files Browse the repository at this point in the history
* Needs lastModified for this... plus bug fix on re for #819

Applies to #432

Auto-merge
  • Loading branch information
Martii authored Apr 20, 2017
1 parent 2abb46e commit 326d5a0
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions controllers/scriptStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function getInstallNameBase(aReq, aOptions) {
var username = aReq.params.username;
var scriptname = aReq.params.scriptname;

var rKnownExtensions = /\.((min\.)?(user\.)?js|meta\.js(on)?)$/;
var rKnownExtensions = /\.(min\.)?((user\.)?js|meta\.js(on)?)$/;

if (!aOptions) {
aOptions = {};
Expand Down Expand Up @@ -382,7 +382,7 @@ var keyScript = function (aReq, aRes, aNext) {
let rJS = /\.js$/;

if (!isLib) {
aNext(userName + '/' + scriptName.replace(/\.(?:meta|(?:min\.)?user)\.js$/, '.user.js'));
aNext(userName + '/' + scriptName.replace(/(\.min)?\.(?:user|meta)\.js$/, '.user.js'));
return;
} else if (rJS.test(scriptName)) {
aNext(userName + '/' + scriptName.replace(/(\.min)?\.js$/, '.js'));
Expand All @@ -395,7 +395,6 @@ var keyScript = function (aReq, aRes, aNext) {

exports.unlockScript = function (aReq, aRes, aNext) {
let rMetaMinUserLibJS = /(?:\.(?:meta|(?:min\.)?user|min))?\.js$/;

let pathname = aReq._parsedUrl.pathname;

let acceptHeader = aReq.headers.accept || '*/*';
Expand Down Expand Up @@ -530,6 +529,7 @@ exports.sendScript = function (aReq, aRes, aNext) {
let rAnyLocalHost = new RegExp('^(?:openuserjs\.org|oujs\.org' +
(isDev ? '|localhost:' + (process.env.PORT || 8080) : '') + ')');

var lastModified = null;
var eTag = null;
var maxAge = 1 * 60 * 60 * 24; // nth day(s) in seconds
var now = null;
Expand Down Expand Up @@ -589,15 +589,23 @@ exports.sendScript = function (aReq, aRes, aNext) {
}

// Set up server to client caching
lastModified = moment(
(!/\.min(\.user)?\.js$/.test(aReq._parsedUrl.pathname)
? aScript.updated
: mtimeUglifyJS2 > aScript.updated ? mtimeUglifyJS2 : aScript.updated)
).utc().format('ddd, DD MMM YYYY HH:mm:ss') + ' GMT';

// Create a based representation of the hex sha512sum
eTag = '"' + Base62.encode(parseInt('0x' + aScript.hash, 16)) + '"';

// NOTE: HTTP/1.1 Caching
aRes.set('Cache-Control', 'public, max-age=' + maxAge +
', no-cache, no-transform, must-revalidate');

aRes.set('Last-Modified', lastModified);

// If already client-side... NOTE: HTTP/1.1 Caching
if (aReq.get('if-none-match') === eTag) {
if (aReq.get('if-none-match') === eTag || aReq.get('if-modified-since') === lastModified) {
aRes.status(304).send(); // Not Modified
return;
}
Expand Down

0 comments on commit 326d5a0

Please sign in to comment.