Skip to content

Unwanted cache key mutation causes poor build performance #312

@sentience

Description

@sentience

TL;DR: I'm seeing in-memory cache misses for identical <img> tags in my site that should be hitting.

I have traced the issue to the re-use of the GLOBAL_OPTIONS object:

this.options = Object.assign({}, GLOBAL_OPTIONS, options);

Some fields of GLOBAL_OPTIONS contain objects (e.g. htmlOptions):

// Defaults used when generateHTML is called from a result set
htmlOptions: {
imgAttributes: {},
pictureAttributes: {},
whitespaceMode: "inline", // "block"
// the <img> will use the largest dimensions for width/height (when multiple output widths are specified)
// see https://github.com/11ty/eleventy-img/issues/63
fallback: "largest", // or "smallest"
},

The htmlOptions.imgAttributes object (among others, possibly) gets mutated when processing an image:

eleventy-img/src/image.js

Lines 324 to 326 in fbca166

// used when results are passed to generate HTML, we maintain some internal metadata about the options used.
let imgAttributes = this.options.htmlOptions?.imgAttributes || {};
imgAttributes.src = this.src;

This mutation is then present in the GLOBAL_OPTIONS object used by subsequent images in the build, causing cache misses for the same image since the options object used in the calculation of the cache key does not match.

Fixing this issue takes my site build from ~30 minutes to ~5 minutes! 🎉


Original issue:

I've just added an image gallery to my site, where each page that displays a full-size image also has all the other images' thumbnails on it (to provide "filmstrip"-style navigation between images). As a result, my site has many identical references to the same source image, which all look like this:

<img
  src="{{ image.src }}"
  alt="{{ image.alt }}"
  class="h-full w-full object-cover transition-transform hover:scale-105"
  eleventy:widths="428,856,268,536,204,408,160,320,288,576"
  sizes="(min-width: 1920px) 428vw, (min-width: 1280px) 268px, (min-width: 1024px) 204px, (min-width: 768px) 160px, (min-width: 640px) 288px, 591px"
  {% unless galleryImage %}
    style="view-transition-name: {{ image.src | slugifyImageSrc }};"
  {% endunless %}
>

While the Image plug-in's build caching works great to accelerate subsequent builds, the initial build takes an extremely long time. Looking at the build output as it runs, it looks to me like these gallery thumbnail images are being processed over and over again (once for each page on which they appear), even though they should have identical input and output, which would make me expect the build output to be reused by these multiple img tags:

CleanShot.2025-12-01.at.12.14.34.mp4

Any chance I'm doing something wrong here? At this point my initial build takes over 30 minutes on my host (CloudCannon), which causes it to time out and fail. I can then trigger another build that runs in under 2 minutes thanks to the cached output of the failed build, but this is not a workable workflow for the non-technical contributors to my site.

This is with @11ty/eleventy-img@6.0.1, and this is my plugin configuration:

  eleventyConfig.addPlugin(eleventyImageTransformPlugin, {
    urlPath: "/img/",
    outputDir: "node_modules/.cache/@11ty/img/",
    svgShortCircuit: "size",
  });
  eleventyConfig.on("eleventy.after", () => {
    fs.cpSync(
      "node_modules/.cache/@11ty/img/",
      path.join(eleventyConfig.directories.output, "/img/"),
      { recursive: true },
    );
  });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions