Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
Use the expected hash as the cache busting param.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick authored and addyosmani committed Apr 28, 2016
1 parent 3d150a0 commit 293f2d6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ module.exports = {
return url.toString();
},

getCacheBustedUrl: function(url, now) {
now = now || Date.now();
getCacheBustedUrl: function(url, param) {
param = param || Date.now();

var urlWithCacheBusting = new URL(url);
urlWithCacheBusting.search += (urlWithCacheBusting.search ? '&' : '') +
'sw-precache=' + now;
'sw-precache=' + param;

return urlWithCacheBusting.toString();
},
Expand Down
14 changes: 8 additions & 6 deletions service-worker.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ function deleteAllCaches() {
}

self.addEventListener('install', function(event) {
var now = Date.now();

event.waitUntil(
// Take a look at each of the cache names we expect for this version.
Promise.all(Object.keys(CurrentCacheNamesToAbsoluteUrl).map(function(cacheName) {
Expand All @@ -72,12 +70,16 @@ self.addEventListener('install', function(event) {
// For caches that are already populated for a given version of a
// resource, there should be 1 entry.
return cache.keys().then(function(keys) {
// If there are 0 entries, either because this is a brand new version
// of a resource or because the install step was interrupted the
// last time it ran, then we need to populate the cache.
if (keys.length === 0) {
// If there are 0 entries, either because this is a brand new version
// of a resource or because the install step was interrupted the
// last time it ran, then we need to populate the cache.
// Use the last bit of the cache name, which contains the hash,
// as the cache-busting parameter.
// See https://github.com/GoogleChrome/sw-precache/issues/100
var cacheBustParam = cacheName.split('-').pop();
var urlWithCacheBusting = getCacheBustedUrl(
CurrentCacheNamesToAbsoluteUrl[cacheName], now);
CurrentCacheNamesToAbsoluteUrl[cacheName], cacheBustParam);

var request = new Request(urlWithCacheBusting,
{credentials: 'same-origin'});
Expand Down

0 comments on commit 293f2d6

Please sign in to comment.