Skip to content

Commit

Permalink
Debug logging when caching Vary: headers (#2916)
Browse files Browse the repository at this point in the history
* Debug logging for vary headers

* Linting
  • Loading branch information
jeffposnick committed Aug 25, 2021
1 parent 9e79454 commit 5da8924
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/workbox-strategies/src/StrategyHandler.ts
Expand Up @@ -319,6 +319,17 @@ class StrategyHandler {
method: effectiveRequest.method,
});
}

// See https://github.com/GoogleChrome/workbox/issues/2818
const vary = response.headers.get('Vary');
if (vary) {
logger.debug(
`The response for ${getFriendlyURL(effectiveRequest.url)} ` +
`has a 'Vary: ${vary}' header. ` +
`Consider setting the {ignoreVary: true} option on your strategy ` +
`to ensure cache matching and deletion works as expected.`,
);
}
}

if (!response) {
Expand Down
24 changes: 22 additions & 2 deletions test/workbox-strategies/sw/test-StrategyHandler.mjs
Expand Up @@ -7,12 +7,13 @@
*/

import {Deferred} from 'workbox-core/_private/Deferred.mjs';
import {timeout} from 'workbox-core/_private/timeout.mjs';
import {logger} from 'workbox-core/_private/logger.mjs';
import {registerQuotaErrorCallback} from 'workbox-core/registerQuotaErrorCallback.mjs';
import {Strategy} from 'workbox-strategies/Strategy.mjs';
import {StrategyHandler} from 'workbox-strategies/StrategyHandler.mjs';
import {spyOnEvent, eventDoneWaiting} from '../../../infra/testing/helpers/extendable-event-utils.mjs';
import {timeout} from 'workbox-core/_private/timeout.mjs';

import {spyOnEvent, eventDoneWaiting} from '../../../infra/testing/helpers/extendable-event-utils.mjs';

class TestStrategy extends Strategy {
_handle() {
Expand Down Expand Up @@ -432,6 +433,25 @@ describe(`StrategyHandler`, function() {
expect(cachePutStub.callCount).to.equal(0);
});

it(`should log when caching a response with a Vary: header in dev mode`, async function() {
if (process.env.NODE_ENV === 'production') {
this.skip();
}

const loggerSpy = sandbox.spy(logger, 'debug');

const request = new Request('/test/vary');
const response = new Response('Vary response', {
headers: {Vary: 'user-agent'},
});

const handler = createStrategyHandler({cacheName: 'vary-test'});
await handler.cachePut(request, response);

// Just a basic test for the logged string.
expect(loggerSpy.firstCall.args[0]).to.include('ignoreVary');
});

it(`should call cacheDidUpdate`, async function() {
const firstPlugin = {
cacheDidUpdate: () => {},
Expand Down

0 comments on commit 5da8924

Please sign in to comment.