Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strips the hash from a URL in a couple of places #633

Merged
merged 3 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions packages/workbox-cache-expiration/src/lib/cache-expiration.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,18 @@ class CacheExpiration {
*
* @param {Object} input
* @param {string} input.cacheName Name of the cache the Responses belong to.
* @param {string} input.url The URL for the entry to update.
* @param {Number} [input.now] A timestamp.
*
* Defaults to the current time.
*
* @param {string} input.url The URL for the entry to update. The hash portion
* of the URL will be ignored.
* @param {Number} [input.now] A timestamp. Defaults to the current time.
*/
async updateTimestamp({cacheName, url, now} = {}) {
isType({url}, 'string');
isType({cacheName}, 'string');

// Remove the hash, if present.
const urlObject = new URL(url, location);
urlObject.hash = '';

if (typeof now === 'undefined') {
now = Date.now();
}
Expand All @@ -205,7 +207,7 @@ class CacheExpiration {
const tx = db.transaction(cacheName, 'readwrite');
tx.objectStore(cacheName).put({
[timestampPropertyName]: now,
[urlPropertyName]: url,
[urlPropertyName]: urlObject.href,
});

await tx.complete;
Expand Down
24 changes: 24 additions & 0 deletions packages/workbox-cache-expiration/test/sw/cache-expiration.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,28 @@ describe('Test of the CacheExpiration class', function() {
.then((idbEntryUrls) => expect(idbEntryUrls).to.eql(urls.slice(extraEntryCount)));
});
});

it(`should ignore the URL's hash when updateTimestamp() is called`, async function() {
const cacheExpiration = new CacheExpiration({maxEntries: MAX_ENTRIES});
const cacheName = getUniqueCacheName();
const url = getUniqueUrl();
const firstNow = NOW;

await cacheExpiration.updateTimestamp({cacheName, url, now: firstNow});
const db = await cacheExpiration.getDB({cacheName});
const firstTx = db.transaction(cacheName, 'readonly');
const firstStore = firstTx.objectStore(cacheName);
const firstEntry = await firstStore.get(url);
expect(firstEntry[timestampPropertyName]).to.equal(firstNow);

const urlWithHash = `${url}#hashvalue`;
const secondNow = firstNow + 1;

await cacheExpiration.updateTimestamp({cacheName, url: urlWithHash, now: secondNow});
const secondTx = db.transaction(cacheName, 'readonly');
const secondStore = secondTx.objectStore(cacheName);
// Get the entry for the original url.
const secondEntry = await secondStore.get(url);
expect(secondEntry[timestampPropertyName]).to.equal(secondNow);
});
});
6 changes: 6 additions & 0 deletions packages/workbox-sw/src/lib/workbox-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ class WorkboxSW {
});

const capture = ({url}) => {
// See https://github.com/GoogleChrome/workbox/issues/488.
// The incoming URL might include a hash/URL fragment, and the URLs in
// the cachedUrls array will never include a hash. We need to normalize
// the incoming URL to ensure that the string comparison works.
url.hash = '';

const cachedUrls = this._revisionedCacheManager.getCachedUrls();
if (cachedUrls.indexOf(url.href) !== -1) {
return true;
Expand Down
12 changes: 12 additions & 0 deletions packages/workbox-sw/test/browser/ignore-url-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,16 @@ describe('Test ignore url params matching', function() {
});
});
});

it(`should ignore the URL's hash when routing to a precached URL`, async function() {
const responseBody = 'hello';
const urlObject = new URL(`/__echo/date/${responseBody}`, location);
urlObject.hash = 'should-be-ignored';

const iframe = await goog.swUtils.activateSW('../static/sw/ignore-url-params.js');
const fetchResponse = await iframe.contentWindow.fetch(urlObject.href);
const fetchResponseBody = await fetchResponse.text();

expect(fetchResponseBody.startsWith(responseBody)).to.be.true;
});
});