Skip to content

Commit

Permalink
Fix bug with cached input contents being returned buffer property d…
Browse files Browse the repository at this point in the history
…uring dryRun.
  • Loading branch information
zachleat committed Apr 22, 2024
1 parent 5ff8997 commit 06c9505
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
37 changes: 23 additions & 14 deletions img.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,30 @@ class Image {
return JSON.stringify(opts);
}

getFileContents() {
getFileContents(overridePath) {
if(this.isRemoteUrl) {
return false;
}

let src = overridePath || this.src;
if(!this._contents) {
this._contents = {};
}

if(!this._contents[src]) {
// perf: check to make sure it’s not a string first
if(typeof this.src !== "string" && Buffer.isBuffer(this.src)) {
this._contents = this.src;
if(typeof src !== "string" && Buffer.isBuffer(src)) {
this._contents[src] = src;
} else {
// TODO @zachleat make this aggressively async.
// TODO @zachleat add a smarter cache here (not too aggressive! must handle input file changes)
// debug("Reading from file system: %o", this.src);
this._contents = fs.readFileSync(this.src);
// debug("Reading from file system: %o", src);
this._contents[src] = fs.readFileSync(src);
}
}


return this._contents;
return this._contents[src];
}

static getValidWidths(originalWidth, widths = [], allowUpscale = false, minimumThreshold = 1) {
Expand Down Expand Up @@ -538,13 +543,13 @@ class Image {
// Cached images already exist in output
let contents;
if(this.options.dryRun) {
contents = this.getFileContents();
contents = this.getFileContents(stat.outputPath);
stat.buffer = contents;
}

if(outputFormat === "svg" && this.options.svgCompressionSize === "br") {
if(!contents) {
contents = this.getFileContents();
contents = this.getFileContents(stat.outputPath);
}
stat.size = brotliSize.sync(contents);
} else {
Expand Down Expand Up @@ -753,13 +758,17 @@ function queueImage(src, opts) {
}

// Local images
let { width, height, type } = getImageSize(src);
try {
let { width, height, type } = getImageSize(src);

return img.getFullStats({
width,
height,
format: type // only required if you want to use the "auto" format
});
return img.getFullStats({
width,
height,
format: type // only required if you want to use the "auto" format
});
} catch(e) {
throw new Error(`Eleventy Image error (statsOnly): \`image-size\` on "${src}" failed. Original error: ${e.message}`);
}
}

let input = await img.getInput();
Expand Down
2 changes: 1 addition & 1 deletion src/on-request-during-serve-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function eleventyImageOnRequestDuringServePlugin(eleventyConfig, options = {}) {
// src could be file path or full url
let src = decodeURIComponent(url.searchParams.get("src"));
let imageFormat = url.searchParams.get("format");
let width = url.searchParams.get("width");
let width = parseInt(url.searchParams.get("width"), 10);
let via = url.searchParams.get("via");

let defaultOptions;
Expand Down

0 comments on commit 06c9505

Please sign in to comment.