feat: siteFile support custom control-cache (#4902)

Co-authored-by: yangbing <yangbing@immotors.com>
This commit is contained in:
binginsist 2022-03-16 09:31:39 +08:00 committed by GitHub
parent a7aa7f37d9
commit caacd09c38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 2 deletions

View File

@ -1,7 +1,6 @@
'use strict';
const path = require('path');
const MAX_AGE = 'public, max-age=2592000'; // 30 days
module.exports = options => {
return function siteFile(ctx, next) {
@ -19,7 +18,7 @@ module.exports = options => {
// '/robots.txt': Buffer <xx..
// content is buffer
if (Buffer.isBuffer(content)) {
ctx.set('cache-control', MAX_AGE);
ctx.set('cache-control', options.cacheControl);
ctx.body = content;
ctx.type = path.extname(ctx.path);
return;

View File

@ -190,6 +190,7 @@ module.exports = appInfo => {
* You can map some files using this options, it will response immdiately when matching.
*
* @member {Object} Config#siteFile - key is path, and value is url or buffer.
* @property {String} cacheControl - files cache , default is public, max-age=2592000
* @example
* // specific app's favicon, => '/favicon.ico': 'https://eggjs.org/favicon.ico',
* config.siteFile = {
@ -198,6 +199,8 @@ module.exports = appInfo => {
*/
config.siteFile = {
'/favicon.ico': fs.readFileSync(path.join(__dirname, 'favicon.png')),
// default cache in 30 days
cacheControl: 'public, max-age=2592000',
};
/**

View File

@ -91,4 +91,20 @@ describe('test/app/middleware/site_file.test.js', () => {
});
});
});
describe('siteFile.cacheControl = no-store', () => {
let app;
before(() => {
app = utils.app('apps/sitefile-custom-cachecontrol');
return app.ready();
});
after(() => app.close());
it('should get custom cache-control', async () => {
await app.httpRequest()
.get('/favicon.ico')
.expect(res => assert(res.headers['cache-control'].includes('no-store')))
.expect(200);
});
});
});

View File

@ -0,0 +1,5 @@
module.exports = app => {
app.get('/', function*() {
this.body = 'Hi, this is home';
});
};

View File

@ -0,0 +1,5 @@
exports.siteFile = {
cacheControl: 'no-store',
};
exports.keys = 'foo';

View File

@ -0,0 +1,3 @@
{
"name": "sitefile-custom-cachecontrol"
}