Skip to content

Commit

Permalink
Support for array of keys to assetcache hashing.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed May 13, 2024
1 parent cbd67e3 commit d9f3a73
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/AssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ class AssetCache {
}

// Defult hashLength also set in global options, duplicated here for tests
static getHash(url, hashLength = 30) {
// v5.0+ key can be Array or literal
static getHash(key, hashLength = 30) {
let hash = createHash("sha256");
hash.update(url);

if (!Array.isArray(key)) {
key = [key];
}

for (let k of key) {
hash.update(k);
}

return ("" + hash.digest("hex")).slice(0, hashLength);
}

Expand Down
4 changes: 3 additions & 1 deletion src/RemoteAssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class RemoteAssetCache extends AssetCache {
if (options.removeUrlQueryParams) {
cleanUrl = RemoteAssetCache.cleanUrl(cleanUrl);
}

let cacheKey = [cleanUrl];

if (options.fetchOptions) {
if (options.fetchOptions.method && options.fetchOptions.method !== "GET") {
cacheKey.push(options.fetchOptions.method);
Expand All @@ -19,7 +21,7 @@ class RemoteAssetCache extends AssetCache {
cacheKey.push(options.fetchOptions.body);
}
}
cacheKey = cacheKey.length > 1 ? JSON.stringify(cacheKey) : cleanUrl;

super(cacheKey, cacheDirectory, options);

this.url = url;
Expand Down

0 comments on commit d9f3a73

Please # to comment.