Skip to content

Commit

Permalink
Enable meta cache from Server to Client (#1070)
Browse files Browse the repository at this point in the history
* Enable meta cache from Server to Client

* * Caching works well but GM kills it with [`channel.LOAD_BYPASS_CACHE`](https://github.com/greasemonkey/greasemonkey/blob/696848acc40c6395904a53acdc15a653d5be502c/modules/remoteScript.js#L606)

**NOTE** Keep this at one day, possibly for long term.

Applies to #432 and #249. Possible addition to #944 down the line. Regression **not** present, nor needed, from detection and revert in #894 . Script meta should always be the same whether minified fork or original

* Scoot identifier declaration down a little

Applies to #432 and #249. Possible addition to #944 down the line.
  • Loading branch information
Martii authored Apr 4, 2017
1 parent 955c15e commit c634b3e
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions controllers/scriptStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,6 @@ exports.sendMeta = function (aReq, aRes, aNext) {
}

function render() {
aRes.set('Content-Type', 'application/json; charset=UTF-8');

aRes.end(JSON.stringify(meta, null, isPro ? '' : ' '));
}

Expand All @@ -636,15 +634,45 @@ exports.sendMeta = function (aReq, aRes, aNext) {
var whitespace = '\u0020\u0020\u0020\u0020';
var tasks = [];

var eTag = null;
var maxAge = 1 * 60 * 60 * 24; // nth day(s) in seconds
var lastModified = null;

if (!aScript) {
aNext();
return;
}

lastModified = aScript.updated;

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

script = modelParser.parseScript(aScript);
meta = script.meta; // NOTE: Watchpoint

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

// NOTE: HTTP/1.0 Caching
aRes.set('Last-Modified', lastModified);

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

if (/\.json$/.test(aReq.params.scriptname)) {
aRes.set('Content-Type', 'application/json; charset=UTF-8');

// NOTE: HTTP/1.0 Caching
aRes.set('Expires', moment(moment() + maxAge * 1000).utc()
.format('ddd, DD MMM YYYY HH:mm:ss') + ' GMT');

// NOTE: HTTP/1.1 Caching
aRes.set('Etag', eTag);

// Check for existance of OUJS metadata block
if (!meta.OpenUserJS) {
Expand All @@ -667,6 +695,13 @@ exports.sendMeta = function (aReq, aRes, aNext) {
} else {
aRes.set('Content-Type', 'text/javascript; charset=UTF-8');

// NOTE: HTTP/1.0 Caching
aRes.set('Expires', moment(moment() + maxAge * 1000).utc()
.format('ddd, DD MMM YYYY HH:mm:ss') + ' GMT');

// NOTE: HTTP/1.1 Caching
aRes.set('Etag', eTag);

aRes.write('// ==UserScript==\n');

if (meta.UserScript.version) {
Expand Down

0 comments on commit c634b3e

Please sign in to comment.