Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
precacheHash ➔ precacheFingerprint
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick committed Jul 31, 2015
1 parent d6eb7e8 commit 76f6667
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
8 changes: 4 additions & 4 deletions bootstrap/sw-toolbox-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
}

var precachePromise;
// When precacheHash is present inside the cacheConfigFile JSON, its a signal that instead of
// reading the list of URLs to precache from the service worker's URL parameters, we need to
// When precacheFingerprint is present inside the cacheConfigFile JSON, its a signal that instead
// of reading the list of URLs to precache from the service worker's URL parameters, we need to
// instead fetch the JSON file and read the list of precache URLs for there. This works around
// the problem that the list of URLs to precache might be longer than the browser-specific limit
// on the size of a service worker's URL.
if (global.params.has('precacheHash') && global.params.has('cacheConfigFile')) {
if (global.params.has('precacheFingerprint') && global.params.has('cacheConfigFile')) {
precachePromise = global.fetch(global.params.get('cacheConfigFile')).then(function(response) {
return response.json();
}).then(function(json) {
return json.precache;
return json.precache || [];
}).catch(function(error) {
return [];
}).then(function(precache) {
Expand Down
29 changes: 18 additions & 11 deletions platinum-sw-cache.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,24 @@
* be concatenated with the values in the JSON file's `precache` property and the set of files
* that are precached will be the union of the two.
*
* There's one additional option, `precacheHash`, that can be set in the JSON. If using a
* build script that might output a large number of files to precache, its recommended
* that your build script generate a stable hash of the serialized `precache` array, and set
* `precacheHash` equal to that hash. That way, any changes to the list of files in `precache`
* will result in a new `precacheHash` value, and that will trigger the service worker
* update flow, which will in turn take care of precaching the new files.
* There's one additional option, `precacheFingerprint`, that can be set in the JSON. If using
* a build script that might output a large number of files to precache, its recommended
* that your build script generate a unique "fingerprint" of the files. Any changes to the
* `precacheFingerprint` value will result in the underlying service worker kicking off the
* process of caching the files listed in `precache`.
* While there are a few different strategies for generating an appropriate
* `precacheFingerprint` value, a process that makes sense is to use a stable hash of the
* serialized `precache` array. That way, any changes to the list of files in `precache`
* will result in a new `precacheFingerprint` value.
* If your build script is Node.js based, one way to generate this hash is:
*
* var md5 = require('crypto').createHash('md5');
* md5.update(JSON.stringify(precache));
* var precacheHash = md5.digest('hex');
* var precacheFingerprint = md5.digest('hex');
*
* Alternatively, you could use something like the
* [SHA-1 signature](http://stackoverflow.com/questions/1161869/how-to-get-sha-of-the-latest-commit-from-remote-git-repository)
* of your latest `git` commit for the `precacheFingerprint` value.
*
* An example file may look like:
*
Expand All @@ -64,7 +71,7 @@
* "defaultCacheStrategy": "fastest",
* "disabled": false,
* "precache": ["file1.html", "file2.css"],
* "precacheHash: "HASH_OF_STRINGIFIED_VALUES_IN_PRECACHE"
* "precacheFingerprint": "FINGERPRINT_OF_FILES_IN_PRECACHE"
* }
*/
cacheConfigFile: String,
Expand All @@ -75,7 +82,7 @@
* in which all the resources will be stored.
*
* If nothing is provided, the default value set in
* [`sw-toolbox.options.cacheName`](https://github.com/GoogleChrome/sw-toolbox/blob/8763dcc9fbc9352d58f184050e2131c42f7b6d68/lib/options.js#L28)
* [`toolbox.options.cacheName`](https://github.com/GoogleChrome/sw-toolbox/blob/8763dcc9fbc9352d58f184050e2131c42f7b6d68/lib/options.js#L28)
* will be used.
*
* The `cacheId` is combined with the service worker's scope to construct the cache name, so
Expand Down Expand Up @@ -158,8 +165,8 @@
// The actual list of files to precache (in config.precache) will be dealt by the
// service worker during the install phase, so we can ignore it here.
// See https://github.com/PolymerElements/platinum-sw/issues/53
if (config.precacheHash) {
params.precacheHash = config.precacheHash;
if (config.precacheFingerprint) {
params.precacheFingerprint = config.precacheFingerprint;
} else {
params.precache = params.precache.concat(config.precache);
}
Expand Down
2 changes: 1 addition & 1 deletion test/platinum-sw-cache/cache-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"defaultCacheStrategy": "networkOnly",
"disabled": false,
"precache": ["listed_in_json1.txt", "listed_in_json2.txt"],
"precacheHash": "DUMMY_HASH"
"precacheFingerprint": "DUMMY_HASH"
}

0 comments on commit 76f6667

Please sign in to comment.