From d64ca7b72465cdd69939dbf8f9ef976fcc9d3280 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 17 Jan 2019 15:55:34 +0800 Subject: [PATCH] fix Content-Type in cached response --- lib/middleware/lru-cache.js | 8 +++++--- lib/middleware/redis-cache.js | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/middleware/lru-cache.js b/lib/middleware/lru-cache.js index c777b02e2..d839c5088 100644 --- a/lib/middleware/lru-cache.js +++ b/lib/middleware/lru-cache.js @@ -86,8 +86,10 @@ module.exports = function(options = {}) { if (Buffer.isBuffer(type)) { type = type.toString(); } - ctx.response.set('X-Koa-Memory-Cache', 'true'); - ctx.response.type = type; + ctx.response.set({ + 'X-Koa-Memory-Cache': 'true', + 'Content-Type': type, + }); try { ctx.state.data = JSON.parse(value); } catch (e) { @@ -121,7 +123,7 @@ module.exports = function(options = {}) { * cacheType */ async function cacheType(ctx, tkey) { - const type = ctx.response.type; + const type = ctx.response.headers['content-type']; if (type) { memoryCache.set(tkey, type); } diff --git a/lib/middleware/redis-cache.js b/lib/middleware/redis-cache.js index 8e911a394..bc06d217f 100644 --- a/lib/middleware/redis-cache.js +++ b/lib/middleware/redis-cache.js @@ -107,8 +107,10 @@ module.exports = function(options = {}) { if (Buffer.isBuffer(type)) { type = type.toString(); } - ctx.response.set('X-Koa-Redis-Cache', 'true'); - ctx.response.type = type; + ctx.response.set({ + 'X-Koa-Redis-Cache': 'true', + 'Content-Type': type, + }); try { ctx.state.data = JSON.parse(value); } catch (e) { @@ -142,7 +144,7 @@ module.exports = function(options = {}) { * cacheType */ async function cacheType(ctx, tkey, expire) { - const type = ctx.response.type; + const type = ctx.response.headers['content-type']; if (type) { await redisClient.setex(tkey, expire, type); }