From cbb3068a4a9e31a61aa426dc1217fd0ead978e25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Pr=C3=A9vost?= Date: Fri, 24 Mar 2023 17:33:13 +0100 Subject: [PATCH 1/4] GH-14: Don't override cache-control if it has already been set --- index.js | 4 +++- test/headers.test.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 236ddf3..9857cc8 100644 --- a/index.js +++ b/index.js @@ -75,7 +75,9 @@ function fastifyCaching (instance, options, next) { } instance.addHook('onRequest', (req, res, next) => { - res.header('Cache-control', value) + if (!res.getHeader('Cache-control')) { + res.header('Cache-control', value) + } next() }) } diff --git a/test/headers.test.js b/test/headers.test.js index d941239..3b44a41 100644 --- a/test/headers.test.js +++ b/test/headers.test.js @@ -2,6 +2,7 @@ const { test } = require('tap') const Fastify = require('fastify') +const fp = require('fastify-plugin') const plugin = require('..') test('decorators get added', async (t) => { @@ -139,6 +140,37 @@ test('sets public with max-age and s-maxage header', async (t) => { t.equal(response.headers['cache-control'], 'public, max-age=300, s-maxage=12345') }) +test('do not set headers if another upstream plugin already sets it', async (t) => { + t.plan(2) + + const opts = { + privacy: plugin.privacy.PUBLIC, + expiresIn: 300, + serverExpiresIn: 12345 + } + + const fastify = Fastify() + await fastify.register(fp(async (fastify, opts) => { + fastify.addHook('onRequest', async (req, reply) => { + reply.header('cache-control', 'do not override') + }) + })) + await fastify.register(plugin, opts) + + fastify.get('/', (req, reply) => { + reply.send({ hello: 'world' }) + }) + + await fastify.ready() + + const response = await fastify.inject({ + method: 'GET', + path: '/' + }) + t.ok(response.headers['cache-control']) + t.equal(response.headers['cache-control'], 'do not override') +}) + test('only sets max-age and ignores s-maxage with private header', async (t) => { t.plan(2) From af9f77116c8ad60afa28879b040d76313e837475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= <998369+prevostc@users.noreply.github.com> Date: Fri, 24 Mar 2023 19:13:05 +0100 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Manuel Spigolon --- test/headers.test.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/test/headers.test.js b/test/headers.test.js index 3b44a41..9afd1ea 100644 --- a/test/headers.test.js +++ b/test/headers.test.js @@ -2,7 +2,6 @@ const { test } = require('tap') const Fastify = require('fastify') -const fp = require('fastify-plugin') const plugin = require('..') test('decorators get added', async (t) => { @@ -150,11 +149,9 @@ test('do not set headers if another upstream plugin already sets it', async (t) } const fastify = Fastify() - await fastify.register(fp(async (fastify, opts) => { - fastify.addHook('onRequest', async (req, reply) => { - reply.header('cache-control', 'do not override') - }) - })) + fastify.addHook('onRequest', async (req, reply) => { + reply.header('cache-control', 'do not override') + }) await fastify.register(plugin, opts) fastify.get('/', (req, reply) => { From 95baa63c4faf3b661154e7e36faecffd3507627d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Pr=C3=A9vost?= Date: Sat, 25 Mar 2023 13:56:20 +0100 Subject: [PATCH 3/4] use hasHeader instead of getHeader --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 9857cc8..5d7fa6a 100644 --- a/index.js +++ b/index.js @@ -75,7 +75,7 @@ function fastifyCaching (instance, options, next) { } instance.addHook('onRequest', (req, res, next) => { - if (!res.getHeader('Cache-control')) { + if (!res.hasHeader('Cache-control')) { res.header('Cache-control', value) } next() From 6159450daa117aa753cb896816b3b9f2a1f13fd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= <998369+prevostc@users.noreply.github.com> Date: Sat, 25 Mar 2023 14:06:32 +0100 Subject: [PATCH 4/4] Cleaner comparison Co-authored-by: James Sumners --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 5d7fa6a..dfc2184 100644 --- a/index.js +++ b/index.js @@ -75,7 +75,7 @@ function fastifyCaching (instance, options, next) { } instance.addHook('onRequest', (req, res, next) => { - if (!res.hasHeader('Cache-control')) { + if (res.hasHeader('Cache-control') === false) { res.header('Cache-control', value) } next()