@@ -144,6 +144,7 @@ class CacheHandler extends DecoratorHandler {
144144 statusMessage,
145145 rawHeaders : strippedHeaders ,
146146 vary : varyDirectives ,
147+ cacheControlDirectives,
147148 cachedAt : now ,
148149 staleAt,
149150 deleteAt
@@ -233,7 +234,7 @@ class CacheHandler extends DecoratorHandler {
233234 *
234235 * @param {number } statusCode
235236 * @param {Record<string, string | string[]> } headers
236- * @param {import('../util/ cache.js') .CacheControlDirectives } cacheControlDirectives
237+ * @param {import('../../types/ cache-interceptor.d.ts').default .CacheControlDirectives } cacheControlDirectives
237238 */
238239function canCacheResponse ( statusCode , headers , cacheControlDirectives ) {
239240 if (
@@ -283,7 +284,7 @@ function canCacheResponse (statusCode, headers, cacheControlDirectives) {
283284/**
284285 * @param {number } now
285286 * @param {Record<string, string | string[]> } headers
286- * @param {import('../util/ cache.js') .CacheControlDirectives } cacheControlDirectives
287+ * @param {import('../../types/ cache-interceptor.d.ts').default .CacheControlDirectives } cacheControlDirectives
287288 *
288289 * @returns {number | undefined } time that the value is stale at or undefined if it shouldn't be cached
289290 */
@@ -319,22 +320,29 @@ function determineStaleAt (now, headers, cacheControlDirectives) {
319320
320321/**
321322 * @param {number } now
322- * @param {import('../util/ cache.js') .CacheControlDirectives } cacheControlDirectives
323+ * @param {import('../../types/ cache-interceptor.d.ts').default .CacheControlDirectives } cacheControlDirectives
323324 * @param {number } staleAt
324325 */
325326function determineDeleteAt ( now , cacheControlDirectives , staleAt ) {
327+ let staleWhileRevalidate = - Infinity
328+ let staleIfError = - Infinity
329+
326330 if ( cacheControlDirectives [ 'stale-while-revalidate' ] ) {
327- return now + ( cacheControlDirectives [ 'stale-while-revalidate' ] * 1000 )
331+ staleWhileRevalidate = now + ( cacheControlDirectives [ 'stale-while-revalidate' ] * 1000 )
332+ }
333+
334+ if ( cacheControlDirectives [ 'stale-if-error' ] ) {
335+ staleIfError = now + ( cacheControlDirectives [ 'stale-if-error' ] * 1000 )
328336 }
329337
330- return staleAt
338+ return Math . max ( staleAt , staleWhileRevalidate , staleIfError )
331339}
332340
333341/**
334342 * Strips headers required to be removed in cached responses
335343 * @param {Buffer[] } rawHeaders
336344 * @param {string[] } parsedRawHeaders
337- * @param {import('../util/ cache.js') .CacheControlDirectives } cacheControlDirectives
345+ * @param {import('../../types/ cache-interceptor.d.ts').default .CacheControlDirectives } cacheControlDirectives
338346 * @returns {Buffer[] }
339347 */
340348function stripNecessaryHeaders ( rawHeaders , parsedRawHeaders , cacheControlDirectives ) {
0 commit comments