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

Serve static assets with an efficient cache policy #2216

Merged
merged 17 commits into from Mar 9, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -1115,6 +1115,9 @@ See https://6.dev-docs.plone.org/volto/upgrade-guide/index.html for more informa

### Breaking

- Add directive to cache stable resources in browser or intermediate server for 365 days,
static resource that could change after a new deployment for 1 minute. @mamico

### Feature

- Added new components `Aliases` for aliases control in Volto. Alias management in both controlpanel and object view. @andreiggr @avoinea
Expand Down
19 changes: 19 additions & 0 deletions src/config/server.js
Expand Up @@ -2,6 +2,7 @@ import imagesMiddleware from '@plone/volto/express-middleware/images';
import filesMiddleware from '@plone/volto/express-middleware/files';
import robotstxtMiddleware from '@plone/volto/express-middleware/robotstxt';
import sitemapMiddleware from '@plone/volto/express-middleware/sitemap';
import staticsMiddleware from '@plone/volto/express-middleware/static';
import devProxyMiddleware from '@plone/volto/express-middleware/devproxy';

const settings = {
Expand All @@ -11,10 +12,28 @@ const settings = {
imagesMiddleware(),
robotstxtMiddleware(),
sitemapMiddleware(),
staticsMiddleware(),
],
criticalCssPath: 'public/critical.css',
readCriticalCss: null, // so it will be defaultReadCriticalCss
extractScripts: { errorPages: false },
staticFiles: [
{
id: 'root_static',
match: /^\/static\/.*/,
headers: {
// stable resources never change. 31536000 seconds == 365 days
'Cache-Control': 'public, max-age=31536000',
},
},
{
id: 'all',
match: /.*/,
headers: {
'Cache-Control': 'public, max-age=60',
},
},
],
};

export default settings;
32 changes: 32 additions & 0 deletions src/express-middleware/static.js
@@ -0,0 +1,32 @@
import express from 'express';
import path from 'path';
import config from '@plone/volto/registry';

const staticMiddleware = express.static(
process.env.BUILD_DIR
? path.join(process.env.BUILD_DIR, 'public')
: process.env.RAZZLE_PUBLIC_DIR,
{
setHeaders: function (res, path) {
const pathLib = require('path');
const base = pathLib.resolve(process.env.RAZZLE_PUBLIC_DIR);
const relpath = path.substr(base.length);
config.settings.serverConfig.staticFiles.some((elem) => {
if (relpath.match(elem.match)) {
for (const name in elem.headers) {
res.setHeader(name, elem.headers[name] || 'undefined');
}
return true;
}
return false;
});
},
},
);

export default function () {
const middleware = express.Router();
middleware.all('*', staticMiddleware);
middleware.id = 'staticResourcesProcessor';
return middleware;
}
7 changes: 0 additions & 7 deletions src/server.jsx
Expand Up @@ -59,13 +59,6 @@ const supported = new locale.Locales(keys(languages), 'en');

const server = express()
.disable('x-powered-by')
.use(
express.static(
process.env.BUILD_DIR
? path.join(process.env.BUILD_DIR, 'public')
: process.env.RAZZLE_PUBLIC_DIR,
),
)
.head('/*', function (req, res) {
// Support for HEAD requests. Required by start-test utility in CI.
res.send('');
Expand Down