@@ -138,6 +138,7 @@ class CacheHandler extends DecoratorHandler {
138138 statusMessage,
139139 rawHeaders : strippedHeaders ,
140140 vary : varyDirectives ,
141+ cacheControlDirectives,
141142 cachedAt : now ,
142143 staleAt,
143144 deleteAt
@@ -227,7 +228,7 @@ class CacheHandler extends DecoratorHandler {
227228 *
228229 * @param {number } statusCode
229230 * @param {Record<string, string | string[]> } headers
230- * @param {import('../util/ cache.js') .CacheControlDirectives } cacheControlDirectives
231+ * @param {import('../../types/ cache-interceptor.d.ts').default .CacheControlDirectives } cacheControlDirectives
231232 */
232233function canCacheResponse ( statusCode , headers , cacheControlDirectives ) {
233234 if ( statusCode !== 200 && statusCode !== 307 ) {
@@ -274,7 +275,7 @@ function canCacheResponse (statusCode, headers, cacheControlDirectives) {
274275/**
275276 * @param {number } now
276277 * @param {Record<string, string | string[]> } headers
277- * @param {import('../util/ cache.js') .CacheControlDirectives } cacheControlDirectives
278+ * @param {import('../../types/ cache-interceptor.d.ts').default .CacheControlDirectives } cacheControlDirectives
278279 *
279280 * @returns {number | undefined } time that the value is stale at or undefined if it shouldn't be cached
280281 */
@@ -310,22 +311,29 @@ function determineStaleAt (now, headers, cacheControlDirectives) {
310311
311312/**
312313 * @param {number } now
313- * @param {import('../util/ cache.js') .CacheControlDirectives } cacheControlDirectives
314+ * @param {import('../../types/ cache-interceptor.d.ts').default .CacheControlDirectives } cacheControlDirectives
314315 * @param {number } staleAt
315316 */
316317function determineDeleteAt ( now , cacheControlDirectives , staleAt ) {
318+ let staleWhileRevalidate = - Infinity
319+ let staleIfError = - Infinity
320+
317321 if ( cacheControlDirectives [ 'stale-while-revalidate' ] ) {
318- return now + ( cacheControlDirectives [ 'stale-while-revalidate' ] * 1000 )
322+ staleWhileRevalidate = now + ( cacheControlDirectives [ 'stale-while-revalidate' ] * 1000 )
323+ }
324+
325+ if ( cacheControlDirectives [ 'stale-if-error' ] ) {
326+ staleIfError = now + ( cacheControlDirectives [ 'stale-if-error' ] * 1000 )
319327 }
320328
321- return staleAt
329+ return Math . max ( staleAt , staleWhileRevalidate , staleIfError )
322330}
323331
324332/**
325333 * Strips headers required to be removed in cached responses
326334 * @param {Buffer[] } rawHeaders
327335 * @param {string[] } parsedRawHeaders
328- * @param {import('../util/ cache.js') .CacheControlDirectives } cacheControlDirectives
336+ * @param {import('../../types/ cache-interceptor.d.ts').default .CacheControlDirectives } cacheControlDirectives
329337 * @returns {Buffer[] }
330338 */
331339function stripNecessaryHeaders ( rawHeaders , parsedRawHeaders , cacheControlDirectives ) {
0 commit comments