Skip to content

Commit

Permalink
Fixes #146
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 19, 2024
1 parent dc7df7d commit 2e84f6b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"homepage": "https://github.com/11ty/eleventy-img#readme",
"dependencies": {
"@11ty/eleventy-fetch": "^5.0.1",
"@11ty/eleventy-fetch": "^5.0.2-beta.1",
"@11ty/eleventy-utils": "^1.0.3",
"brotli-size": "^4.0.0",
"debug": "^4.4.0",
Expand All @@ -53,7 +53,7 @@
"@eslint/js": "^9.17.0",
"ava": "^6.2.0",
"eslint": "^9.17.0",
"globals": "^15.13.0",
"globals": "^15.14.0",
"pixelmatch": "^5.3.0"
},
"ava": {
Expand Down
16 changes: 9 additions & 7 deletions src/disk-cache.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const fs = require("node:fs");
// const debug = require("debug")("Eleventy:Image");

class DiskCache {
Expand All @@ -23,15 +22,19 @@ class DiskCache {
return [this.hitCounter, this.missCounter];
}

isCached(path, input, incrementCounts = true) {
// Disk cache runs once per output file, so we only want to increment counts once per input
if(this.inputs.has(input)) {
isCached(targetFile, sourceInput, incrementCounts = true) {
if(!this.#existsCache) {
throw new Error("Missing `#existsCache`");
}

// Disk cache runs once per output file, so we only increment counts once per input
if(this.inputs.has(sourceInput)) {
incrementCounts = false;
}

this.inputs.set(input, true);
this.inputs.set(sourceInput, true);

if(this.#existsCache?.exists(path) || fs.existsSync(path)) {
if(this.#existsCache?.exists(targetFile)) {
if(incrementCounts) {
this.hitCounter++;
}
Expand All @@ -41,7 +44,6 @@ class DiskCache {
}

if(incrementCounts) {
this.inputs.set(input, true);
this.missCounter++;
}

Expand Down
4 changes: 2 additions & 2 deletions src/global-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ const DEFAULTS = {

fixOrientation: false, // always rotate images to ensure correct orientation

// Advanced
useCacheValidityInHash: true,
// Removed, no longer necessary in v6.0
// useCacheValidityInHash: true,

// When the original width is smaller than the desired output width, this is the minimum size difference
// between the next smallest image width that will generate one extra width in the output.
Expand Down
47 changes: 27 additions & 20 deletions src/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ const { createHash } = require("node:crypto");
const sharp = require("sharp");
const getImageSize = require("image-size");
const brotliSize = require("brotli-size");
const debug = require("debug")("Eleventy:Image");
const debugUtil = require("debug");
const debug = debugUtil("Eleventy:Image");
const assetsDebug = debugUtil("Eleventy:Assets");

const { CreateFetch } = require("@11ty/eleventy-fetch");
const { Fetch } = require("@11ty/eleventy-fetch");

const Util = require("./util.js");
const ImagePath = require("./image-path.js");
Expand Down Expand Up @@ -67,7 +69,7 @@ class Image {
}, this.options.cacheOptions);

// v6.0.0 this now inherits eleventy-fetch option defaults
this.assetCache = CreateFetch(src, this.cacheOptions);
this.assetCache = Fetch(src, this.cacheOptions);
}
}

Expand Down Expand Up @@ -322,20 +324,9 @@ class Image {
// probably a remote URL
hash.update(this.src);

// add whether or not the cached asset is still valid per the cache duration (work with empty duration or "*")
if(this.assetCache) {
if(this.assetCache.getDurationMs(this.cacheOptions.duration) === 0) {
// don’t use cache to inform hash if duration is set to 0.
} else if(this.options.useCacheValidityInHash && this.isRemoteUrl) {
let stamp = this.assetCache.getCachedTimestamp();
if(!stamp) {
// We need to propose the timestamp for cache validity across builds
// This needs to be known before fetching (or in sync contexts)
stamp = Date.now();
this.assetCache.setInitialCacheTimestamp(stamp);
}
hash.update(`Cache:${stamp}`);
}
// `useCacheValidityInHash` was removed in v6.0.0, but we’ll keep this as part of the hash to maintain consistent hashes across versions
if(this.isRemoteUrl && this.assetCache && this.cacheOptions) {
hash.update(`ValidCache:true`);
}
}

Expand All @@ -359,8 +350,6 @@ class Image {

hash.update(JSON.stringify(hashObject));

// TODO allow user to update other things into hash

// Get hash in base64, and make it URL safe.
// NOTE: When increasing minimum Node version to 14,
// replace with hash.digest('base64url')
Expand Down Expand Up @@ -481,6 +470,23 @@ class Image {
return this._transformRawFiles(results, outputFormats);
}

isOutputCached(targetFile, sourceInput) {
if(!this.options.useCache) {
return false;
}

// last cache was a miss, so we must write to disk
if(this.assetCache && !this.assetCache.wasLastFetchCacheHit()) {
return false;
}

if(!diskCache.isCached(targetFile, sourceInput, !Util.isRequested(this.options.generatedVia))) {
return false;
}

return true;
}

// src should be a file path to an image or a buffer
async resize(input) {
let sharpImage = sharp(input, Object.assign({
Expand All @@ -497,7 +503,7 @@ class Image {
let fullStats = this.getFullStats(metadata);
for(let outputFormat in fullStats) {
for(let stat of fullStats[outputFormat]) {
if(this.options.useCache && diskCache.isCached(stat.outputPath, input, !Util.isRequested(this.options.generatedVia))) {
if(this.isOutputCached(stat.outputPath, input)) {
// Cached images already exist in output
let contents;
if(this.options.dryRun) {
Expand Down Expand Up @@ -599,6 +605,7 @@ class Image {
if(this.options.dryRun) {
debug( "Generated %o", stat.url );
} else {
assetsDebug("Wrote image file to disk: %o", stat.outputPath);
debug( "Wrote %o", stat.outputPath );
}
}
Expand Down

0 comments on commit 2e84f6b

Please sign in to comment.