@@ -113,6 +113,7 @@ class CacheHandler {
113113 statusMessage,
114114 headers : strippedHeaders ,
115115 vary : varyDirectives ,
116+ cacheControlDirectives,
116117 cachedAt : now ,
117118 staleAt,
118119 deleteAt
@@ -170,7 +171,7 @@ class CacheHandler {
170171 *
171172 * @param {number } statusCode
172173 * @param {Record<string, string | string[]> } headers
173- * @param {import('../util/ cache.js') .CacheControlDirectives } cacheControlDirectives
174+ * @param {import('../../types/ cache-interceptor.d.ts').default .CacheControlDirectives } cacheControlDirectives
174175 */
175176function canCacheResponse ( statusCode , headers , cacheControlDirectives ) {
176177 if ( statusCode !== 200 && statusCode !== 307 ) {
@@ -217,7 +218,7 @@ function canCacheResponse (statusCode, headers, cacheControlDirectives) {
217218/**
218219 * @param {number } now
219220 * @param {Record<string, string | string[]> } headers
220- * @param {import('../util/ cache.js') .CacheControlDirectives } cacheControlDirectives
221+ * @param {import('../../types/ cache-interceptor.d.ts').default .CacheControlDirectives } cacheControlDirectives
221222 *
222223 * @returns {number | undefined } time that the value is stale at or undefined if it shouldn't be cached
223224 */
@@ -253,22 +254,36 @@ function determineStaleAt (now, headers, cacheControlDirectives) {
253254
254255/**
255256 * @param {number } now
256- * @param {import('../util/ cache.js') .CacheControlDirectives } cacheControlDirectives
257+ * @param {import('../../types/ cache-interceptor.d.ts').default .CacheControlDirectives } cacheControlDirectives
257258 * @param {number } staleAt
258259 */
259260function determineDeleteAt ( now , cacheControlDirectives , staleAt ) {
261+ let staleWhileRevalidate = - Infinity
262+ let staleIfError = - Infinity
263+
260264 if ( cacheControlDirectives [ 'stale-while-revalidate' ] ) {
261- return now + ( cacheControlDirectives [ 'stale-while-revalidate' ] * 1000 )
265+ staleWhileRevalidate = now + ( cacheControlDirectives [ 'stale-while-revalidate' ] * 1000 )
266+ }
267+
268+ if ( cacheControlDirectives [ 'stale-if-error' ] ) {
269+ staleIfError = now + ( cacheControlDirectives [ 'stale-if-error' ] * 1000 )
262270 }
263271
264- return staleAt
272+ return Math . max ( staleAt , staleWhileRevalidate , staleIfError )
265273}
266274
267275/**
268276 * Strips headers required to be removed in cached responses
277+ <<<<<<< HEAD
269278 * @param {Record<string, string | string[]> } headers
270279 * @param {import('../util/cache.js').CacheControlDirectives } cacheControlDirectives
271280 * @returns {Record<string, string | string []> }
281+ =======
282+ * @param {Buffer[] } rawHeaders
283+ * @param {string[] } parsedRawHeaders
284+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives } cacheControlDirectives
285+ * @returns {Buffer[] }
286+ >>>>>>> 45c0793e (cache: fix stale-while-revalidate and stale-if-error)
272287 */
273288function stripNecessaryHeaders ( headers , cacheControlDirectives ) {
274289 const headersToRemove = [ 'connection' ]
0 commit comments