Skip to content

Commit f91689a

Browse files
committed
fix: only cache 200, 203 status codes, add tests
1 parent f831377 commit f91689a

File tree

2 files changed

+228
-48
lines changed

2 files changed

+228
-48
lines changed

src/http-data-source.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ export interface HTTPDataSourceOptions {
7070
lru?: Partial<LRUOptions>
7171
}
7272

73-
// https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_success
74-
const cacheableStatusCodes = [200, 201, 202, 203, 206]
73+
// rfc7231 6.1
74+
// We only cache status codes that indicates a successful response
75+
// We don't cache redirects, client errors because we expect to cache JSON payload.
76+
const statusCodeCacheableByDefault = new Set([200, 203])
7577

7678
/**
7779
* HTTPDataSource is an optimized HTTP Data Source for Apollo Server
@@ -126,7 +128,7 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
126128
request: Request,
127129
response: Response<TResult>,
128130
): boolean {
129-
return cacheableStatusCodes.indexOf(response.statusCode) > -1 && request.method === 'GET'
131+
return statusCodeCacheableByDefault.has(response.statusCode) && request.method === 'GET'
130132
}
131133

132134
/**

0 commit comments

Comments
 (0)