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

Commit

Permalink
added: ability to supply html string as dependency for dynamicUrlToDe…
Browse files Browse the repository at this point in the history
…pendencies (#262)
  • Loading branch information
HenrikJoreteg authored and jeffposnick committed Mar 8, 2017
1 parent c24dc9c commit c6aa26f
Showing 1 changed file with 45 additions and 31 deletions.
76 changes: 45 additions & 31 deletions lib/sw-precache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
});

Expand Down

0 comments on commit c6aa26f

Please sign in to comment.