From 5eb23b38e077e9565a7869b01773966a20c2aac2 Mon Sep 17 00:00:00 2001 From: PhoenixPM - BK Date: Fri, 26 Jul 2019 14:00:42 +0200 Subject: [PATCH 1/4] #3268 implemented config for cache-control setting --- CHANGELOG.md | 1 + config/default.json | 5 +++++ core/scripts/server.js | 12 ++++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a7148aff4..e5cff95e2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added price formatting based on locales in multistore - @andrzejewsky (#3060) - Added support for tax calculation where the values from customer_tax_class_ids is used - @resubaka (#3245) - Added loading product attributes (`entities.productListWithChildren.includeFields`) on category page - @andrzejewsky (#3220) +- Added config to set Cache-Control header for static assets based on mime type - @phoenix-bjoern (#3268) ### Fixed diff --git a/config/default.json b/config/default.json index f3b96604aa..48861412e4 100644 --- a/config/default.json +++ b/config/default.json @@ -423,6 +423,11 @@ "fullLanguageName": "English", "bundleAllStoreviewLanguages": true }, + "expireHeaders": { + "default": "30d", + "application/json": "24h", + "image/png": "7d" + }, "newsletter": { "endpoint": "/api/ext/mailchimp-subscribe/subscribe" }, diff --git a/core/scripts/server.js b/core/scripts/server.js index 12437daffd..43c5a60292 100755 --- a/core/scripts/server.js +++ b/core/scripts/server.js @@ -1,6 +1,7 @@ const fs = require('fs') const path = require('path') const express = require('express') +const ms = require('ms') const compile = require('lodash.template') const rootPath = require('app-root-path').path const resolve = file => path.resolve(rootPath, file) @@ -104,8 +105,15 @@ function invalidateCache (req, res) { } const serve = (path, cache, options) => express.static(resolve(path), Object.assign({ - maxAge: cache && isProd ? 2592000000 : 0, // 1 month in milliseconds = 1000 * 60 * 60 * 24 * 30 = 2592000000 - fallthrough: false + fallthrough: false, + setHeaders: cache ? function (res, path) { + let mimeType = express.static.mime.lookup(path); + let maxAge = config.expireHeaders.default; + if (config.expireHeaders.hasOwnProperty(mimeType)) { + maxAge = config.expireHeaders.get(mimeType); + } + res.setHeader('Cache-Control', 'public, max-age=' + ms(maxAge)); + } : null }, options)) const themeRoot = require('../build/theme-path') From 6b85b1e4701ceb5f6f42ae922011d127d51b7e01 Mon Sep 17 00:00:00 2001 From: PhoenixPM - BK Date: Fri, 26 Jul 2019 14:05:32 +0200 Subject: [PATCH 2/4] #3268 added back isProd check --- core/scripts/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/scripts/server.js b/core/scripts/server.js index 43c5a60292..b18558569f 100755 --- a/core/scripts/server.js +++ b/core/scripts/server.js @@ -106,7 +106,7 @@ function invalidateCache (req, res) { const serve = (path, cache, options) => express.static(resolve(path), Object.assign({ fallthrough: false, - setHeaders: cache ? function (res, path) { + setHeaders: cache && isProd ? function (res, path) { let mimeType = express.static.mime.lookup(path); let maxAge = config.expireHeaders.default; if (config.expireHeaders.hasOwnProperty(mimeType)) { From 114e63796b3c94a6d6e4c059ecd221f4f9507eb1 Mon Sep 17 00:00:00 2001 From: PhoenixPM - BK Date: Fri, 26 Jul 2019 14:32:55 +0200 Subject: [PATCH 3/4] #3268 added ms dependency --- package.json | 1 + yarn.lock | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/package.json b/package.json index 48dead3e97..0b3c2797ab 100755 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "js-sha3": "^0.8.0", "localforage": "^1.7.2", "magento2-rest-client": "github:DivanteLtd/magento2-rest-client", + "ms": "^2.1.2", "pm2": "^2.10.4", "redis-tag-cache": "^1.2.1", "reflect-metadata": "^0.1.12", diff --git a/yarn.lock b/yarn.lock index 0ea6d28d74..e083f7ebed 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9398,6 +9398,11 @@ ms@^2.0.0, ms@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a" +ms@^2.1.2: + version "2.1.2" + resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009" + integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w== + multimatch@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/multimatch/-/multimatch-2.1.0.tgz#9c7906a22fb4c02919e2f5f75161b4cdbd4b2a2b" From 16c92b45d4497f66193224115ebc7b9c996fce19 Mon Sep 17 00:00:00 2001 From: PhoenixPM - BK Date: Fri, 26 Jul 2019 14:54:50 +0200 Subject: [PATCH 4/4] #3268 use const instead of let --- core/scripts/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/scripts/server.js b/core/scripts/server.js index b18558569f..4e3a554c17 100755 --- a/core/scripts/server.js +++ b/core/scripts/server.js @@ -107,7 +107,7 @@ function invalidateCache (req, res) { const serve = (path, cache, options) => express.static(resolve(path), Object.assign({ fallthrough: false, setHeaders: cache && isProd ? function (res, path) { - let mimeType = express.static.mime.lookup(path); + const mimeType = express.static.mime.lookup(path); let maxAge = config.expireHeaders.default; if (config.expireHeaders.hasOwnProperty(mimeType)) { maxAge = config.expireHeaders.get(mimeType);