From caacd09c38aae03fc291febbb97a43c8ecbdc221 Mon Sep 17 00:00:00 2001 From: binginsist Date: Wed, 16 Mar 2022 09:31:39 +0800 Subject: [PATCH] feat: siteFile support custom control-cache (#4902) Co-authored-by: yangbing --- app/middleware/site_file.js | 3 +-- config/config.default.js | 3 +++ test/app/middleware/site_file.test.js | 16 ++++++++++++++++ .../siteFile-custom-cacheControl/app/router.js | 5 +++++ .../config/config.default.js | 5 +++++ .../siteFile-custom-cacheControl/package.json | 3 +++ 6 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/apps/siteFile-custom-cacheControl/app/router.js create mode 100644 test/fixtures/apps/siteFile-custom-cacheControl/config/config.default.js create mode 100644 test/fixtures/apps/siteFile-custom-cacheControl/package.json diff --git a/app/middleware/site_file.js b/app/middleware/site_file.js index bca430772..907bd128b 100644 --- a/app/middleware/site_file.js +++ b/app/middleware/site_file.js @@ -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 { * 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', }; /** diff --git a/test/app/middleware/site_file.test.js b/test/app/middleware/site_file.test.js index 0824348b2..f1cd8fa1c 100644 --- a/test/app/middleware/site_file.test.js +++ b/test/app/middleware/site_file.test.js @@ -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); + }); + }); }); diff --git a/test/fixtures/apps/siteFile-custom-cacheControl/app/router.js b/test/fixtures/apps/siteFile-custom-cacheControl/app/router.js new file mode 100644 index 000000000..eb66c8e54 --- /dev/null +++ b/test/fixtures/apps/siteFile-custom-cacheControl/app/router.js @@ -0,0 +1,5 @@ +module.exports = app => { + app.get('/', function*() { + this.body = 'Hi, this is home'; + }); +}; diff --git a/test/fixtures/apps/siteFile-custom-cacheControl/config/config.default.js b/test/fixtures/apps/siteFile-custom-cacheControl/config/config.default.js new file mode 100644 index 000000000..c26defaac --- /dev/null +++ b/test/fixtures/apps/siteFile-custom-cacheControl/config/config.default.js @@ -0,0 +1,5 @@ +exports.siteFile = { + cacheControl: 'no-store', +}; + +exports.keys = 'foo'; diff --git a/test/fixtures/apps/siteFile-custom-cacheControl/package.json b/test/fixtures/apps/siteFile-custom-cacheControl/package.json new file mode 100644 index 000000000..842650eb7 --- /dev/null +++ b/test/fixtures/apps/siteFile-custom-cacheControl/package.json @@ -0,0 +1,3 @@ +{ + "name": "sitefile-custom-cachecontrol" +}