feat(core): add edge and last-modified header, close #338

This commit is contained in:
DIYgod
2019-04-26 11:59:17 +08:00
parent 6692c4a4f4
commit 801f75c732
4 changed files with 27 additions and 1 deletions

View File

@@ -1,3 +1,4 @@
const etagCalculate = require('etag');
const logger = require('../utils/logger');
const config = require('../config');
const headers = {
@@ -11,5 +12,27 @@ const headers = {
module.exports = async (ctx, next) => {
logger.info(`${ctx.url}, user IP: ${ctx.ips[0] || ctx.ip}`);
ctx.set(headers);
await next();
let body = ctx.body;
if (!body || typeof body !== 'string' || ctx.response.get('ETag')) {
return;
}
const status = (ctx.status / 100) | 0;
if (2 !== status) {
return;
}
const match = body.match(/<lastBuildDate>(.*)<\/lastBuildDate>/);
if (match) {
ctx.set({
'Last-Modified': body.match(/<lastBuildDate>(.*)<\/lastBuildDate>/)[1],
});
}
body = body.replace(/<lastBuildDate>(.*)<\/lastBuildDate>/, '');
ctx.response.etag = etagCalculate(body);
};