diff --git a/lib/sw-precache.js b/lib/sw-precache.js index 8faa8e0..4a319d8 100644 --- a/lib/sw-precache.js +++ b/lib/sw-precache.js @@ -188,47 +188,61 @@ function generate(params, callback) { }); Object.keys(params.dynamicUrlToDependencies).forEach(function(dynamicUrl) { - if (!Array.isArray(params.dynamicUrlToDependencies[dynamicUrl])) { + var dependency = params.dynamicUrlToDependencies[dynamicUrl]; + var isString = typeof dependency === 'string'; + + if (!Array.isArray(dependency) && !isString) { throw Error(util.format( 'The value for the dynamicUrlToDependencies.%s ' + - 'option must be an Array.', + 'option must be an Array or a String.', dynamicUrl)); } - var filesAndSizesAndHashes = params.dynamicUrlToDependencies[dynamicUrl] - .sort() - .map(function(file) { - try { - return getFileAndSizeAndHashForFile(file); - } catch (e) { - // Provide some additional information about the failure if the file is missing. - if (e.code === 'ENOENT') { - params.logger(util.format( - '%s was listed as a dependency for dynamic URL %s, but ' + - 'the file does not exist. Either remove the entry as a ' + - 'dependency, or correct the path to the file.', - file, dynamicUrl - )); + if (isString) { + cumulativeSize += dependency.length; + relativeUrlToHash[dynamicUrl] = getHash(dependency); + } else { + var filesAndSizesAndHashes = dependency + .sort() + .map(function(file) { + try { + return getFileAndSizeAndHashForFile(file); + } catch (e) { + // Provide some additional information about the failure if the file is missing. + if (e.code === 'ENOENT') { + params.logger(util.format( + '%s was listed as a dependency for dynamic URL %s, but ' + + 'the file does not exist. Either remove the entry as a ' + + 'dependency, or correct the path to the file.', + file, dynamicUrl + )); + } + // Re-throw the exception unconditionally, since this should be treated as fatal. + throw e; } - // Re-throw the exception unconditionally, since this should be treated as fatal. - throw e; - } - }); - var concatenatedHashes = ''; + }); + var concatenatedHashes = ''; - filesAndSizesAndHashes.forEach(function(fileAndSizeAndHash) { - // Let's assume that the response size of a server-generated page is roughly equal to the - // total size of all its components. - cumulativeSize += fileAndSizeAndHash.size; - concatenatedHashes += fileAndSizeAndHash.hash; - }); + filesAndSizesAndHashes.forEach(function(fileAndSizeAndHash) { + // Let's assume that the response size of a server-generated page is roughly equal to the + // total size of all its components. + cumulativeSize += fileAndSizeAndHash.size; + concatenatedHashes += fileAndSizeAndHash.hash; + }); - relativeUrlToHash[dynamicUrl] = getHash(concatenatedHashes); + relativeUrlToHash[dynamicUrl] = getHash(concatenatedHashes); + } if (params.verbose) { - params.logger(util.format( - 'Caching dynamic URL "%s" with dependencies on %j', - dynamicUrl, params.dynamicUrlToDependencies[dynamicUrl])); + if (isString) { + params.logger(util.format( + 'Caching dynamic URL "%s" with dependency on user-supplied string', + dynamicUrl)); + } else { + params.logger(util.format( + 'Caching dynamic URL "%s" with dependencies on %j', + dynamicUrl, dependency)); + } } });