Skip to content

Commit 8c5d8ed

Browse files
committed
Set Header before route render to avoid sending before response is sent
1 parent fb0907b commit 8c5d8ed

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@luispittagros/nuxt-ssr-redis-cache",
3-
"version": "1.1.4",
3+
"version": "1.1.5",
44
"description": "Blazing Fast SSR page renderings using Redis.",
55
"main": "src/cache.js",
66
"author": "Luís Pitta Grós",

src/cache.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,18 @@ export default async function nuxtRedisCache(moduleOptions) {
6161

6262
context.req.hitCache = !!value
6363

64+
context.res.setHeader('X-Cache', value ? 'HIT' : 'MISS')
65+
6466
if (!value) {
6567
return renderRoute(route, context)
6668
}
6769

68-
return new Promise(function (resolve) {
69-
resolve(deserialize(value))
70-
})
70+
return new Promise((resolve) => resolve(deserialize(value)))
7171
}
7272
})
7373

7474
this.nuxt.hook('render:route', async (url, result, context) => {
75-
const hitCache = context.req.hitCache
76-
77-
if (hitCache !== undefined) {
78-
context.res.setHeader('X-Cache', hitCache ? 'HIT' : 'MISS')
79-
}
80-
81-
if (!hitCache && isCacheable(url, options.paths, context.req.headers['cache-control'])) {
75+
if (!context.req.hitCache && isCacheable(url, options.paths, context.req.headers['cache-control'])) {
8276
client.set('nuxt/route::' + url, serialize(result), {
8377
EX: options.ttl,
8478
})

0 commit comments

Comments
 (0)