Skip to content

Commit

Permalink
Ensure that ignoreUrlParametersMatching works with directoryIndex (#762)
Browse files Browse the repository at this point in the history
* Ensure that ignoreUrlParametersMatching works with directoryIndex

* Changed the stubbing a bit to make Firefox happy.

* Cleaned up the test a bit.

* Add in checks for the cache.match() arguments.
  • Loading branch information
jeffposnick authored Sep 6, 2017
1 parent 2d24aa7 commit 30f09c6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/workbox-sw/src/lib/workbox-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ class WorkboxSW {
}

if (directoryIndex && strippedUrl.pathname.endsWith('/')) {
url.pathname += directoryIndex;
return cachedUrls.indexOf(url.href) !== -1;
strippedUrl.pathname += directoryIndex;
return cachedUrls.indexOf(strippedUrl.href) !== -1;
}

return false;
Expand Down
48 changes: 48 additions & 0 deletions packages/workbox-sw/test/sw/directory-index-custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,52 @@ describe(`Test Directory Index`, function() {
self.dispatchEvent(fetchEvent);
});
});

it(`should take the ignoreUrlParametersMatching setting into account when using the directoryIndex`, function() {
const urlParameter = 'test';
const ignoreUrlParametersMatching = [new RegExp(urlParameter)];
const baseUrl = '/example/url/';
const directoryIndex = 'custom.html';

const urlToPrecache = new URL(baseUrl + directoryIndex, self.location).href;
const urlToRequest = new URL(baseUrl + '?' + urlParameter, self.location).href;

// We need to create a stub for match() that returns a real Promise,
// rather than null, or else Firefox is unhappy.
const stub = sinon.stub(Cache.prototype, 'match')
.callsFake(() => Promise.resolve(null));
stubs.push(stub);

const workboxSW = new WorkboxSW({
directoryIndex,
ignoreUrlParametersMatching,
});
workboxSW.precache([urlToPrecache]);

return new Promise((resolve, reject) => {
const fetchEvent = new FetchEvent('fetch', {
// Request the URL without the directory index, but with the URL param.
request: new Request(urlToRequest),
});
fetchEvent.respondWith = (promiseChain) => {
promiseChain.then(() => {
// Make sure that the cache.match() stub was called with the arguments
// we expect.
if (stub.called) {
try {
const cacheMatchArgument = stub.firstCall.args[0];
expect(cacheMatchArgument).to.be.a('Request');
expect(cacheMatchArgument.url).to.eql(urlToRequest);
resolve();
} catch (error) {
reject(error);
}
} else {
reject('The expected stub function was not called.');
}
});
};
self.dispatchEvent(fetchEvent);
});
});
});

0 comments on commit 30f09c6

Please sign in to comment.