Skip to content

Commit

Permalink
feat: immutable cache support
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Nov 11, 2021
1 parent 92b9ed7 commit 76a8af7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/header/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ const interpretExpires: HeaderInterpreter = (expires) => {
};

const interpretCacheControl: HeaderInterpreter = (cacheControl, headers) => {
const { noCache, noStore, mustRevalidate, maxAge } = parse(cacheControl);
const { noCache, noStore, mustRevalidate, maxAge, immutable } = parse(cacheControl);

// Header told that this response should not be cached.
if (noCache || noStore) {
return false;
}

if (immutable) {
// 1 year is sufficient, as Infinity may cause more problems.
// It might not be the best way, but a year is better than none.
return 1000 * 60 * 60 * 24 * 365;
}

// Already out of date, for cache can be saved, but must be requested again
if (mustRevalidate) {
return 0;
Expand Down
9 changes: 9 additions & 0 deletions test/header/interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ describe('tests header interpreter', () => {
expect(result).toBe(false);
});

it('tests with immutable', () => {
const result = defaultHeaderInterpreter({
[Header.CacheControl]: 'immutable'
});

// 1 year
expect(result).toBe(1000 * 60 * 60 * 24 * 365);
});

it('tests with future expires', () => {
const date = new Date(new Date().getFullYear() + 1, 1, 1);

Expand Down

0 comments on commit 76a8af7

Please sign in to comment.