-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement explicit cache control configuration
- Loading branch information
Showing
9 changed files
with
86 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { createResponseHeaders } from './headers'; | ||
|
||
describe(createResponseHeaders.name, () => { | ||
it('returns common headers when cache is not set', () => { | ||
const expectedHeaders = { | ||
'content-type': 'application/json', | ||
'access-control-allow-origin': '*', | ||
'access-control-allow-methods': '*', | ||
}; | ||
expect(createResponseHeaders()).toStrictEqual(expectedHeaders); | ||
}); | ||
|
||
it('returns browser cache headers when cache type is browser', () => { | ||
const expectedHeaders = { | ||
'content-type': 'application/json', | ||
'access-control-allow-origin': '*', | ||
'access-control-allow-methods': '*', | ||
'cache-control': 'max-age=3600', | ||
}; | ||
expect( | ||
createResponseHeaders({ | ||
type: 'browser', | ||
maxAgeSeconds: 3600, | ||
}) | ||
).toStrictEqual(expectedHeaders); | ||
}); | ||
|
||
it('returns CDN cache headers when cache type is cdn', () => { | ||
const expectedHeaders = { | ||
'content-type': 'application/json', | ||
'access-control-allow-origin': '*', | ||
'access-control-allow-methods': '*', | ||
'cache-control': 'no-store', | ||
'cdn-cache-control': 'max-age=3600', | ||
}; | ||
expect( | ||
createResponseHeaders({ | ||
type: 'cdn', | ||
maxAgeSeconds: 3600, | ||
}) | ||
).toStrictEqual(expectedHeaders); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { Cache } from './schema'; | ||
|
||
const COMMON_HEADERS = { | ||
'content-type': 'application/json', | ||
'access-control-allow-origin': '*', | ||
'access-control-allow-methods': '*', | ||
}; | ||
|
||
export const createResponseHeaders = (cache?: Cache | undefined) => { | ||
if (!cache) return COMMON_HEADERS; | ||
|
||
const { type, maxAgeSeconds } = cache; | ||
switch (type) { | ||
case 'browser': { | ||
return { | ||
...COMMON_HEADERS, | ||
'cache-control': `max-age=${maxAgeSeconds}`, | ||
}; | ||
} | ||
case 'cdn': { | ||
return { | ||
...COMMON_HEADERS, | ||
'cache-control': 'no-store', // Disable browser-caching | ||
'cdn-cache-control': `max-age=${maxAgeSeconds}`, | ||
}; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters