mirror of
https://github.com/eggjs/egg.git
synced 2024-12-04 07:14:30 +00:00
feat: siteFile support custom control-cache (#4902)
Co-authored-by: yangbing <yangbing@immotors.com>
This commit is contained in:
parent
a7aa7f37d9
commit
caacd09c38
@ -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;
|
||||
|
||||
@ -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',
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -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
test/fixtures/apps/siteFile-custom-cacheControl/app/router.js
vendored
Normal file
5
test/fixtures/apps/siteFile-custom-cacheControl/app/router.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
module.exports = app => {
|
||||
app.get('/', function*() {
|
||||
this.body = 'Hi, this is home';
|
||||
});
|
||||
};
|
||||
5
test/fixtures/apps/siteFile-custom-cacheControl/config/config.default.js
vendored
Normal file
5
test/fixtures/apps/siteFile-custom-cacheControl/config/config.default.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
exports.siteFile = {
|
||||
cacheControl: 'no-store',
|
||||
};
|
||||
|
||||
exports.keys = 'foo';
|
||||
3
test/fixtures/apps/siteFile-custom-cacheControl/package.json
vendored
Normal file
3
test/fixtures/apps/siteFile-custom-cacheControl/package.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"name": "sitefile-custom-cachecontrol"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user