mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2025-02-18 00:51:13 -05:00
Revert "[Master] Project page caching"
This commit is contained in:
parent
d3b6cd032c
commit
69757e6bec
3 changed files with 92 additions and 51 deletions
|
@ -1,7 +1,6 @@
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
var defaults = require('lodash.defaults');
|
var defaults = require('lodash.defaults');
|
||||||
var fastlyConfig = require('./lib/fastly-config-methods');
|
var fastlyConfig = require('./lib/fastly-config-methods');
|
||||||
const languages = require('../languages.json');
|
|
||||||
|
|
||||||
var route_json = require('../src/routes.json');
|
var route_json = require('../src/routes.json');
|
||||||
|
|
||||||
|
@ -42,43 +41,23 @@ async.auto({
|
||||||
// on any of those route conditions.
|
// on any of those route conditions.
|
||||||
var notPassStatement = fastlyConfig.getAppRouteCondition('../build/*', routes, extraAppRoutes, __dirname);
|
var notPassStatement = fastlyConfig.getAppRouteCondition('../build/*', routes, extraAppRoutes, __dirname);
|
||||||
|
|
||||||
|
// For all the routes in routes.json, construct a varnish-style regex that matches
|
||||||
|
// only if NONE of those routes are matched.
|
||||||
|
var passStatement = fastlyConfig.negateConditionStatement(notPassStatement);
|
||||||
|
|
||||||
// For a non-pass condition, point backend at s3
|
// For a non-pass condition, point backend at s3
|
||||||
var recvCondition = '' +
|
var backendCondition = fastlyConfig.setBackend(
|
||||||
'if (' + notPassStatement + ') {\n' +
|
'F_s3',
|
||||||
' set req.backend = F_s3;\n' +
|
S3_BUCKET_NAME,
|
||||||
' set req.http.host = \"' + S3_BUCKET_NAME + '\";\n' +
|
notPassStatement
|
||||||
'} else {\n' +
|
);
|
||||||
' if (!req.http.Fastly-FF) {\n' +
|
// For a pass condition, set forwarding headers
|
||||||
' if (req.http.X-Forwarded-For) {\n' +
|
var forwardCondition = fastlyConfig.setForwardHeaders(passStatement);
|
||||||
' set req.http.Fastly-Temp-XFF = req.http.X-Forwarded-For \", \" client.ip;\n' +
|
|
||||||
' } else {\n' +
|
|
||||||
' set req.http.Fastly-Temp-XFF = client.ip;\n' +
|
|
||||||
' }\n' +
|
|
||||||
' } else {\n' +
|
|
||||||
' set req.http.Fastly-Temp-XFF = req.http.X-Forwarded-For;\n' +
|
|
||||||
' }\n' +
|
|
||||||
' set req.grace = 60s;\n' +
|
|
||||||
' if (req.http.Cookie:scratchlanguage) {\n' +
|
|
||||||
' set req.http.Accept-Language = req.http.Cookie:scratchlanguage;\n' +
|
|
||||||
' } else {\n' +
|
|
||||||
' set req.http.Accept-Language = accept.language_lookup("' +
|
|
||||||
Object.keys(languages).join(':') + '", ' +
|
|
||||||
'"en", ' +
|
|
||||||
'std.tolower(req.http.Accept-Language)' +
|
|
||||||
');\n' +
|
|
||||||
' }\n' +
|
|
||||||
' if (req.url ~ "^/projects/" && !req.http.Cookie:scratchsessionid) {\n' +
|
|
||||||
' set req.http.Cookie = req.http.Cookie:scratchlanguage;\n' +
|
|
||||||
' } else {\n' +
|
|
||||||
' return(pass);\n' +
|
|
||||||
' }\n' +
|
|
||||||
'}\n';
|
|
||||||
|
|
||||||
|
|
||||||
fastly.setCustomVCL(
|
fastly.setCustomVCL(
|
||||||
results.version,
|
results.version,
|
||||||
'recv-condition',
|
'recv-condition',
|
||||||
recvCondition,
|
backendCondition + forwardCondition,
|
||||||
cb
|
cb
|
||||||
);
|
);
|
||||||
}],
|
}],
|
||||||
|
|
|
@ -84,6 +84,45 @@ var FastlyConfigMethods = {
|
||||||
return 'redirects/' + route.pattern;
|
return 'redirects/' + route.pattern;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns custom vcl configuration as a string for setting the backend
|
||||||
|
* of a request to the given backend/host.
|
||||||
|
*
|
||||||
|
* @param {string} backend name of the backend declared in fastly
|
||||||
|
* @param {string} host name of the s3 bucket to be set as the host
|
||||||
|
* @param {string} condition condition under which backend should be set
|
||||||
|
*/
|
||||||
|
setBackend: function (backend, host, condition) {
|
||||||
|
return '' +
|
||||||
|
'if (' + condition + ') {\n' +
|
||||||
|
' set req.backend = ' + backend + ';\n' +
|
||||||
|
' set req.http.host = \"' + host + '\";\n' +
|
||||||
|
'}\n';
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns custom vcl configuration as a string for headers that
|
||||||
|
* should be added for the condition in which a request is forwarded.
|
||||||
|
*
|
||||||
|
* @param {string} condition condition under which to set pass headers
|
||||||
|
*/
|
||||||
|
setForwardHeaders: function (condition) {
|
||||||
|
return '' +
|
||||||
|
'if (' + condition + ') {\n' +
|
||||||
|
' if (!req.http.Fastly-FF) {\n' +
|
||||||
|
' if (req.http.X-Forwarded-For) {\n' +
|
||||||
|
' set req.http.Fastly-Temp-XFF = req.http.X-Forwarded-For \", \" client.ip;\n' +
|
||||||
|
' } else {\n' +
|
||||||
|
' set req.http.Fastly-Temp-XFF = client.ip;\n' +
|
||||||
|
' }\n' +
|
||||||
|
' } else {\n' +
|
||||||
|
' set req.http.Fastly-Temp-XFF = req.http.X-Forwarded-For;\n' +
|
||||||
|
' }\n' +
|
||||||
|
' set req.grace = 60s;\n' +
|
||||||
|
' return(pass);\n' +
|
||||||
|
'}\n';
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns custom vcl configuration as a string that sets the varnish
|
* Returns custom vcl configuration as a string that sets the varnish
|
||||||
* Time to Live (TTL) for responses that come from s3.
|
* Time to Live (TTL) for responses that come from s3.
|
||||||
|
@ -93,13 +132,9 @@ var FastlyConfigMethods = {
|
||||||
setResponseTTL: function (condition) {
|
setResponseTTL: function (condition) {
|
||||||
return '' +
|
return '' +
|
||||||
'if (' + condition + ') {\n' +
|
'if (' + condition + ') {\n' +
|
||||||
' if (req.url ~ "^/projects/" && !req.http.Cookie:scratchsessionid) {\n' +
|
' set beresp.ttl = 0s;\n' +
|
||||||
' set beresp.http.Vary = "Accept-Encoding, Accept-Language";\n' +
|
' set beresp.grace = 0s;\n' +
|
||||||
' } else {\n' +
|
' return(pass);\n' +
|
||||||
' set beresp.ttl = 0s;\n' +
|
|
||||||
' set beresp.grace = 0s;\n' +
|
|
||||||
' return(pass);\n' +
|
|
||||||
' }\n' +
|
|
||||||
'}\n';
|
'}\n';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -59,17 +59,44 @@ tap.test('getAppRouteCondition', function (t) {
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('testSetTTL', function (t) {
|
tap.test('testSetBackend', function (t) {
|
||||||
var ttl = fastlyConfig.setResponseTTL('itsactuallyttyl');
|
var backend = fastlyConfig.setBackend('wemust', 'goback', 'marty');
|
||||||
t.equal(ttl, '' +
|
t.equal(backend, '' +
|
||||||
'if (itsactuallyttyl) {\n' +
|
'if (marty) {\n' +
|
||||||
' if (req.url ~ "^/projects/" && !req.http.Cookie:scratchsessionid) {\n' +
|
' set req.backend = wemust;\n' +
|
||||||
' set beresp.http.Vary = "Accept-Encoding, Accept-Language";\n' +
|
' set req.http.host = \"goback\";\n' +
|
||||||
' } else {\n' +
|
'}\n'
|
||||||
' set beresp.ttl = 0s;\n' +
|
);
|
||||||
' set beresp.grace = 0s;\n' +
|
t.end();
|
||||||
' return(pass);\n' +
|
});
|
||||||
' }\n' +
|
|
||||||
|
tap.test('testSetForward', function (t) {
|
||||||
|
var forward = fastlyConfig.setForwardHeaders('alwaysforward');
|
||||||
|
t.equal(forward, '' +
|
||||||
|
'if (alwaysforward) {\n' +
|
||||||
|
' if (!req.http.Fastly-FF) {\n' +
|
||||||
|
' if (req.http.X-Forwarded-For) {\n' +
|
||||||
|
' set req.http.Fastly-Temp-XFF = req.http.X-Forwarded-For ", " client.ip;\n' +
|
||||||
|
' } else {\n' +
|
||||||
|
' set req.http.Fastly-Temp-XFF = client.ip;\n' +
|
||||||
|
' }\n' +
|
||||||
|
' } else {\n' +
|
||||||
|
' set req.http.Fastly-Temp-XFF = req.http.X-Forwarded-For;\n' +
|
||||||
|
' }\n' +
|
||||||
|
' set req.grace = 60s;\n' +
|
||||||
|
' return(pass);\n' +
|
||||||
|
'}\n'
|
||||||
|
);
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('testSetTTL', function (t) {
|
||||||
|
var ttl = fastlyConfig.setResponseTTL('itsactuallyttyl');
|
||||||
|
t.equal(ttl, '' +
|
||||||
|
'if (itsactuallyttyl) {\n' +
|
||||||
|
' set beresp.ttl = 0s;\n' +
|
||||||
|
' set beresp.grace = 0s;\n' +
|
||||||
|
' return(pass);\n' +
|
||||||
'}\n'
|
'}\n'
|
||||||
);
|
);
|
||||||
t.end();
|
t.end();
|
||||||
|
|
Loading…
Reference in a new issue