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

Commit

Permalink
Merge pull request #57 from PolymerElements/precache-hash
Browse files Browse the repository at this point in the history
Use precacheHash in the config file in lieu of a precache URL param
  • Loading branch information
wibblymat committed Aug 3, 2015
2 parents 1bd47d1 + 76f6667 commit fd9190f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
24 changes: 21 additions & 3 deletions bootstrap/sw-toolbox-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,28 @@
var strategy = global.params.get('defaultCacheStrategy');
global.toolbox.router.default = global.toolbox[strategy] || global[strategy];
}

if (global.params.has('precache')) {
global.toolbox.precache(global.params.get('precache'));

var precachePromise;
// 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('precacheFingerprint') && global.params.has('cacheConfigFile')) {
precachePromise = global.fetch(global.params.get('cacheConfigFile')).then(function(response) {
return response.json();
}).then(function(json) {
return json.precache || [];
}).catch(function(error) {
return [];
}).then(function(precache) {
return precache.concat(global.params.get('precache'));
})
} else {
precachePromise = Promise.resolve(global.params.get('precache'));
}

global.toolbox.precache(precachePromise);

if (global.params.has('route')) {
var setsOfRouteParams = global.params.get('route');
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
],
"dependencies": {
"polymer": "Polymer/polymer#^1.0.0",
"sw-toolbox": "^2.0.2"
"sw-toolbox": "^2.0.4"
},
"devDependencies": {
"iron-component-page": "PolymerElements/iron-component-page#^1.0.2",
Expand Down
38 changes: 35 additions & 3 deletions platinum-sw-cache.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,33 @@
* 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, `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 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:
*
* {
* "cacheId": "my-cache-id",
* "defaultCacheStrategy": "fastest",
* "disabled": false,
* "precache": ["file1.html", "file2.css"]
* "precache": ["file1.html", "file2.css"],
* "precacheFingerprint": "FINGERPRINT_OF_FILES_IN_PRECACHE"
* }
*/
cacheConfigFile: String,
Expand All @@ -62,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 @@ -126,6 +146,7 @@
};

if (this.cacheConfigFile) {
params.cacheConfigFile = this.cacheConfigFile;
window.fetch(this.cacheConfigFile).then(function(response) {
if (!response.ok) {
throw Error('unable to load ' + this.cacheConfigFile);
Expand All @@ -137,7 +158,18 @@
// Use an empty set of parameters to effectively disable caching.
params = {};
} else {
params.precache = params.precache.concat(config.precache);
// If there's a hash of the list of files to precache provided in the config file,
// then copy that over to the params that will be used to construct the service worker
// URL. This works around the issue where a potentially large number of precache
// files could result in a longer URL than a browser will allow.
// 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.precacheFingerprint) {
params.precacheFingerprint = config.precacheFingerprint;
} else {
params.precache = params.precache.concat(config.precache);
}
params.cacheId = config.cacheId || params.cacheId;
params.defaultCacheStrategy = config.defaultCacheStrategy ||
params.defaultCacheStrategy;
Expand Down
3 changes: 2 additions & 1 deletion test/platinum-sw-cache/cache-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"cacheId": "my-cache-id",
"defaultCacheStrategy": "networkOnly",
"disabled": false,
"precache": ["listed_in_json1.txt", "listed_in_json2.txt"]
"precache": ["listed_in_json1.txt", "listed_in_json2.txt"],
"precacheFingerprint": "DUMMY_HASH"
}
2 changes: 1 addition & 1 deletion test/platinum-sw-cache/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
suite('Precaching', function() {
test('precache results in Cache Storage API entries', function() {
return navigator.serviceWorker.ready.then(function() {
window.fetch(swc.cacheConfigFile).then(function(response) {
return window.fetch(swc.cacheConfigFile).then(function(response) {
return response.json();
}).then(function(config) {
return swc.precache.concat(config.precache);
Expand Down

0 comments on commit fd9190f

Please sign in to comment.