@fastify/caching 8.1.0 → 8.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -75,7 +75,9 @@ function fastifyCaching (instance, options, next) {
75
75
  }
76
76
 
77
77
  instance.addHook('onRequest', (req, res, next) => {
78
- res.header('Cache-control', value)
78
+ if (res.hasHeader('Cache-control') === false) {
79
+ res.header('Cache-control', value)
80
+ }
79
81
  next()
80
82
  })
81
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/caching",
3
- "version": "8.1.0",
3
+ "version": "8.3.0",
4
4
  "description": "A plugin for Fastify to enable management of cache control headers",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
@@ -38,7 +38,7 @@
38
38
  "snazzy": "^9.0.0",
39
39
  "standard": "^17.0.0",
40
40
  "tap": "^16.0.0",
41
- "tsd": "^0.25.0"
41
+ "tsd": "^0.28.0"
42
42
  },
43
43
  "dependencies": {
44
44
  "abstract-cache": "^1.0.1",
@@ -139,6 +139,35 @@ test('sets public with max-age and s-maxage header', async (t) => {
139
139
  t.equal(response.headers['cache-control'], 'public, max-age=300, s-maxage=12345')
140
140
  })
141
141
 
142
+ test('do not set headers if another upstream plugin already sets it', async (t) => {
143
+ t.plan(2)
144
+
145
+ const opts = {
146
+ privacy: plugin.privacy.PUBLIC,
147
+ expiresIn: 300,
148
+ serverExpiresIn: 12345
149
+ }
150
+
151
+ const fastify = Fastify()
152
+ fastify.addHook('onRequest', async (req, reply) => {
153
+ reply.header('cache-control', 'do not override')
154
+ })
155
+ await fastify.register(plugin, opts)
156
+
157
+ fastify.get('/', (req, reply) => {
158
+ reply.send({ hello: 'world' })
159
+ })
160
+
161
+ await fastify.ready()
162
+
163
+ const response = await fastify.inject({
164
+ method: 'GET',
165
+ path: '/'
166
+ })
167
+ t.ok(response.headers['cache-control'])
168
+ t.equal(response.headers['cache-control'], 'do not override')
169
+ })
170
+
142
171
  test('only sets max-age and ignores s-maxage with private header', async (t) => {
143
172
  t.plan(2)
144
173