Skip to content

Commit

Permalink
feat: siteFile support custom control-cache (#4902)
Browse files Browse the repository at this point in the history
Co-authored-by: yangbing <yangbing@immotors.com>
  • Loading branch information
binginsist and yangbing authored Mar 16, 2022
1 parent a7aa7f3 commit caacd09
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 2 deletions.
3 changes: 1 addition & 2 deletions app/middleware/site_file.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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',
};

/**
Expand Down
16 changes: 16 additions & 0 deletions test/app/middleware/site_file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
5 changes: 5 additions & 0 deletions test/fixtures/apps/siteFile-custom-cacheControl/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = app => {
app.get('/', function*() {
this.body = 'Hi, this is home';
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.siteFile = {
cacheControl: 'no-store',
};

exports.keys = 'foo';
3 changes: 3 additions & 0 deletions test/fixtures/apps/siteFile-custom-cacheControl/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "sitefile-custom-cachecontrol"
}

1 comment on commit caacd09

@vercel
Copy link

@vercel vercel bot commented on caacd09 Mar 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.