Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/configurable expire headers for static assets #3289

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@
"fullLanguageName": "English",
"bundleAllStoreviewLanguages": true
},
"expireHeaders": {
"default": "30d",
"application/json": "24h",
"image/png": "7d"
},
"newsletter": {
"endpoint": "/api/ext/mailchimp-subscribe/subscribe"
},
Expand Down
12 changes: 10 additions & 2 deletions core/scripts/server.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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 && isProd ? function (res, path) {
const 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')
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down