From 1633f418432e1373184c630c625c4641cef7a144 Mon Sep 17 00:00:00 2001 From: saltyaom Date: Wed, 24 Apr 2024 19:22:14 +0700 Subject: [PATCH] :tada: feat: release 1.0.3 --- src/index.ts | 6 +++--- test/index.test.ts | 32 +++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index cfb8056..2ec99c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -252,7 +252,7 @@ export const staticPlugin = async ( headers['Etag'] = etag headers['Cache-Control'] = directive - if (maxAge) + if (maxAge !== null) headers['Cache-Control'] += `, max-age=${maxAge}` return new Response(file, { @@ -278,7 +278,7 @@ export const staticPlugin = async ( headers['Etag'] = etag headers['Cache-Control'] = directive - if (maxAge) + if (maxAge !== null) headers[ 'Cache-Control' ] += `, max-age=${maxAge}` @@ -371,7 +371,7 @@ export const staticPlugin = async ( headers['Etag'] = etag headers['Cache-Control'] = directive - if (maxAge) + if (maxAge !== null) headers['Cache-Control'] += `, max-age=${maxAge}` return new Response(file, { diff --git a/test/index.test.ts b/test/index.test.ts index 98ee565..388d44b 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -272,7 +272,37 @@ describe('Static Plugin', () => { const res = await app.handle(req('/public/takodachi')) - expect(res.headers.get('Cache-Control')).toBe('public, max-age=0') + expect(res.headers.get('Cache-Control')).toBe('public, max-age=86400') + expect(res.status).toBe(200) + }) + + it('skip Cache-Control header when maxAge is null', async () => { + const app = new Elysia().use( + staticPlugin({ + maxAge: null + }) + ) + + await app.modules + + const res = await app.handle(req('/public/takodachi.png')) + + expect(res.headers.get('Cache-Control')).toBe('public') + expect(res.status).toBe(200) + }) + + it('set cache directive', async () => { + const app = new Elysia().use( + staticPlugin({ + directive: 'private' + }) + ) + + await app.modules + + const res = await app.handle(req('/public/takodachi.png')) + + expect(res.headers.get('Cache-Control')).toBe('private, max-age=86400') expect(res.status).toBe(200) })