Skip to content

Commit

Permalink
Fix so the defer count only counts unique images, not duplicate reque…
Browse files Browse the repository at this point in the history
…sts to the same image.
  • Loading branch information
zachleat committed May 11, 2024
1 parent 8632433 commit c403b9f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
9 changes: 6 additions & 3 deletions img.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { TemplatePath } = require("@11ty/eleventy-utils");
const svgHook = require("./src/format-hooks/svg.js");
const MemoryCache = require("./src/memory-cache.js");
const DiskCache = require("./src/disk-cache.js");
const DeferCounter = require("./src/defer-counter.js");
const BuildLogger = require("./src/build-logger.js");
const Util = require("./src/util.js");

Expand Down Expand Up @@ -718,7 +719,7 @@ class ImagePath {
/* Size Cache */
let memCache = new MemoryCache();
let diskCache = new DiskCache();
let deferCount = 0;
let deferCounter = new DeferCounter();
let buildLogger = new BuildLogger();

/* Queue */
Expand Down Expand Up @@ -759,13 +760,15 @@ function setupLogger(eleventyConfig, opts) {

buildLogger.setupOnce(eleventyConfig, () => {
// before build
deferCount = 0;
deferCounter.resetCount();
memCache.resetCount();
diskCache.resetCount();
}, () => {
// after build
let [memoryCacheHit] = memCache.getCount();
let [diskCacheHit, diskCacheMiss] = diskCache.getCount();
// these are unique images, multiple requests to optimize the same image are de-duplicated
let deferCount = deferCounter.getCount();

let cachedCount = memoryCacheHit + diskCacheHit;
let optimizedCount = diskCacheMiss + diskCacheHit + memoryCacheHit + deferCount;
Expand Down Expand Up @@ -809,7 +812,7 @@ function queueImage(src, opts) {
setupLogger(eleventyConfig, opts);

if(opts.transformOnRequest) {
deferCount++;
deferCounter.increment(src);
}

if(resolvedOptions.useCache) {
Expand Down
27 changes: 27 additions & 0 deletions src/defer-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class DeferCounter {
constructor() {
this.deferCount = 0;
this.inputs = new Map();
}

resetCount() {
this.deferCount = 0;
}

getCount() {
return this.deferCount;
}

increment(input) {
if(input) {
if(this.inputs.has(input)) {
return;
}
this.inputs.set(input, true);
}

this.deferCount++;
}
}

module.exports = DeferCounter;

0 comments on commit c403b9f

Please sign in to comment.