Skip to content

Commit

Permalink
Adds setInitialCacheTimestamp method for consistent cache timestamps,
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 17, 2024
1 parent 65e5ea6 commit 4bd827b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/AssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const debug = require("debug")("Eleventy:Fetch");

class AssetCache {
#customFilename;
#hasSaved = false;
#cacheLocationDirty = false;

constructor(source, cacheDirectory, options = {}) {
if(!Sources.isValidSource(source)) {
Expand All @@ -33,6 +35,14 @@ class AssetCache {
}
}

setInitialCacheTimestamp(timestamp) {
this.initialCacheTimestamp = timestamp;

if(this.#hasSaved) {
throw new Error("`setInitialCacheTimestamp` method must be called before the object is saved.");
}
}

log(message) {
if (this.options.verbose) {
console.log(`[11ty/eleventy-fetch] ${message}`);
Expand Down Expand Up @@ -110,7 +120,7 @@ class AssetCache {

set hash(value) {
if (value !== this._hash) {
this._cacheLocationDirty = true;
this.#cacheLocationDirty = true;
}

this._hash = value;
Expand All @@ -122,7 +132,7 @@ class AssetCache {

set cacheDirectory(dir) {
if (dir !== this._cacheDirectory) {
this._cacheLocationDirty = true;
this.#cacheLocationDirty = true;
}

this._cacheDirectory = dir;
Expand Down Expand Up @@ -160,14 +170,14 @@ class AssetCache {
}

get cache() {
if (!this._cache || this._cacheLocationDirty) {
if (!this._cache || this.#cacheLocationDirty) {
let cache = FlatCacheCreate({
cacheId: this.cacheFilename,
cacheDir: this.rootDir,
});

this._cache = cache;
this._cacheLocationDirty = false;
this.#cacheLocationDirty = false;
}
return this._cache;
}
Expand Down Expand Up @@ -222,9 +232,16 @@ class AssetCache {
async save(contents, type = "buffer", metadata = {}) {
if (this.options.dryRun) {
debug("An attempt was made to save to the file system with `dryRun: true`. Skipping.");

// Errors are still expected from this
this.#hasSaved = true;
return;
}

if(!contents) {
throw new Error("save(contents) expects contents (it was falsy)");
}

if(!this.isDirEnsured) {
this.ensureDir();
}
Expand All @@ -241,12 +258,13 @@ class AssetCache {
debug(`Writing ${contentPath}`);

this.cache.set(this.hash, {
cachedAt: Date.now(),
cachedAt: this.initialCacheTimestamp || Date.now(),
type: type,
metadata,
});

this.cache.save();
this.#hasSaved = true;
}

async getCachedContents(type) {
Expand Down Expand Up @@ -291,6 +309,10 @@ class AssetCache {
return this.getCachedContents(type);
}

getCachedTimestamp() {
return this.cachedObject?.cachedAt || this.initialCacheTimestamp;
}

isCacheValid(duration = this.defaultDuration) {
if (!this.cachedObject) {
// not cached
Expand Down
27 changes: 27 additions & 0 deletions test/AssetCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,30 @@ test("Uses filenameFormat", async (t) => {
t.falsy(fs.existsSync(cachePath));
t.falsy(fs.existsSync(jsonCachePath));
});

test("setInitialCacheTimestamp method (used by Eleventy Image to establish a consistent cached file name in synchronous contexts)", async (t) => {
let cache = new AssetCache("this_is_a_test", ".cache", {
dryRun: true
});
let timestamp = Date.now();
cache.setInitialCacheTimestamp(timestamp);

await cache.save("test");

t.is(cache.getCachedTimestamp(), timestamp);
});

test("setInitialCacheTimestamp method after save throws error", async (t) => {
let cache = new AssetCache("this_is_a_test", ".cache", {
dryRun: true
});
let timestamp = Date.now();

await cache.save("test");

t.throws(() => {
cache.setInitialCacheTimestamp(timestamp);
}, {
message: "`setInitialCacheTimestamp` method must be called before the object is saved."
})
});

0 comments on commit 4bd827b

Please sign in to comment.