diff --git a/lib/handler/cache-revalidation-handler.js b/lib/handler/cache-revalidation-handler.js index 01a809e487e..c89879e1f61 100644 --- a/lib/handler/cache-revalidation-handler.js +++ b/lib/handler/cache-revalidation-handler.js @@ -19,26 +19,26 @@ const DecoratorHandler = require('../handler/decorator-handler') class CacheRevalidationHandler extends DecoratorHandler { #successful = false /** - * @type {(() => void)} + * @type {((boolean) => void)} */ - #successCallback + #callback /** * @type {(import('../../types/dispatcher.d.ts').default.DispatchHandlers)} */ #handler /** - * @param {() => void} successCallback Function to call if the cached value is valid + * @param {(boolean) => void} callback Function to call if the cached value is valid * @param {import('../../types/dispatcher.d.ts').default.DispatchHandlers} handler */ - constructor (successCallback, handler) { - if (typeof successCallback !== 'function') { - throw new TypeError('successCallback must be a function') + constructor (callback, handler) { + if (typeof callback !== 'function') { + throw new TypeError('callback must be a function') } super(handler) - this.#successCallback = successCallback + this.#callback = callback this.#handler = handler } @@ -107,9 +107,9 @@ class CacheRevalidationHandler extends DecoratorHandler { * @param {string[] | null} rawTrailers */ onComplete (rawTrailers) { - if (this.#successful) { - this.#successCallback() - } else if (typeof this.#handler.onComplete === 'function') { + this.#callback(this.#successful) + + if (!this.#successful && typeof this.#handler.onComplete === 'function') { this.#handler.onComplete(rawTrailers) } } @@ -120,6 +120,8 @@ class CacheRevalidationHandler extends DecoratorHandler { * @param {Error} err */ onError (err) { + this.#callback(false) + if (typeof this.#handler.onError === 'function') { this.#handler.onError(err) } diff --git a/lib/interceptor/cache.js b/lib/interceptor/cache.js index c7409cc141c..d4530cdc72a 100644 --- a/lib/interceptor/cache.js +++ b/lib/interceptor/cache.js @@ -145,8 +145,15 @@ module.exports = (opts = {}) => { 'if-modified-since': new Date(value.cachedAt).toUTCString() } }, + // TODO (fix): This is suspect... new CacheRevalidationHandler( - () => respondWithCachedValue(stream, value), + (success) => { + if (success) { + respondWithCachedValue(stream, value) + } else { + stream.on('error', () => {}).destroy() + } + }, new CacheHandler(globalOpts, opts, handler) ) )