feat: support etag

This commit is contained in:
DIYgod
2020-09-29 18:53:24 +08:00
parent 6676fefe1a
commit 6ec230f58f
4 changed files with 28 additions and 17 deletions

View File

@@ -19,8 +19,7 @@ module.exports = async (ctx, next) => {
await next();
let body = ctx.body;
if (!body || typeof body !== 'string' || ctx.response.get('ETag')) {
if (!ctx.body || typeof ctx.body !== 'string' || ctx.response.get('ETag')) {
return;
}
@@ -29,14 +28,17 @@ module.exports = async (ctx, next) => {
return;
}
const match = body.match(/<lastBuildDate>(.*)<\/lastBuildDate>/);
if (match) {
ctx.set({
'Last-Modified': body.match(/<lastBuildDate>(.*)<\/lastBuildDate>/)[1],
});
ctx.set('ETag', etagCalculate(ctx.body.replace(/<lastBuildDate>(.*)<\/lastBuildDate>/, '').replace(/<atom:link(.*)\/>/, '')));
if (ctx.fresh) {
ctx.status = 304;
ctx.body = null;
} else {
const match = ctx.body.match(/<lastBuildDate>(.*)<\/lastBuildDate>/);
if (match) {
ctx.set({
'Last-Modified': match[1],
});
}
}
body = body.replace(/<lastBuildDate>(.*)<\/lastBuildDate>/, '');
ctx.response.etag = etagCalculate(body);
};