Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Set Cache-Control headers for both the static files and dynamically g…
Browse files Browse the repository at this point in the history
…enerated JS files
  • Loading branch information
ianb committed Oct 19, 2016
1 parent b068b1b commit 2a754b5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ CLIENT_SECRET=efde5874713abf2a8ec9d8af8488fae7c7e0a933dba08208f644fa93b5622dab
SHOW_STACK_TRACES=true
LOG_LINT=true
LOG_LEVEL=debug
SET_CACHE=false
17 changes: 17 additions & 0 deletions server/src/caching.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const config = require("./config").getProperties();

exports.cacheTime = 60 * 60 * 24 * 1; // 1 day

if (! config.setCache) {
exports.cacheTime = 0;
}

exports.setCache = function (res, options) {
if (config.setCache) {
options = options || {};
let pub = options.private ? "private" : "public";
res.set("Cache-Control", `${pub}, max-age=${exports.cacheTime}`);
} else {
res.set("Cache-Control", "no-cache");
}
};
7 changes: 7 additions & 0 deletions server/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ var conf = convict({
default: "",
env: "STATSD_PREFIX",
arg: "statsd-prefix"
},
setCache: {
doc: "Set Cache-Control headers",
format: Boolean,
default: true,
env: "SET_CACHE",
arg: "set-cache"
}
});

Expand Down
7 changes: 6 additions & 1 deletion server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const validUrl = require("valid-url");
const { createProxyUrl } = require("./proxy-url");
const statsd = require("./statsd");
const { notFound } = require("./pages/not-found/server");
const { cacheTime, setCache } = require("./caching");

const PROXY_HEADER_WHITELIST = {
"content-type": true,
Expand Down Expand Up @@ -196,7 +197,8 @@ app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json({limit: '100mb'}));

app.use("/static", express.static(path.join(__dirname, "static"), {
index: false
index: false,
maxAge: cacheTime
}));

let xpidir = path.join(__dirname, "..", "xpi");
Expand Down Expand Up @@ -245,6 +247,7 @@ app.get("/ga-activation-hashed.js", function (req, res) {

function sendGaActivation(req, res, hashPage) {
let promise;
setCache(res, {private: true});
if (req.deviceId) {
promise = hashUserId(req.deviceId).then((uuid) => {
return uuid.toString();
Expand All @@ -261,12 +264,14 @@ function sendGaActivation(req, res, hashPage) {
}

app.get("/set-content-hosting-origin.js", function (req, res) {
setCache(res);
let postMessageOrigin = `${req.protocol}://${req.config.contentOrigin}`;
let script = `var CONTENT_HOSTING_ORIGIN = "${postMessageOrigin}";`
jsResponse(res, script);
});

app.get("/configure-raven.js", function (req, res) {
setCache(res);
if (! req.config.sentryPublicDSN) {
jsResponse(res, "");
return;
Expand Down

0 comments on commit 2a754b5

Please sign in to comment.