Skip to content

Commit

Permalink
Handle a TTL of 0 correctly
Browse files Browse the repository at this point in the history
When a TTL override of 0 is set, the override is overriden with the
policy due to using the || operator, which considers 0 to be false.
  • Loading branch information
sebnow committed Apr 18, 2019
1 parent 5a0e848 commit 4aad590
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/apollo-datasource-rest/src/HTTPCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class HTTPCache {
return response;
}

let ttl = ttlOverride || Math.round(policy.timeToLive() / 1000);
let ttl = ttlOverride === undefined ? Math.round(policy.timeToLive() / 1000) : ttlOverride;
if (ttl <= 0) return response;

// If a response can be revalidated, we don't want to remove it from the cache right after it expires.
Expand Down
15 changes: 15 additions & 0 deletions packages/apollo-datasource-rest/src/__tests__/HTTPCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@ describe('HTTPCache', () => {
expect(await response.json()).toEqual({ name: 'Ada Lovelace' });
expect(response.headers.get('Age')).toEqual('10');
});

it('allows disabling caching when the TTL is 0 (falsy)', async () => {
fetch.mockJSONResponseOnce(
{ name: 'Ada Lovelace' },
{ 'Cache-Control': 'max-age=30' },
);

await httpCache.fetch(new Request('https://api.example.com/people/1'), {
cacheOptions: (response: Response, request: Request) => ({
ttl: 0,
}),
});

expect(store.size).toEqual(0);
});
});

it('allows specifying a custom cache key', async () => {
Expand Down

0 comments on commit 4aad590

Please sign in to comment.