mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-07 21:47:57 +08:00
feat: support etag
This commit is contained in:
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user