Skip to content

Commit

Permalink
Merge pull request #24 from sjdonado/main
Browse files Browse the repository at this point in the history
feat: add maxAge param to Cache-Control header
  • Loading branch information
SaltyAom authored Apr 24, 2024
2 parents e403d35 + f56e43e commit f3f0607
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
resolve = resolveFn,
headers = {},
noCache = false,
maxAge = 0,
indexHTML = true
}: {
/**
Expand Down Expand Up @@ -122,6 +123,15 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
* If set to true, browser caching will be disabled
*/
noCache?: boolean
/**
* @default 0
*
* Specifies the maximum amount of time in seconds, a resource will be considered fresh.
* This freshness lifetime is calculated relative to the time of the request.
* This setting helps control browser caching behavior.
* A `maxAge` of 0 will prevent caching, requiring requests to validate with the server before use.
*/
maxAge?: number
/**
* @default true
*
Expand Down Expand Up @@ -208,7 +218,7 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
}

headers['Etag'] = etag
headers['Cache-Control'] = 'public, max-age=0'
headers['Cache-Control'] = `public, max-age=${maxAge}`

return new Response(file, {
headers
Expand All @@ -232,7 +242,8 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
}

headers['Etag'] = etag
headers['Cache-Control'] = 'public, max-age=0'
headers['Cache-Control'] =
`public, max-age=${maxAge}`

return new Response(file, {
headers
Expand Down Expand Up @@ -314,7 +325,7 @@ export const staticPlugin = async <Prefix extends string = '/prefix'>(
})

headers['Etag'] = etag
headers['Cache-Control'] = 'public, max-age=0'
headers['Cache-Control'] = `public, max-age=${maxAge}`

return new Response(file, {
headers
Expand Down
33 changes: 33 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,39 @@ describe('Static Plugin', () => {
expect(res.status).toBe(200)
})

it('return Cache-Control header when maxAge is set', async () => {
const app = new Elysia().use(
staticPlugin({
alwaysStatic: true,
noExtension: true,
maxAge: 3600
})
)

await app.modules

const res = await app.handle(req('/public/takodachi'))

expect(res.headers.get('Cache-Control')).toBe('public, max-age=3600')
expect(res.status).toBe(200)
})

it('return Cache-Control header when maxAge is not set', async () => {
const app = new Elysia().use(
staticPlugin({
alwaysStatic: true,
noExtension: true
})
)

await app.modules

const res = await app.handle(req('/public/takodachi'))

expect(res.headers.get('Cache-Control')).toBe('public, max-age=0')
expect(res.status).toBe(200)
})

it('return not modified response (etag)', async () => {
const app = new Elysia().use(
staticPlugin({
Expand Down

0 comments on commit f3f0607

Please sign in to comment.