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

added: ability to supply html string as dependency for dynamicUrlToDependencies #262

Merged
merged 1 commit into from
Mar 8, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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