@fastify/caching 9.0.4 → 9.0.6

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.
@@ -0,0 +1,24 @@
1
+ name: Release the package
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ inputs:
6
+ semver:
7
+ description: 'Release bump type'
8
+ required: true
9
+ type: choice
10
+ options:
11
+ - patch
12
+ - minor
13
+ - major
14
+
15
+ permissions: {}
16
+
17
+ jobs:
18
+ release:
19
+ permissions:
20
+ id-token: write # required for npm provenance via OIDC
21
+ contents: write # required to push the release commit and the tag
22
+ uses: fastify/workflows/.github/workflows/reusable-release.yml@731d0cca9a6a5b9586dad6b0326d49001ab9216f
23
+ with:
24
+ semver: ${{ inputs.semver }}
package/index.js CHANGED
@@ -72,10 +72,10 @@ function fastifyCaching (instance, options, next) {
72
72
 
73
73
  instance.addHook('onSend', function cachingSetCacheControlHeader (_req, reply, _payload, next) {
74
74
  if (!reply.hasHeader('Cache-control')) {
75
- // Only apply the caching value to successful (2xx) responses.
75
+ // Only apply the caching value to successful (2xx) and 304 Not Modified responses.
76
76
  // Error responses must not be cached, otherwise browsers may serve
77
77
  // a stale 4xx/5xx for the duration of max-age.
78
- if (reply.statusCode >= 200 && reply.statusCode < 300) {
78
+ if ((reply.statusCode >= 200 && reply.statusCode < 300) || reply.statusCode === 304) {
79
79
  reply.header('Cache-control', value)
80
80
  } else {
81
81
  reply.header('Cache-control', 'no-store')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/caching",
3
- "version": "9.0.4",
3
+ "version": "9.0.6",
4
4
  "description": "A plugin for Fastify to enable management of cache control headers",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -346,6 +346,66 @@ test('does not override a Cache-control header set upstream on error responses',
346
346
  t.assert.strictEqual(response.headers['cache-control'], 'private, max-age=10')
347
347
  })
348
348
 
349
+ test('sets configured Cache-control header on 304 Not Modified responses', async (t) => {
350
+ t.plan(2)
351
+
352
+ const opts = {
353
+ privacy: plugin.privacy.PRIVATE,
354
+ expiresIn: 300
355
+ }
356
+
357
+ const fastify = Fastify()
358
+ await fastify.register(plugin, opts)
359
+
360
+ fastify.get('/', (_req, reply) => {
361
+ reply.code(304).send()
362
+ })
363
+
364
+ await fastify.ready()
365
+
366
+ const response = await fastify.inject({
367
+ method: 'GET',
368
+ path: '/'
369
+ })
370
+ t.assert.strictEqual(response.statusCode, 304)
371
+ t.assert.strictEqual(response.headers['cache-control'], 'private, max-age=300')
372
+ })
373
+
374
+ test('sets configured Cache-control header on conditional 304 etag hit responses', async (t) => {
375
+ t.plan(4)
376
+
377
+ const opts = {
378
+ privacy: plugin.privacy.PUBLIC,
379
+ expiresIn: 300
380
+ }
381
+
382
+ const fastify = Fastify()
383
+ await fastify.register(plugin, opts)
384
+
385
+ fastify.get('/', (_req, reply) => {
386
+ reply.etag('123456').send({ hello: 'world' })
387
+ })
388
+
389
+ await fastify.ready()
390
+
391
+ const first = await fastify.inject({
392
+ method: 'GET',
393
+ path: '/'
394
+ })
395
+ t.assert.strictEqual(first.statusCode, 200)
396
+ t.assert.strictEqual(first.headers['cache-control'], 'public, max-age=300')
397
+
398
+ const second = await fastify.inject({
399
+ method: 'GET',
400
+ path: '/',
401
+ headers: {
402
+ 'if-none-match': '123456'
403
+ }
404
+ })
405
+ t.assert.strictEqual(second.statusCode, 304)
406
+ t.assert.strictEqual(second.headers['cache-control'], 'public, max-age=300')
407
+ })
408
+
349
409
  test('sets the expires header', async (t) => {
350
410
  t.plan(2)
351
411